题解 | 数对计数
数对计数
https://www.nowcoder.com/practice/7d05171e7e0e4c6086be233769e01d94
/*
我们要是用一个multiset来装所有数字
对于每一个新加入的数字,我们统计以下有多少数字能跟他进行加加减减操作后,
结果的绝对值是c
然后把这两个数字往res上进行累加
*/
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int rd()
{
int rs = 0;char c = getchar();
while (!isdigit(c))
c = getchar();
while (isdigit(c))
{
rs = (rs<<3)+(rs<<1)+c-'0';
c = getchar();
}return rs;
}
void wt(int t)
{
if (t > 9) wt(t /10);
putchar(t%10|48);
}
void solve()
{
int n = rd(),c = rd();
multiset<int>ms;
int res = 0;
for (int i = 0;i < n;i++)
{
int t = rd();
res+=ms.count(t+c);
res+=ms.count(t-c);
ms.insert(t);
}
wt(res);
}
int main() {
solve();return 0;
}
// 64 位输出请用 printf("%lld")
