可以使用STL,比如用map存
by anonymous_letter @ 2022-04-14 14:35:42
对数据离散化后再开桶记录,查询的时候直接二分或者 `lower_bound` 找一下在桶中对应的位置即可。
by Suzt_ilymtics @ 2022-04-14 14:37:32
@[bloodstalk](/user/231543) 对每个数进行哈希
by Usada_Pekora @ 2022-04-14 14:38:20
@[bloodstalk](/user/231543) 不行,内存限制是 $128 \rm MB$,只能开 $3.3e7$ 左右的 $\rm int$ 变量,建议学习二分答案或者 $\rm map$。
by uniqueharry @ 2022-04-14 14:38:36
哦你可能没学过离散化和 `lower_bound`,可以自己百度去搜一下是什么怎么用,实现可以参考我的代码
```cpp
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int MAXN = 2e5+5;
const int INF = 1e9+7;
const int mod = 1e9+7;
int n, c, ans = 0;
int a[MAXN], date[MAXN], Cnt = 0;
int cnt[MAXN];
int read(){
int s = 0, f = 0;
char ch = getchar();
while(!isdigit(ch)) f |= (ch == '-'), ch = getchar();
while(isdigit(ch)) s = (s << 1) + (s << 3) + ch - '0' , ch = getchar();
return f ? -s : s;
}
signed main()
{
n = read(), c = read();
for(int i = 1; i <= n; ++i) a[i] = date[i] = read();
sort(date + 1, date + n + 1); date[0] = -INF;
for(int i = 1; i <= n; ++i) if(date[i - 1] != date[i]) date[++Cnt] = date[i];
for(int i = 1; i <= n; ++i) {
a[i] = lower_bound(date + 1, date + Cnt + 1, a[i]) - date;
cnt[a[i]]++;
}
for(int i = 1; i <= Cnt; ++i) {
int x = lower_bound(date + 1, date + Cnt + 1, c + date[i]) - date;
if(date[x] == c + date[i]) {
ans += cnt[x] * cnt[i];
}
}
printf("%lld", ans);
return 0;
}
```
by Suzt_ilymtics @ 2022-04-14 14:39:59
@[Zyingyzzz](/user/434929) @[dd12345](/user/667448) @[Suzt_ilymtics](/user/230580) @uniqueharry谢谢朋友们过了
```cpp
#include<bits/stdc++.h>
using namespace std;
const int N=200005;
map<long long,long long> tong;
int a[N];
int n,c;
long long ans;
int main()
{
scanf("%d%d",&n,&c);
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
tong[a[i]]++;
}
sort(a+1,a+n+1);
for(int i=1;i<=n;i++)
{
if(a[i]!=a[i-1])
ans+=tong[a[i]]*tong[a[i]-c];
}
printf("%lld",ans);
}/*10 3
10 4 7 5 10 4 5 8 5 7*/
```
by bloodstalk @ 2022-04-14 14:51:29