此处最重要的就是 理解map[a[i]]++ 每出现一次就次数加一,在遍历完数组值后 由已知关系式 A-B=C---->A-C=B 那么在满足关系式的情况下,如果出现了该数组中本来就有的数字,那么它的出现次数之和就是你符合条件的次数
1 #include <iostream>
2 #include <map>
3 using namespace std;
4 typedef long long LL;
5 LL a[200001];
6 map<LL,LL> m;//建立一个数字到出现次数的映射 map<num,times>
7 //A-B=C --> A-C=B
8 int main() {
9 int n;
10 LL c;
11 LL ans=0;
12 cin >> n >> c;
13 for(int i=1;i<=n;i++) {
14 cin >> a[i];
15 m[a[i]]++;
16 a[i]-=c;
17 }
18 for(int i=1;i<=n;i++) ans+=m[a[i]];
19 cout << ans << endl;
20 return 0;
21 }