做题打卡第36天

昨天的竞赛竟然是三个小时!!
我以为是两个点

算了
(以上当我没说)
不狡辩了
就是菜qaq
今天继续总结比赛没做上的题
Polycarp found under the Christmas tree an array a of n elements and instructions for playing with it:

At first, choose index i (1≤i≤n) — starting position in the array. Put the chip at the index i (on the value ai).
While i≤n, add ai to your score and move the chip ai positions to the right (i.e. replace i with i+ai).
If i>n, then Polycarp ends the game.
For example, if n=5 and a=[7,3,1,2,3], then the following game options are possible:

Polycarp chooses i=1. Game process: i=1⟶+78. The score of the game is: a1=7.
Polycarp chooses i=2. Game process: i=2⟶+35⟶+38. The score of the game is: a2+a5=6.
Polycarp chooses i=3. Game process: i=3⟶+14⟶+26. The score of the game is: a3+a4=3.
Polycarp chooses i=4. Game process: i=4⟶+26. The score of the game is: a4=2.
Polycarp chooses i=5. Game process: i=5⟶+38. The score of the game is: a5=3.
Help Polycarp to find out the maximum score he can get if he chooses the starting index in an optimal way.

Input
The first line contains one integer t (1≤t≤104) — the number of test cases. Then t test cases follow.

The first line of each test case contains one integer n (1≤n≤2⋅105) — the length of the array a.

The next line contains n integers a1,a2,…,an (1≤ai≤109) — elements of the array a.

It is guaranteed that the sum of n over all test cases does not exceed 2⋅105.
原题链接:https://codeforces.com/problemset/problem/1472/C
其实就是个递推
想麻烦了

下面是答案:

#include<bits/stdc++.h>

using namespace std;
const int N = 2e5 + 10;
typedef long long ll;

ll t, n;
ll a[N];
ll dp[N];

int main() {

    cin >> t;
    while (t--) {

        cin >> n;
        for (int i = 1; i <= n; i++) scanf("%lld", &a[i]);
        ll maxn = 0;
        for (int i = n; i > 0; i--) {
            if(i+a[i]>n){
                dp[i]=a[i];
            }else{
                dp[i]=a[i]+dp[a[i]+i];
            }
            maxn=max(dp[i],maxn);
        }
        cout << maxn << endl;
    }


    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值