1.问题描述:
2.算法分析:
这道题目如果你非要算A - B = C你枚举都不知道枚举多少层,我们转换一下求出A - C = B就可以了,两个二重循环遍历,扫一下数组即可,时间复杂度为O(n^2), 满分数据为200000,必吃T,那么我们来看该怎么降复杂度呢,可以使用STL 中的map, map的关键字存A - C的值,映射的是次数。这样就可以使得时间复杂度降为O(logn)可以过得,这也相当于空间换时间了。
3.AC代码:
#include <cstdio>
#include <algorithm>
#include <map>
using namespace std;
const int maxn = 2e5 + 5;
typedef long long LL;
LL a[maxn];
map<LL,LL> mp;
int main() {
int n;
LL c;
scanf("%d%lld", &n, &c);
for (int i = 1; i <= n; i++) {
scanf("%lld", &a[i]);
mp[a[i]]++;
a[i] -= c;
}
LL ans = 0;
for (int i = 1; i <= n; i++) {
ans += mp[a[i]];
}
printf("%lld\n", ans);
return 0;
}