COGS——C610. 数对的个数

http://cogs.pro/cogs/problem/problem.php?pid=610

Description
出题是一件痛苦的事情!
题目看多了也有审美疲劳,于是我舍弃了大家所熟悉的A+B Problem,改用A-B了哈哈!

好吧,题目是这样的:给出一串数以及一个数字C,要求计算出所有A-B=C的数对的个数。

(不同位置的数字一样的数对算不同的数对)
Input Format
第一行包括2个非负整数N和C,中间用空格隔开。
第二行有N个整数,中间用空格隔开,作为要求处理的那串数。
Output Format
输出一行,表示该串数中包含的所有满足A-B=C的数对的个数。
Sample Input
4 1
1 1 2 3
Sample Output
3
Data Limit
对于90%的数据,N <= 2000;
对于100%的数据,N <= 200000。
所有输入数据都在longint范围内。
 

 1 #include <algorithm>
 2 #include <iostream>
 3 #include <cstdio>
 4 #include <map>
 5 #define LL long long
 6 
 7 using namespace std;
 8 
 9 map<LL,LL>ma;
10 LL n,c,ans;
11 LL a[200005],sum[2000005];
12 
13 int main()
14 {
15     freopen("dec.in","r",stdin);
16     freopen("dec.out","w",stdout);
17     cin>>n>>c;
18     for(int i=1;i<=n;i++)
19     {
20         cin>>a[i];
21         ma[a[i]]=1;
22         sum[a[i]]++;
23     }
24     sort(a+1,a+n+1);
25     for(int i=1;i<=n;i++)
26         if(ma[c+a[i]])
27             ans+=sum[a[i]+c];
28     cout<<ans;
29     return 0;
30 }
——每当你想要放弃的时候,就想想是为了什么才一路坚持到现在。
原文地址:https://www.cnblogs.com/Shy-key/p/6724993.html