当前位置: 代码迷 >> 综合 >> This Message Will Self-Destruct in 5s
  详细解决方案

This Message Will Self-Destruct in 5s

热度:74   发布时间:2024-01-11 14:52:46.0

This Message Will Self-Destruct in 5s

题目描述

You are the top spy of AtCoder Kingdom. To prevent the stolen secret from being handed to AlDebaran Kingdom, you have sneaked into the party where the transaction happens.
There are N attendees in the party, and they are given attendee numbers from 1 through N. The height of Attendee i is Ai.
According to an examination beforehand, you know that a pair of attendees satisfying the condition below will make the transaction.
The absolute difference of their attendee numbers is equal to the sum of their heights.There are N(N?1)/2 ways to choose two from the N attendees and make a pair. Among them, how many satisfy the condition above?
P.S.: We cannot let you know the secret.

输入

Input is given from Standard Input in the following format:
NA1 A2 … AN

输出

Print the number of pairs satisfying the condition.

样例输入
【样例1】
6
2 3 3 1 3 1
【样例2】
6
5 2 4 2 8 8
【样例3】
32
3 1 4 1 5 9 2 6 5 3 5 8 9 7 9 3 2 3 8 4 6 2 6 4 3 3 8 3 2 7 9 5
样例输出
【样例1】
3
【样例2】
0
【样例3】
22
提示

样例1解释:
·A1+A4=3, so the pair of Attendee 1 and 4 satisfy the condition.
·A2+A6=4, so the pair of Attendee 2 and 6 satisfy the condition.
·A4+A6=2, so the pair of Attendee 4 and 6 satisfy the condition.
No other pair satisfies the condition, so you should print 3.

思路:

ai+aj =j-i => ai+i == j-aj ,用map,匹配

代码:
#include<iostream>
#include<algorithm>
#include<map>
using namespace std;
typedef long long ll;
const int N=200010;
ll n,ans;
ll a[N];
map<ll,ll> ma;
int main()
{//ai+aj==j-i  =>  ai+i==j-aj cin>>n;for(int i=1;i<=n;i++){cin>>a[i];if(i>a[i]) ma[i-a[i]]++;}for(int i=1;i<=n;i++){if(ma[a[i]+i]) ans+=ma[a[i]+i];}cout<<ans;return 0;
} 
  相关解决方案