题解 | 谐距下标对
谐距下标对
https://www.nowcoder.com/practice/12fd032361704c978bcb9c2c2b3bb93d
// 这题TmD居然卡long long 气死我了,入门题卡我10分钟
// 必须收藏留着过年复习
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
unordered_map<int,ll>mp;
int main() {
int n;cin>>n;ll cnt = 0;
mp.clear();
for (int i = 1;i <= n;i++){
int t;cin>>t;
mp[t-i]++;
}
for (auto &[k,v]:mp){
if (v <= 1)continue;
cnt+=(v-1)*v/2;
}cout<<cnt;
return 0;
}
// 64 位输出请用 printf("%lld")

查看4道真题和解析