zoj 3381(DP+RMQ)

14 篇文章 0 订阅
Osaisen Choudai!

Time Limit: 3 Seconds       Memory Limit: 65536 KB

saisenbako (賽銭箱, offertory, or religious collection box) is situated in front of the hall of Hakurei Shrine. Hakurei Reimu (博麗霊夢), the miko of Hakurei Shrine, may ask the friends who are visting her to throw some saisen (賽銭, offerings, or coins) into it. Reimu wants as much saisen as possible. Howerver, she cannot ask for saisen too frequently, because this will make her friends refuse to offer any more. On the other hand, if she doesn't ask for saisen for a very long period, her friends will become not be willing to offer any more. Generally, at the i-th day, if she asks for saisen, then she will get si saisen, what's more, she cannot ask again for xi days (including the i-th day), and she should ask again in yi days (including the i-th day).

Given sixi and yi for n days, how much saisen can Reimu get at most in these days if she asks saisen at the first day?

Input

The are multiple cases. Each case begins with a line of integer 0 < n < 50000, then n lines, each contains 3 positive integers: si, and xi < yi. Process to the end of file.

Output

For each test cases, output an integer, the maximun amount of saisen Reimu can get, in a seperate line. It's guaranteed that the answer always fit into a 32-bit signed integer.

Sample Input
3
1 1 2
2 2 3
3 3 4
3
1 1 3
2 2 4
3 3 5
4
10 3 10
7 1 7
5 2 5
1 1 2
5
1 1 9
10 3 10
7 1 7
5 2 5
1 1 2
Sample Output
3
4
11
13
References

上海アリス幻樂団

acm_x_touhou


Author:  WU, Zejun
Source:  ACM × Touhou

Contest: ZOJ Monthly, August 2010

题目:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3381

分析:这题很简单的想到DP,也很简单的想到超时,也很简单的想到用RMQ来优化。。。

今天第一次1A啊~~~肉牛满面阿

代码:

#include<cstdio>
#define max(a,b) (a>b?a:b)
using namespace std;
const int mm=50005;
int f[mm][22],s[mm],x[mm],y[mm];
int i,j,k,n;
int get(int l,int r)
{
    if(r>n)r=n;
	if(l>r)return 0;
	int k=0;
	while(1<<(k+1)<=r-l+1)++k;
	return max(f[l][k],f[r-(1<<k)+1][k]);
}
void ST(int i)
{
	for(int j=1;i+(1<<j)-1<=n;++j)
		f[i][j]=max(f[i][j-1],f[i+(1<<(j-1))][j-1]);
}
int main()
{
    while(scanf("%d",&n)!=-1)
    {
        for(i=1;i<=n;++i)scanf("%d%d%d",&s[i],&x[i],&y[i]);
        for(i=n;i>0;--i)
            f[i][0]=s[i]+get(i+x[i],i+y[i]-1),ST(i);
        printf("%d\n",f[1][0]);
    }
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值