题目意思
给出n个数,\(s_1...s_i...s_n\)
然后让你求\(|j - i| = s[i]+s[j]\)出现的次数
solution
我们就直接假设j>i;
然后把上边的式子移一下项就得到了
\[j - s[j] = i+s[i] \]
因为这个题有2秒和1G
所以我们就直接开一个map存每个i+s[i] 出现的次数
然后看j-s[j]出现了几次
复杂度就是\(O(n)\)
code
/*
Auther:_Destiny
time:2020.5.3
*/
#include <bits/stdc++.h>
#define ll long long
#define N 200010
#define M 1010
using namespace std;
int n;
int f[N];
map<int, int> ss;
int read() {
int s = 0, f = 0; char ch = getchar();
while (!isdigit(ch)) f |= (ch == '-'), ch = getchar();
while (isdigit(ch)) s = s * 10 + (ch ^ 48), ch = getchar();
return f ? -s : s;
}
int main() {
n = read();
for (int i = 1; i <= n; i++)
f[i] = read();
for (int i = 1; i <= n; i++) {
int x = i + f[i];
ss[x]++;
}
ll ans = 0;
for (int i = 1; i <= n; i++)
ans += ss[i - f[i]];
cout << ans << "\n";
}