题目
You are given an array 𝑎 of 𝑛 integers. Count the number of pairs of
indices (𝑖,𝑗) such that 𝑖<𝑗 and 𝑎𝑗−𝑎𝑖=𝑗−𝑖.Input The first line contains one integer 𝑡 (1≤𝑡≤104). Then 𝑡 test
cases follow.The first line of each test case contains one integer 𝑛 (1≤𝑛≤2⋅105).
The second line of each test case contains 𝑛 integers 𝑎1,𝑎2,…,𝑎𝑛
(1≤𝑎𝑖≤𝑛) — array 𝑎.It is guaranteed that the sum of 𝑛 over all test cases does not
exceed 2⋅105.Output For each test case output the number of pairs of indices
(𝑖,𝑗) such that 𝑖<𝑗 and 𝑎𝑗−𝑎𝑖=𝑗−𝑖.Example inputCopy 4 6 3 5 1 4 6 6 3 1 2 3 4 1 3 3 4 6 1 6 3 4 5 6
outputCopy 1 3 3 10
题解
𝑎𝑗−𝑎𝑖=𝑗−𝑖,
𝑎𝑗−𝑗=𝑎𝑖−𝑖
突然题目就弱智了起来
代码
#include <bits/stdc++.h>
using namespace std;
void solve() {
int n;
cin >> n;
map<int, int> a;
long long res = 0;
for (int i = 0; i < n; i++) {
int x;
cin >> x;
x -= i;
res += a[x];
a[x]++;
}
cout << res << endl;
}
int main() {
int tests;
cin >> tests;
while (tests-- > 0) {
solve();
}
return 0;
}