E - Sleeping Schedule
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 aiaihours 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
Input7 24 21 23 16 17 14 20 20 11 22Output3
一看到这个题就想到了dp,一开始想的是用一个一维数组,因为每天都是睡一整天,但是发现如果无法确定前一天这个点是否可以醒来,就会有问题,所以加一个维度表示现在在第几天,那么对于当前天数的当前时间a[i][j],前一天就有两个时间可以醒来,如果是这两个位置不能醒来(-1),那就继续下一个时间,如果能醒来,那就看现在入睡是不是好时间加上两个时间的最大值
x表示前一天正常睡眠
y表示前一天早一小时睡眠
if (dp[i - 1][x] == -1 && dp[i - 1][y] == -1) { continue; } else { dp[i][j] = max(dp[i - 1][x], dp[i - 1][y]) + good(j); }
感觉难点就在于睡眠时间和醒来时间是两个不同的时间
睡眠-----h小时-----醒来------a[i]小时(-1)-------睡眠
#include <iostream> #include <cstdio> #include <cstdlib> #include <string> #include <algorithm> using namespace std; int n, h, l, r; int good(int x) { if (x >= l && x <= r) { return 1; } else { return 0; } } int main() { cin >> n >> h >> l >> r; int a[2001] = { 0 }; for (int i = 1; i <= n; i++) { cin >> a[i]; } int dp[2001][2001] = { 0 }; memset(dp, -1, sizeof(dp)); dp[0][0] = 0; for (int i = 1; i <= n; i++) { for (int j = 0; j < h; j++) { int x = j - a[i]; int y = j - a[i] + 1; if (x < 0) { x += h; } if (y < 0) { y += h; } if (dp[i - 1][x] == -1 && dp[i - 1][y] == -1) { continue; } else { dp[i][j] = max(dp[i - 1][x], dp[i - 1][y]) + good(j); } } } int mmax = 0; for (int j = 0; j < h; j++) { mmax = max(mmax, dp[n][j]); } cout << mmax << endl; return 0; }

浙公网安备 33010602011771号