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

本文介绍了一个编程挑战,要求找到Polycarp玩数组游戏时的最大得分。玩家从数组中选择一个起始位置,根据数组元素移动并累加得分,直到超出数组范围。通过逆向遍历数组,确定最佳起始位置,以获得最高总分。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.

Output
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.

Example
inputCopy
4
5
7 3 1 2 3
3
2 1 4
6
2 1000 2 3 995 1
5
1 1 1 1 1
outputCopy
7
6
1000
5
Note
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.
将数组倒着处理,看当前数组位置上的数能加几次大于n

#include<map>
#include<stack>
#include<queue>
#include<string>
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#define ls (k<<1)
#define rs (k<<1|1)
#define pb push_back
#define mid ((l+r)>>1)
#include<bits/stdc++.h>
using namespace std;
const int p=1e4+7;
const int mod=1e9+7;
const int maxn=1e6+1;
typedef long long ll;
const int inf=0x3f3f3f3f;
int a[maxn],b[maxn];
void solve(){
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        for(int i=1;i<=2*n;i++)
            a[i]=0;
        for(int i=1;i<=n;i++){
            cin>>a[i];
        }
        int maxx=0;
        for(int i=n;i>=1;i--){
            if(a[i]+i<=n)
                a[i]=a[a[i]+i]+a[i];
            maxx=max(a[i],maxx);
        }
        cout<<maxx<<endl;
    }
}
 
signed main()
{
    ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
	solve();
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值