题意:
给你一天 h h h小时,初始时间是 0 0 0,每天可以使时间 + a i +a_i +ai或者 + a i − 1 +a_i-1 +ai−1,问最多可以让多少天的时间在 [ l , r ] [l,r] [l,r]范围内。
思路:
算是个比较简单的
d
p
dp
dp了,一直想贪心,没有转换到
d
p
dp
dp的思路,如果想到
d
p
dp
dp,那么这个题估计就直接秒了。
设
f
[
i
]
[
j
]
f[i][j]
f[i][j]表示到了第
i
i
i天,时间
m
o
d
h
\bmod h
modh为
j
j
j的情况,转移方程也比较明显了:
f
[
i
]
[
j
]
=
m
a
x
(
f
[
i
−
1
]
[
(
j
−
a
[
i
]
)
m
o
d
h
]
,
f
[
i
−
1
]
[
(
j
−
a
[
i
]
+
1
)
m
o
d
h
]
)
+
(
l
≤
j
≤
r
)
f[i][j]=max(f[i-1][(j-a[i])\bmod h],f[i-1][(j-a[i]+1)\bmod h])+(l \le j \le r)
f[i][j]=max(f[i−1][(j−a[i])modh],f[i−1][(j−a[i]+1)modh])+(l≤j≤r)
直接
n
2
n^2
n2转移就好啦。
// Problem: E. Sleeping Schedule
// Contest: Codeforces - Codeforces Round #627 (Div. 3)
// URL: https://codeforces.com/contest/1324/problem/E
// Memory Limit: 256 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)
//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")
//#pragma GCC optimize(2)
#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<map>
#include<cmath>
#include<cctype>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include<sstream>
#include<ctime>
#include<cstdlib>
#define X first
#define Y second
#define L (u<<1)
#define R (u<<1|1)
#define pb push_back
#define mk make_pair
#define Mid (tr[u].l+tr[u].r>>1)
#define Len(u) (tr[u].r-tr[u].l+1)
#define random(a,b) ((a)+rand()%((b)-(a)+1))
#define db puts("---")
using namespace std;
//void rd_cre() { freopen("d://dp//data.txt","w",stdout); srand(time(NULL)); }
//void rd_ac() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//AC.txt","w",stdout); }
//void rd_wa() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//WA.txt","w",stdout); }
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;
const int N=2010,mod=1e9+7,INF=0x3f3f3f3f;
const double eps=1e-6;
int n,h,l,r;
int a[N];
int f[N][N],st[N][N];
int check(int now) {
return now>=l&&now<=r;
}
int main()
{
// ios::sync_with_stdio(false);
// cin.tie(0);
memset(f,-63,sizeof(f));
scanf("%d%d%d%d",&n,&h,&l,&r);
for(int i=1;i<=n;i++) scanf("%d",&a[i]);
int ans=0;
f[0][0]=0;
for(int i=1;i<=n;i++) {
for(int j=0;j<h;j++) {
f[i][j]=max(f[i-1][(j-a[i]+h)%h],f[i-1][(j-a[i]+1+h)%h])+check(j);
ans=max(ans,f[i][j]);
}
}
cout<<ans<<endl;
return 0;
}
/*
*/