CF1472C Long Jumps

动态规划,倒着扫一遍即可

code

#include <cstdio>
#include <cctype>
#include <cstring>
#include <algorithm>

const int N = 2e5 + 10;
int a[N], dp[N];
inline int read(){
    
    
    int x = 0, op = 1; char ch = getchar();
    while (!isdigit(ch)){
    
    if (ch == '-') op = -1;ch = getchar();}
    while (isdigit(ch)){
    
    x = (x<<1)+(x<<3)+(ch^48); ch = getchar();}
    return x * op;
}

inline void write(int x){
    
    
    if (x < 0) putchar('-'), x = -x;
    if (x > 9) write(x / 10);
    putchar(x % 10 + '0');
}

void solve(){
    
    
    int n = read();
    memset(dp, 0, sizeof dp);
    for (int i = 1; i <= n; ++i) {
    
    
        a[i] = read();
    }

    int res = 0;
    for (int i = n; i >= 1; i--) {
    
    
        dp[i] = a[i];
        int j = i + a[i];
        if (j <= n){
    
    
            dp[i] += dp[j];
        }
        res = std::max(res, dp[i]);
    }
    write(res), putchar('\n');
}
int main(){
    
    
    int cases = read();
    while (cases--){
    
    
        solve();
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_50070650/article/details/112660490
今日推荐