- 题目链接:C. Jump and Treasure
- 解题思路:dp+单调队列,读过题目很容易明白是单调队列,问题有两个,从 0 0 0开始出发,最后一步到平台因为只要到达 [ n + 1 , + ∞ ) [n+1,+\infty ) [n+1,+∞),就可以获胜,所以可以简化为只要到达 n + 1 n+1 n+1就行,切记不能与在 [ 0 , n ] [0,n] [0,n]一样按照 x x x的倍数运动,否则最后都只会得到在 ( n / x ) ∗ x + x (n/x)*x+x (n/x)∗x+x上的结果。
#include<bits/stdc++.h>
#define MOD 1000000007;
#define int long long
#define rep(i,s,t) for(int i=s;i<=t;i++)
#define repd(i,s,t,d) for(int i=s;i<=t;i+=d)
#define dep(i,s,t) for(int i=s;i>=t;i--)
#define depd(i,s,t,d) for(int i=s;i>=t;i-=d)
#define lson now<<1,l,mid
#define rson now<<1|1,mid+1,r
#define lc now<<1
#define rc now<<1|1
#define N 2000006
#define inf 0x7f7f7f7f7f7f7f7f
using namespace std;
int dp[N],a[N],ans[N],n,d;
bool b[N];
int q[N];
void solve(int x)
{
int head=1,tail=1;
dp[0]=0;
q[1]=0;
repd(i,x,n,x)
{
while(head<=tail&&q[head]<i-d)head++;
dp[i]=dp[q[head]]+a[i];
while(head<=tail&&dp[q[tail]]<=dp[i])tail--;
q[++tail]=i;
}
ans[x]=-inf;
depd(i,(n/x)*x,n+1-d,x)ans[x]=max(ans[x],dp[i]);
}
signed main()
{
int t,x;
scanf("%lld%lld%lld",&n,&t,&d);
rep(i,1,n)scanf("%lld",&a[i]);
while(t--)
{
scanf("%lld",&x);
if(x>d)
{
printf("Noob\n");
}
else
{
if(!b[x])solve(x);
b[x]=1;
printf("%lld\n",ans[x]);
}
}
}
```