★★   输入文件:dec.in   输出文件:dec.out   简单对比
时间限制:1 s   内存限制:128 MB

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范围内。
 

map大法好!!!

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<cmath>
 5 #include<vector>
 6 #include<map>
 7 #define LL long long 
 8 using namespace std;
 9 LL read(LL & n)
10 {
11     int flag=0,x=0;char c='/';
12     while(c<'0'||c>'9'){c=getchar();if(c=='-')flag=1;}
13     while(c>='0'&&c<='9')x=x*10+(c-48),c=getchar();
14     if(flag)n=-x;
15     else n=x;
16 }
17 LL n,c,p,ans=0,a[200001];
18 map<LL,int>mp;
19 int main()
20 {
21     freopen("dec.in","r",stdin);
22     freopen("dec.out","w",stdout);
23     read(n);read(c);
24     for(int i=1;i<=n;i++)
25         read(a[i]),mp[a[i]]++;
26     for(int i=1;i<=n;i++)
27     {LL now=a[i]-c;ans=ans+mp[now];}    
28     cout<<ans;
29     return 0;
30 }

 

内容来源于网络如有侵权请私信删除
你还没有登录,请先登录注册
  • 还没有人评论,欢迎说说您的想法!

相关课程