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

链接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

扫描二维码关注公众号,回复: 9823785 查看本文章

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;
}
发布了217 篇原创文章 · 获赞 49 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/weixin_43872728/article/details/104837496