https://www.luogu.com.cn/problem/P1102
使用一个map记录一样的数字出现的次数,使用另外一个map记录数字来寻找比自己大c和小c的数字
7 10 与10 7都算,最后整体除以2就行
#include<iostream> #include<cstdio> #include<map> using namespace std; map<long long , long long>list; map<long long, long long>amount; int main() { long long n, c; long long counts = 0; scanf("%lld%lld", &n, &c); for (int i = 1; i <=n; i++) { long long a; scanf("%lld", &a); list[a] = i; amount[a]++; } map<long long, long long>::iterator it = amount.begin(); for (; it != amount.end(); it++) { if (list[it->first + c] != 0)counts += amount[it->first + c] * amount[it->first]; if (list[it->first - c] != 0)counts += amount[it->first - c] * amount[it->first]; } printf("%lld", counts/2); }
原文:https://www.cnblogs.com/Jason66661010/p/13205230.html