Codeforces Round #627 (Div. 3)-E. Sleeping Schedule

整理的算法模板:ACM算法模板总结(分类详细版)

链接E. Sleeping Schedule

E. Sleeping Schedule

Vova had a pretty weird sleeping schedule. There are hh hours in a day. Vova will sleep exactly nn times. The ii-th time he will sleep exactly after aiai hours from the time he woke up. You can assume that Vova woke up exactly at the beginning of this story (the initial time is 00). Each time Vova sleeps exactly one day (in other words, hh hours).

Vova thinks that the ii-th sleeping time is good if he starts to sleep between hours ll and rr inclusive.

Vova can control himself and before the ii-th time can choose between two options: go to sleep after aiai hours or after ai−1ai−1 hours.

Your task is to say the maximum number of good sleeping times Vova can obtain if he acts optimally.

Input

The first line of the input contains four integers n,h,ln,h,l and rr (1≤n≤2000,3≤h≤2000,0≤l≤r<h1≤n≤2000,3≤h≤2000,0≤l≤r<h) — the number of times Vova goes to sleep, the number of hours in a day and the segment of the good sleeping time.

The second line of the input contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai<h1≤ai<h), where aiai is the number of hours after which Vova goes to sleep the ii-th time.

Output

Print one integer — the maximum number of good sleeping times Vova can obtain if he acts optimally.

Example

input

7 24 21 23
16 17 14 20 20 11 22

output

3

Note

The maximum number of good times in the example is 33.

The story starts from t=0t=0. Then Vova goes to sleep after a1−1a1−1 hours, now the time is 1515. This time is not good. Then Vova goes to sleep after a2−1a2−1 hours, now the time is 15+16=715+16=7. This time is also not good. Then Vova goes to sleep after a3a3 hours, now the time is 7+14=217+14=21. This time is good. Then Vova goes to sleep after a4−1a4−1 hours, now the time is 21+19=1621+19=16. This time is not good. Then Vova goes to sleep after a5a5 hours, now the time is 16+20=1216+20=12. This time is not good. Then Vova goes to sleep after a6a6 hours, now the time is 12+11=2312+11=23. This time is good. Then Vova goes to sleep after a7a7 hours, now the time is 23+22=2123+22=21. This time is also good.

 

题意:

某个星球上一天有h小时(0,1,2,...,h−1)(h≤2000),现在有个人在某一天的0点整睡醒了。他现在要睡n(≤2000)次觉,其中第i次睡觉必定会在他醒来的ai(1≤ai≤h−1)小时之后发生。他每次都正好睡h小时醒来。

给定一对l,r(0≤l<r≤h−1),他觉得如果一次睡觉在 l,l+1,...,r这几个时刻中的某一个时刻开始,那这次睡觉比较好。

现在他成为了大脑升级人,可以用大脑控制自己的清醒时间了,使得自己正好提早一小时睡觉(即选择一些数把它们减去1)。问他最多能得到多少次好的睡觉。

思路:

dp思想;状态集合 f ( i, j ) 表示第i次睡醒的时刻是 j ;状态属性为当第i次睡醒的时刻是为 j 时 所睡的好觉的最大值;

状态计算:f (i , j) = max( f( i-1, (j - a[i] +h)%h ) , f( i-1,(j - a[i] +1+h) %h) + check(j);

注意临界值判断即可;

答案为:(不会打符号的弱鸡qaq....)

 

#include<bits/stdc++.h>
using namespace std;
const int N=2020;
int a[N],dp[N][N];
int n,h,l,r;
int good(int x)
{
	if(x>=l&&x<=r) return 1;
	return 0;
}
int main()
{
	memset(dp,-1,sizeof dp);
	cin >>n>>h>>l>>r;
	for(int i=1;i<=n;i++) cin >>a[i];
	dp[0][0]=0;
	for(int i=1;i<=n;i++)
	{
		for(int j=0;j<h;j++)
		{
			if(dp[i-1][(j-a[i]+h)%h]<0&&dp[i-1][(j-a[i]+1+h)%h]<0) continue;
			dp[i][j]=max(dp[i-1][(j-a[i]+h)%h],dp[i-1][(j-a[i]+1+h)%h])+good(j);
		}
	}
	int ans=0;
	for(int i=n,j=0;j<h;j++) ans=max(ans,dp[i][j]);
	cout <<ans<<endl;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值