题目链接:https://www.luogu.org/problemnew/show/P4889
主要涉及到map的用法,看到题目时不要思维定式,灵活转化即可A掉。
code:
#include<iostream>
#include<map>
using namespace std;
typedef long long ll;
map<ll, ll> M;
int n, m;
ll ans;
ll f(ll x) {
if (x<2) return 0;
else return (x-1)*x/2;
}
int main() {
ll k;
cin >> n >> m;
for (int i=1; i<=n; i++) {
cin >> k;
M[i+k]++;
M[i-k]++;
}
for (map<ll, ll>::iterator it=M.begin(); it!=M.end(); it++)
ans+=f(it->second);//it->second,是指向映射出的值,it->first是指向被映射的值
cout << ans;
return 0;
}