题解
2024-08-05 20:19:24
发布于:湖南
31阅读
0回复
0点赞
用桶的话相对简单一些
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
int a[200005], bucket[400005];
int read(){
char c = getchar();
int x = 0, f = 1;
while(!isdigit(c)){
if(c == '-') f = -f;
c = getchar();
}
while(isdigit(c)){
x = (x << 3) + (x << 1) + c - '0';
c = getchar();
}
return x * f;
}
int main(){
int n = read();
for(int i = 1; i <= n; i++){
a[i] = read();
bucket[a[i] - i + 200000]++;//注意要偏移(可能是负的)
}
long long ct = 0;
for(int i = 0; i <= 400000; i++){
ct += 1ll * bucket[i] * (bucket[i] - 1) / 2;//这里寻找相同元素数量就简单多了
}
cout << ct;
return 0;
}
什么?你想看无桶解法?在这看吧!
这里空空如也
有帮助,赞一个