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

https://codeforces.com/contest/1472/problem/C

题意:这个题是从i=1开始加上a[i],看看会跳到哪个格子,然后跳转到a[i+a[i]]的位置,求能产生的在数组范围内的最大的结果。

但是正常做从头开始会t掉,于是我们倒过来求,让最后的格子加上本身的位置如果大于n舍弃,不大于n就替换掉原有数组中a[i]的值

在这些值中找最大即可。

 1 //
 2 // Created by w on 2021/1/4.
 3 //
 4 
 5 #include <iostream>
 6 using namespace  std;
 7 const int maxx=2e5+10;
 8 int a[maxx],b[maxx];
 9 typedef  long long ll ;
10 int main()
11 {
12     int t;
13     cin>>t;
14     while (t--)
15     {
16         ll max_1=-1;
17         ll n;
18         cin>>n;
19         for(int i=1;i<=n;i++)
20         cin>>a[i];
21         for(int i=n;i>0;i--)
22         {
23             if(a[i]+i<=n)
24                 a[i]+=a[i+a[i]];
25             if (a[i]>max_1)
26                 max_1=a[i];
27         }
28         cout<<max_1<<endl;
29     }
30     return 0;
31 }

 

posted @ 2021-01-06 21:20  BlackSnow  阅读(159)  评论(0)    收藏  举报