E know the game when the title is a DP. . . But did not make it. . .
Every DP do question the biggest problem is not knowing how to set up the array to represent a
DP is provided a [i] [j]
denotes the greatest benefit when the i-th value of j sleep period
then
if (j + a [i]) % h ~ R & lt between L
DP [i] [(j + a [i])% h ] = max (dp [i] [(j + a [i])% h], d [i-1] [j] + 1)
if not
dp [i] [(j + a [i])% h ] = max% h (d [i] [(j + a [i])], 0, d [i - 1] [j])
Code:
#include <bits/stdc++.h>
#define ll long long
using namespace std;
int a[2005],inf=99999999;
int d[2005][2005];
void init()
{
for(int i=0;i<2002;i++)
{
for(int j=0;j<2002;j++)
{d[i][j]=-1;}
}
}
int main()
{
int n,h,l,r;
cin>>n>>h>>l>>r;
for(int i=1;i<=n;i++)
{cin>>a[i];}
init();//初始化f数组
if(a[1]>=l&&a[1]<=r)
{d[1][a[1]]=1;}
else
{d[1][a[1]]=0;}
if(a[1]-1>=l&&a[1]-1<=r)
{d[1][a[1]-1]=1;}
else
{d[1][a[1]-1]=0;}
for(int i=2;i<=n;i++)
{
for(int j=0;j<=h;j++)
{
int op=a[i];
if(d[i-1][j]!=-1)
{
int u=(j+op)%h;
if(u>=l&&u<=r)
{d[i][u]=max(d[i][u],d[i-1][j]+1);}
else
{d[i][u]=max(d[i][u],0);
d[i][u]=max(d[i][u],d[i-1][j]);}
op--;
u=(j+op)%h;
if(u>=l&&u<=r)
{d[i][u]=max(d[i][u],d[i-1][j]+1);}
else
{d[i][u]=max(d[i][u],0);
d[i][u]=max(d[i][u],d[i-1][j]);}
}
}
}
int maxx=-1;
for(int j=0;j<=h;j++)
{maxx=max(maxx,d[n][j]);}
cout<<maxx<<endl;
return 0;
}