​This Message Will Self-Destruct in 5s Editorial​

Problem Statement

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 NN attendees in the party, and they are given attendee numbers from 11 through NN. The height of Attendee ii is A_iAi​.

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 \frac{N(N-1)}{2}2N(N−1)​ ways to choose two from the NN attendees and make a pair. Among them, how many satisfy the condition above?

P.S.: We cannot let you know the secret.

Constraints

  • All values in input are integers.
  • 2 \leq N \leq 2 \times 10^52≤N≤2×105
  • 1 \leq A_i \leq 10^9\ (1 \leq i \leq N)1≤Ai​≤109 (1≤i≤N)

Input

Input is given from Standard Input in the following format:

NN
A_1A1​ A_2A2​ \dots… A_NAN​

Output

Print the number of pairs satisfying the condition.


Sample Input 1 Copy

Copy

6
2 3 3 1 3 1

Sample Output 1 Copy

Copy

3
  • A_1 + A_4 = 3A1​+A4​=3, so the pair of Attendee 11 and 44 satisfy the condition.
  • A_2 + A_6 = 4A2​+A6​=4, so the pair of Attendee 22 and 66 satisfy the condition.
  • A_4 + A_6 = 2A4​+A6​=2, so the pair of Attendee 44 and 66 satisfy the condition.

No other pair satisfies the condition, so you should print 33.

 一道atcoder的题目,也确实是以前没有想过的思路,做个记录。

题目需要的就是下标差绝对值和高度之和相等的配对数目

思路

用h数组存储高度,那么表达式也就是 abs(i-j)==h[i]+h[j],假设 i>j (无所谓的),那么去掉绝对值也就是 i - j == h[i] +h[j] ,移项得到 i-h[i]==j+h[j],所以只需要找到这样的i和j的数量。

引入map<>存储满足和i匹配的j+h[j]的j的数量,可以看到对于任意一组i,j,在扫描到i时, j 是匹配的,在扫描到 j 时,i 一定也是满足式子的,所以可以把计算分为两个阶段,扫描 i 时标记这一对,扫描到 j 时将这一对收集到答案中,体现到代码中其实就是先ans+然后再mp++的问题。而答案就是由这样的一组组 i j 组成,可解。

代码还是很短的

#include<stdio.h>
#include<map>
#define ll long long
using namespace std;
ll h[1000000];
map<ll, ll>mp;
int main()
{
	int n;
	scanf("%d", &n);
	for (int i = 1; i <= n; i++)
		scanf("%lld", h + i);
	ll ans = 0;
	for (int i = 1; i <= n; i++) {
		ans += mp[i - h[i]];
		mp[i + h[i]]++;
	}
	printf("%lld", ans);
	return 0;
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值