选取石子

题目 选取石子

image-3944602a

思路分析

也是逆想思考

\(i-j =a_i-a_j\)转变成\(a_i-i=a_j-j\)

直接处理查询不方便

先把所有的x-i的值存在哈希表里 相同的就累加起来

最后查询就方便了

代码实现

#include<bits/stdc++.h>
using namespace std;

typedef long long LL;
unordered_map<int,LL> h;
int n;

int main()
{
    cin>>n;
    for(int i=1;i<=n;i++){
        int x;
        cin>>x;
        h[x-i]+=x;
    }
    LL res=0;
    for(auto item:h)
        res=max(res,item.second);
    cout<<res;
    return 0;
}

同类题型

视频讲解


⬅️ 进球 🏠 00-刷题理模型 ➡️ 集合询问