P1102 A-B 数对 hash + sort
作者:
多米尼克領主的致意
,
2024-05-06 22:49:33
,
所有人可见
,
阅读 2
O(nlogn) map比数组好的是hash范围可以开到int 或者 ll 而数组则是2e7
但是复杂度略微高于手写hash
#include<bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
typedef long long ll;
int a[N], b[N];
int n, m;
map<int, int>h;
ll ans;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
for(int i =1;i <= n;i++)cin >> a[i];
sort(a + 1, a + 1 + n);
int last = -1, cnt = 0;
for(int i = 1;i <= n;i++)
{
if(a[i] != last){
last = a[i];
b[++cnt] = a[i];
}
h[a[i]] += 1;
}
// for(int i = 1;i <= n;i++)cout << h[a[i]] << endl;
for(int i = 1;i <= n;i++)
{
if(h[a[i] - m])
{
ans += h[a[i] - m];
}
}
cout << ans;
}