題目意思
給出n個數,\(s_1...s_i...s_n\)
然後讓你求\(|j - i| = s[i]+s[j]\)出現的次數
solution
我們就直接假設j>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";
}