--- title: "选取石子" created: 2025-11-28 tags: - 算法 --- # 选取石子 ## 题目 [选取石子](https://www.acwing.com/problem/content/description/3774/) ![[image-3944602a.png]] ## 思路分析 也是逆想思考 $i-j =a\_i-a\_j$转变成$a\_i-i=a\_j-j$ 直接处理查询不方便 先把所有的x-i的值存在哈希表里 相同的就累加起来 最后查询就方便了 ## 代码实现 ```cpp #include using namespace std; typedef long long LL; unordered_map 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<