这是一道简单的DP题目,其中要用到RMQ问题的解决方法
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int MAXN = 50010;
int S[MAXN], X[MAXN], Y[MAXN];
int ST[MAXN][20], dp[MAXN];
int n;
//dp[i] = S[i] + max{ dp[j] } { i+X[i] <= j < i + Y[i] }
//后面一段区间最值显然属于RMQ问题
void update(int x)
{
ST[x][0] = dp[x];
for(int i = 1; x + (1<<i) - 1 <= n; ++i)
ST[x][i] = max(ST[x][i-1], ST[x+(1<<(i-1))][i-1]);
return ;
}
int query(int l, int r)
{
if(l > r) return 0;
int k = 0;
while((1<<(k+1)) <= (r - l + 1)) k++;
return max(ST[l][k], ST[r-(1<<k)+1][k]);
}
int main()
{
//freopen("aa.in", "r", stdin);
//freopen("bb.out", "w", stdout);
while(scanf("%d", &n) != EOF)
{
memset(dp, 0, sizeof(dp));
for(int i = 1; i <= n; ++i)
{
scanf("%d %d %d", &S[i], &X[i], &Y[i]);
X[i] = i + X[i];
Y[i] = min(i + Y[i] - 1, n);
}
for(int i = n; i >= 1; --i)
{
dp[i] = S[i] + query(X[i], Y[i]);
update(i);
}
printf("%d\n", dp[1]);
}
return 0;
}