-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSleepingSchedule.cpp
More file actions
executable file
·138 lines (103 loc) · 4.1 KB
/
SleepingSchedule.cpp
File metadata and controls
executable file
·138 lines (103 loc) · 4.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
Code Forces #627 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 0).
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 ai−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≤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 a1,a2,…,an (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
7 24 21 23
16 17 14 20 20 11 22
output
3
Note
The maximum number of good times in the example is 3.
The story starts from t=0.
Then Vova goes to sleep after a1−1 hours, now the time is 15. This time is not good.
Then Vova goes to sleep after a2−1 hours, now the time is 15+16=7. This time is also not good.
Then Vova goes to sleep after a3 hours, now the time is 7+14=21. This time is good.
Then Vova goes to sleep after a4−1 hours, now the time is 21+19=16. This time is not good.
Then Vova goes to sleep after a5 hours, now the time is 16+20=12. This time is not good.
Then Vova goes to sleep after a6 hours, now the time is 12+11=23. This time is good.
Then Vova goes to sleep after a7 hours, now the time is 23+22=21. This time is also good.
*/
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
#define ff first
#define ss second
#define int long long
#define pb push_back
#define mp make_pair
#define pii pair<int,int>
#define vi vector<int>
#define mii map<int,int>
#define pqb priority_queue<int>
#define pqs priority_queue<int,vi,greater<int> >
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define mod 1000000007
#define inf 1e18
#define ps(x,y) fixed<<setprecision(y)<<x
#define mk(arr,n,type) type *arr=new type[n];
#define w(x) int x; cin>>x; while(x--)
#define pw(b,p) pow(b,p) + 0.1
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;
void c_p_c()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
}
int dp[2001][2001];
int a[2001];
int32_t main()
{
c_p_c();
int n, h, l, r;
cin >> n >> h >> l >> r;
dp[0][0] = 0;
for (int i = 1; i < h; ++i)
dp[0][i] = -1;
for (int i = 1; i <= n; ++i)
cin >> a[i];
for (int i = 0; i < n; ++i){
for (int hr = 0; hr < h; ++hr){
dp[i + 1][hr] = -1;
}
for (int hr = 0; hr < h; ++hr){
if (dp[i][hr] == -1)
continue;
int h1 = (hr + a[i + 1] - 1 + h) % h;
int h2 = (hr + a[i + 1]) % h;
dp[i + 1][h1] = max(dp[i + 1][h1], dp[i][hr] + (l <= h1 && h1 <= r));
dp[i + 1][h2] = max(dp[i + 1][h2], dp[i][hr] + (l <= h2 && h2 <= r));
}
}
int ans = 0;
for (int i = 0; i < h; ++i)
if (dp[n][i] != -1)
ans = max(ans, dp[n][i]);
cout << ans << '\n';
return 0;
}