CF1520D Same Differences

题目描述

You are given an array aa of nn integers. Count the number of pairs of indices (i, j)(i,j) such that i < ji<j and a_j - a_i = j - iaj​−ai​=j−i .

输入格式

The first line contains one integer tt ( 1 \le t \le 10^41≤t≤104 ). Then tt test cases follow.

The first line of each test case contains one integer nn ( 1 \le n \le 2 \cdot 10^51≤n≤2⋅105 ).

The second line of each test case contains nn integers a_1, a_2, \ldots, a_na1​,a2​,…,an​ ( 1 \le a_i \le n1≤ai​≤n ) — array aa .

It is guaranteed that the sum of nn over all test cases does not exceed 2 \cdot 10^52⋅105 .

输出格式

For each test case output the number of pairs of indices (i, j)(i,j) such that i < ji<j and a_j - a_i = j - iaj​−ai​=j−i .

题意翻译

给出 TT 组序列 aa,长度为 NN,询问有多少对 (i,j)(i,j) 满足 i < ji<j 且 a_j - a_i = j - iaj​−ai​=j−i。

输入输出样例

输入 #1复制

4
6
3 5 1 4 6 6
3
1 2 3
4
1 3 3 4
6
1 6 3 4 5 6

输出 #1复制

1
3
3

思路:aj-ai==j-i,推出aj-j==ai-i.运用map

​
#include<iostream>
#include<map> 
using namespace std;
int main()
{
	long long int t;
	scanf("%lld",&t);
	while(t--)
	{
		long long int n;
		scanf("%lld",&n);
		map<long long int,long long int>mp;
		long long int sum=0;
		for(int i=1;i<=n;i++)
		{
			int x;
			cin>>x;
			sum+=mp[x-i];//第一次为0;第二次出现sum加一表示一对 
			mp[x-i]++; 
		}
		cout<<sum<<endl;
	}
	return 0;
}

​

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值