题解 | 谐距下标对

谐距下标对

https://www.nowcoder.com/practice/12fd032361704c978bcb9c2c2b3bb93d

统计a[i]-i的数量,利用组合数计算谐距下标对数

#include <iostream>
#include<vector>
#include<unordered_map>
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n;
    cin>>n;
    vector<int>a(n);
    unordered_map<int,int>cnt;
    for(int i=0;i<n;i++){
        cin>>a[i];
        cnt[a[i]-i]++;
    }
    long long res=0;
    for(const auto&p:cnt){
        int c=p.second;
        res+=(long long)c*(c-1)/2;
    }
    cout<<res<<endl;
    return 0;
}

全部评论

相关推荐

程序员牛肉:可以说含金量不如王者荣耀省标。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务