E. Sleeping Schedule
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Vova had a pretty weird sleeping schedule. There are ℎh hours in a day. Vova will sleep exactly 𝑛n times. The 𝑖i-th time he will sleep exactly after 𝑎𝑖ai 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, ℎh hours).
Vova thinks that the 𝑖i-th sleeping time is good if he starts to sleep between hours 𝑙l and 𝑟r inclusive.
Vova can control himself and before the 𝑖i-th time can choose between two options: go to sleep after 𝑎𝑖ai hours or after 𝑎𝑖−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,l and 𝑟r (1≤𝑛≤2000,3≤ℎ≤2000,0≤𝑙≤𝑟<ℎ1≤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 𝑛n integers 𝑎1,𝑎2,…,𝑎𝑛a1,a2,…,an (1≤𝑎𝑖<ℎ1≤ai<h), where 𝑎𝑖ai is the number of hours after which Vova goes to sleep the 𝑖i-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 𝑡=0t=0. Then Vova goes to sleep after 𝑎1−1a1−1 hours, now the time is 1515. This time is not good. Then Vova goes to sleep after 𝑎2−1a2−1 hours, now the time is 15+16=715+16=7. This time is also not good. Then Vova goes to sleep after 𝑎3a3 hours, now the time is 7+14=217+14=21. This time is good. Then Vova goes to sleep after 𝑎4−1a4−1 hours, now the time is 21+19=1621+19=16. This time is not good. Then Vova goes to sleep after 𝑎5a5 hours, now the time is 16+20=1216+20=12. This time is not good. Then Vova goes to sleep after 𝑎6a6 hours, now the time is 12+11=2312+11=23. This time is good. Then Vova goes to sleep after 𝑎7a7 hours, now the time is 23+22=2123+22=21. This time is also good.
题意:n,h,l,r,给n个时间间隔,从0开始睡觉,一天共有h小时,每次睡ai 或者 ai-1段时间,如果在l 和 r段时间入睡 那么满意度+1,问满意度最高多少?
考虑时间的唯一性 所以 dp[i][j] 代表 第i段在第j小时睡觉的满意值
那么就有递推公式:
if((j + a[i])%h >= l && (j + a[i])%h <= r)
dp[i][(j + a[i])%h] = max(dp[i][(j + a[i])%h],dp[i - 1][j] + 1);
else dp[i][(j + a[i])%h] = max(dp[i][(j + a[i])%h],dp[i - 1][j]);
if((j + a[i] - 1)%h >= l && (j + a[i] - 1)%h <= r)
dp[i][(j + a[i] - 1)%h] = max(dp[i][(j + a[i] - 1)%h],dp[i - 1][j] + 1);
else dp[i][(j + a[i] - 1)%h] = max(dp[i][(j + a[i] - 1)%h],dp[i - 1][j]);
完整代码为:
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define ff(i,a,b) for(int i = a; i <= b; i++)
#define f(i,a,b) for(int i = a; i < b; i++)
typedef pair<int,int> P;
#define ll long long
int dp[2010][2010];
int a[2010];
int main()
{
ios::sync_with_stdio(false);
int n,h,l,r;
cin >> n >> h >> l >> r;
memset(dp,-0x3f3f3f3f,sizeof(dp));
dp[0][0] = 0;
ff(i,1,n) cin >> a[i];
ff(i,1,n) ff(j,0,h - 1)
{
if((j + a[i])%h >= l && (j + a[i])%h <= r)
dp[i][(j + a[i])%h] = max(dp[i][(j + a[i])%h],dp[i - 1][j] + 1);
else dp[i][(j + a[i])%h] = max(dp[i][(j + a[i])%h],dp[i - 1][j]);
if((j + a[i] - 1)%h >= l && (j + a[i] - 1)%h <= r)
dp[i][(j + a[i] - 1)%h] = max(dp[i][(j + a[i] - 1)%h],dp[i - 1][j] + 1);
else dp[i][(j + a[i] - 1)%h] = max(dp[i][(j + a[i] - 1)%h],dp[i - 1][j]);
}
int maxx = 0;
ff(i,0,h - 1)
maxx = max(maxx,dp[n][i]);
cout << maxx << endl;
return 0;
}
本文探讨了一个有趣的问题,即如何通过优化睡眠时间表来最大化满意度。故事围绕Vova的睡眠习惯展开,他每天醒来后会在特定时间再次入睡。目标是在允许的时间段内获得尽可能多的“好”睡眠时间。
258

被折叠的 条评论
为什么被折叠?



