题目描述
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; }
CF1520D Same Differences
最新推荐文章于 2024-08-06 10:22:43 发布