Codeforces Round 693 (Div. 3) C. Long Jumps(DP)

Long Jumps

题面翻译

P o l y c a r p Polycarp Polycarp在玩一个游戏,给定有 n n n个元素的数组 a a a,设定一个起始点 i i i。当 i < = n i<=n i<=n时, i = i + a [ i ] i=i+a[i] i=i+a[i],得分 s c o r e = s c o r e + a [ i ] score = score + a[i] score=score+a[i],试求出对于数组 a a a可以得出的最大得分。

题目描述

Polycarp found under the Christmas tree an array $ a $ of $ n $ elements and instructions for playing with it:

  • At first, choose index $ i $ ( $ 1 \leq i \leq n $ ) — starting position in the array. Put the chip at the index $ i $ (on the value $ a_i $ ).
  • While $ i \leq n $ , add $ a_i $ to your score and move the chip $ a_i $ positions to the right (i.e. replace $ i $ with $ i + a_i $ ).
  • 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 \overset{+7}{\longrightarrow} 8 $ . The score of the game is: $ a_1 = 7 $ .
  • Polycarp chooses $ i = 2 $ . Game process: $ i = 2 \overset{+3}{\longrightarrow} 5 \overset{+3}{\longrightarrow} 8 $ . The score of the game is: $ a_2 + a_5 = 6 $ .
  • Polycarp chooses $ i = 3 $ . Game process: $ i = 3 \overset{+1}{\longrightarrow} 4 \overset{+2}{\longrightarrow} 6 $ . The score of the game is: $ a_3 + a_4 = 3 $ .
  • Polycarp chooses $ i = 4 $ . Game process: $ i = 4 \overset{+2}{\longrightarrow} 6 $ . The score of the game is: $ a_4 = 2 $ .
  • Polycarp chooses $ i = 5 $ . Game process: $ i = 5 \overset{+3}{\longrightarrow} 8 $ . The score of the game is: $ a_5 = 3 $ .

Help Polycarp to find out the maximum score he can get if he chooses the starting index in an optimal way.

输入格式

The first line contains one integer $ t $ ( $ 1 \leq t \leq 10^4 $ ) — the number of test cases. Then $ t $ test cases follow.

The first line of each test case contains one integer $ n $ ( $ 1 \leq n \leq 2 \cdot 10^5 $ ) — the length of the array $ a $ .

The next line contains $ n $ integers $ a_1, a_2, \dots, a_n $ ( $ 1 \leq a_i \leq 10^9 $ ) — elements of the array $ a $ .

It is guaranteed that the sum of $ n $ over all test cases does not exceed $ 2 \cdot 10^5 $ .

输出格式

For each test case, output on a separate line one number — the maximum score that Polycarp can get by playing the game on the corresponding array according to the instruction from the statement. Note that Polycarp chooses any starting position from $ 1 $ to $ n $ in such a way as to maximize his result.

样例 #1

样例输入 #1

4
5
7 3 1 2 3
3
2 1 4
6
2 1000 2 3 995 1
5
1 1 1 1 1

样例输出 #1

7
6
1000
5

提示

The first test case is explained in the statement.

In the second test case, the maximum score can be achieved by choosing $ i = 1 $ .

In the third test case, the maximum score can be achieved by choosing $ i = 2 $ .

In the fourth test case, the maximum score can be achieved by choosing $ i = 1 $ .


这道题怎么DP?
DP主要讲究由一个状态转化为另一个状态,如果你想以从前往后的角度思考,你就会发现,你如果想要搜出来所有的点能够得到的分数就必然得到巨大的时间复杂度,那么从另一个角度,以从后往前的角度思考。

以最后一个点为起点,一定是没得走的,因为怎么走都会超出边界,所以最后一个点的值一定就是 a [ n ] a[n] a[n]
一直往前搜,直到搜到了一个点满足i + a[i] <= n,那么这时候就可以状态转移为f[i] += f[i+a[i]]

这样就会发现,在从后往前搜的时候,不管搜到哪个点,他需要转移过来的状态都已经被计算完了,这样就符合了DP。

CODE:

#include<bits/stdc++.h>
using namespace std;
const int N = 2e5+10;

int a[N];
int f[N];
int n;
#define pii pair<int,int>

void solve(){
    cin >> n;
    for(int i = 1;i <= n;i++)cin >> a[i];
    
    int res = 0;
    for(int i = n;i;i--){
        f[i] = a[i];
        if(i + a[i] <= n){
            f[i] += f[i+a[i]];
        }
        res = max(res,f[i]);
    }

    cout << res << endl;
    
    return;
}   

int main(){
    int T;cin >> T;
    while(T--){
        solve();
    }
    return 0;
}
  • 16
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值