题解 | #下标数组#
下标数组
https://www.nowcoder.com/practice/2eac3a5a8cee42da8de003ecca29e271
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> a(n+1); for(int i = 1; i <= n; i++) cin >> a[i]; unordered_map<int,int> cnt; long long ret = 0; for(int i = 1; i <= n; i++){ if(cnt.find(i-a[i]) != cnt.end()){ ret += cnt[i-a[i]]; } cnt[a[i] - i]++;; } cout << ret << endl; return 0; }