D. Same Differences

D. Same Differences

D.相同的差异

time limit per test: 2 seconds
每次测试的时间限制:2秒

memory limit per test 256 megabytes
每测试256兆字节的内存限制

input. standard input
投入。标准输入

output: standard output
产出:标准产出

You are given an array a of n integers. Count the number of pairs of indices (i,j) such thati < jandaj-ai=j-i.
给你一个n个整数数组a。计算指数(i,j)的对数,使i<jandaj-ai=j-i。

Input
输入

The first line contains one integert(1≤t < 10*). Then t test cases follow.
第一行包含一个整数(1≤t<10*)。然后是测试用例。

The first line of each test case contains one integern(1 < n< 2. 105).
每个测试用例的第一行包含一个整数(1<n<2.105)。

The second line of each test case contains n integers a1,a2,…,an(1 <ai≤n)- array a.
每个测试用例的第二行包含n个整数A1、a2、.、(1<ai≤n)-数组a。

It is guaranteed that the sum of n over all test cases does not exceed 2.105.
保证所有测试用例上n的和不超过2.105。

Output
输出量

For each test case output the number of pairs of indices (i,j) suchthati< jandaj-ai=j-i.
对于每个测试用例输出的索引对数(i,j)都是i<jandaj-ai=j-i。

变为a[i]-i=a[j]-j

int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        for(int i=1 ; i<=n ; ++i)
            cin>>a[i];
        map<int,int>mp;
        ll ans = 0;
        mp.clear();
        for (int i=1;i<=n;++i)
        {
            if (mp.count(a[i]-i))
                ans += mp[a[i]-i];
            mp[a[i]-i]++;
        }
        cout << ans << endl;
    }
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值