前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Codeforces Round #627 (Div. 3) E. Sleeping Schedule (DP)

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

作者头像
glm233
发布2020-09-28 17:42:23
4830
发布2020-09-28 17:42:23
举报
文章被收录于专栏:glm的全栈学习之路

E. Sleeping Schedule

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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

Copy

代码语言:javascript
复制
7 24 21 23
16 17 14 20 20 11 22

output

Copy

代码语言:javascript
复制
3

Note

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

思路:dp,dp[i][j]表示第i次睡觉时,当前时间是j的最大答案,转移方程:

dp[i][(j+a[i])%h]=dp[i-1][j]+((j+a[i])%h>=l&&(j+a[i])%h<=r));

dp[i][(j+a[i]-1)%h]=dp[i-1][j]+((j+a[i]-1)%h>=l&&(j+a[i]-1)%h<=r));

最后在dp[n][0~h-1]之间取最大就好,注意初始化全部为-1表示不能到达的状态,dp[0][0]=0

代码语言:javascript
复制
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define rg register ll
#define inf 2147483647
#define lb(x) (x&(-x))
ll sz[200005],n;
template <typename T> inline void read(T& x)
{
    x=0;char ch=getchar();ll f=1;
    while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
    while(isdigit(ch)){x=(x<<3)+(x<<1)+(ch^48);ch=getchar();}x*=f;
}
inline ll query(ll x){ll res=0;while(x){res+=sz[x];x-=lb(x);}return res;}
inline void add(ll x,ll val){while(x<=n){sz[x]+=val;x+=lb(x);}}//第x个加上val
ll h,l,r,a[2005],dp[2005][2005];
int main()
{
	cin>>n>>h>>l>>r;
	for(rg i=1;i<=n;i++)cin>>a[i];
	memset(dp,-1,sizeof(dp));
	dp[0][0]=0;
	ll maxx=-1;
	for(rg i=1;i<=n;i++)
	{
		for(rg j=0;j<h;j++)
		{
			if(dp[i-1][j]>=0)
			{
				ll k=(j+a[i])%h;
				dp[i][k]=max(dp[i-1][j]+(l<=k&&r>=k),dp[i][k]);
				k=(j+a[i]-1)%h;
				dp[i][k]=max(dp[i-1][j]+(l<=k&&r>=k),dp[i][k]);
			}
		}
	}
	for(rg i=0;i<h;i++)maxx=max(dp[n][i],maxx);
	cout<<maxx<<endl;
    return 0;
    
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020/03/14 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档