题解:AT_abc417_c Distance Indicators
观察式子:
考虑枚举
那么我们就需要先开一个 map,记为 cnt,在输入的时候对于每一个元素,进行 cnt[i - a[i]]++ 的操作。然后在枚举到每一个 ans += cnt[i + a[i]] 统计答案。
题目说了
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5 + 5;
int n, a[N];
map <int, int> cnt;
int main(){
ios :: sync_with_stdio(false);
cin >> n;
for(int i = 1; i <= n; i++) cin >> a[i], cnt[i - a[i]]++;
ll ans = 0;
for(int i = 1; i <= n; i++)
ans += cnt[i + a[i]];
cout << ans;
return 0;
}