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

时间:2022-07-26
本文章向大家介绍Codeforces Round #627 (Div. 3) E. Sleeping Schedule (DP),主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

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

7 24 21 23
16 17 14 20 20 11 22

output

Copy

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

#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;
    
}