链接:https://codeforces.ml/contest/1324/problem/E
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 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.
代码:
#include<bits/stdc++.h>
using namespace std;
long long s,n,h,l,r,x,u,v,max1=0;
long long a[2001],b[2001];
long long dp[2001][2001];
int main()
{
cin>>n>>h>>l>>r;
s=0;
b[0]=0;
for(int i=1;i<=n;i++)
{
cin>>a[i];
b[i]=b[i-1]+a[i];
//b[i]%=h;
}
for(int i=1;i<=n;i++)
{
for(int j=0;j<=i;j++)
{
if((b[i]-j)%h>=l&&(b[i]-j)%h<=r)
dp[i][j]=max(dp[i-1][j],dp[i-1][j-1])+1;
else
dp[i][j]=max(dp[i-1][j],dp[i-1][j-1]);
}
}
for(int i=0;i<=n;i++)
max1=max(max1,dp[n][i]);
cout<<max1;
}