Loading

Codeforces 627 E. Sleeping Schedule(线性DP)

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 ai1ai−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 (1n2000,3h2000,0lr<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 (1ai<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 a11a1−1 hours, now the time is 1515 . This time is not good. Then Vova goes to sleep after a21a2−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 a41a4−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.

这题是道不错的DP(因为我做出来了2333)

很明显就能看出来是DP题,dp数组的含义很明确:dp[i][j]表示第i次的j时刻准备入睡时的好的睡眠次数。首先明确一点,睡觉的话一睡一整天,所以对实际的时间可以认为是没有一影响的。考虑转移方程:j时刻已经准备入睡了,所以j-a[i]或j-(a[i]-1)表示上一次准备入睡的时刻。这么写肯定不行,因为会减出负数,所以要+h再%h,就能保证限制在0~h-1内了。到这里要注意:能从任意一个(j-a[i]+h)%h或者(j-(a[i]-1)+h)%h转移过来吗?显然不行,只能从上一阶段特定的那几个状态转移过来,所以在这里要加入合法性判断(我用的二维数组,实际上没必要),同时在本阶段更新完后也要把本阶段更新的状态标记为合法,不这样写的话最后的答案会变大。别忘了j如果在l~r范围内dp[i][j]要++。

#include <bits/stdc++.h>
#define N 2005
using namespace std;
int n,h,l,r;
int a[N];
int dp[N][N]={0};
int valid[N][N]={0}; 
int main()
{
    cin>>n>>h>>l>>r;
    int i,j;
    valid[0][0]=1;
    for(i=1;i<=n;i++)scanf("%d",&a[i]);
    for(i=1;i<=n;i++)
    {
        for(j=0;j<h;j++)
        {
            if(i==1&&j<a[1]-1)continue;
            //dp[i][j]=max(dp[i-1][(j-a[i]+h)%h],dp[i-1][(j-a[i]+1+h)%h]);
            //if(j>=l&&j<=r)dp[i][j]++;这么是错误的 必须得能正确转移才能累加 否则的话会加多了  
            if(valid[i-1][(j-a[i]+h)%h])//合法性 
            {
                dp[i][j]=dp[i-1][(j-a[i]+h)%h]+(j>=l&&j<=r?1:0);//一共有两种转移的情况 
                valid[i][j]=1;//记得标记为合法 
            }
            if(valid[i-1][(j-a[i]+1+h)%h])
            {
                dp[i][j]=max(dp[i][j],dp[i-1][(j-a[i]+1+h)%h]+(j>=l&&j<=r?1:0));
                valid[i][j]=1;
            }
        }    
    }
    int ans=0;
    for(i=0;i<=h;i++)ans=max(ans,dp[n][i]);
    cout<<ans;
    return 0;
}

 

posted @ 2020-03-29 00:54  脂环  阅读(198)  评论(0编辑  收藏  举报