/*
JOI 2022 Problem 2
Solution:
First, set A_i = max(A_i, B_i) to avoid edge cases.
Then, binary search on the answer (makes the problem easier).
Start with Bitaro attending every class.
Then, for each course in which he overprepares, calculate the # of classes that can be skipped.
See if those missed courses (in total) can make up the ones he underprepares for.
*/
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define sz(x) ((int) x.size())
#define all(x) x.begin(), x.end()
#define debug if (true) cerr
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
bool check(int N, ll M, vi& A, vi& B, ll K) {
vector<ll> prepLeft(N, K);
// Initially, go to all the classes
rep(i, 0, N) prepLeft[i] -= M * A[i];
// Count number of free classes (to be reassigned)
// Also count number of needed self study periods
ll numFree = 0;
ll numSelf = 0;
rep(i, 0, N) {
if (prepLeft[i] <= -A[i]) {
// Overprepared
numFree += min(-prepLeft[i] / A[i], M);
} else if (prepLeft[i] > 0) {
// Underprepared
numSelf += (prepLeft[i]+B[i]-1) / B[i];
if (numSelf >= 1e18) return false;
}
}
// Possible if enough free periods exist
return numFree >= numSelf;
}
void solve() {
int N; cin >> N;
ll M; cin >> M;
vi A(N), B(N);
rep(i, 0, N) cin >> A[i];
rep(i, 0, N) {
cin >> B[i];
A[i] = max(A[i], B[i]);
}
// Binary search on the answer
ll ansLow = 0, ansHigh = 2e18;
while (ansLow < ansHigh) {
ll mid = (ansLow+ansHigh+1) / 2;
if (check(N, M, A, B, mid)) ansLow = mid;
else ansHigh = mid - 1;
}
ll ans = ansLow;
cout << ans << '\n';
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin.exceptions(cin.failbit);
solve();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
3 ms |
340 KB |
Output is correct |
11 |
Correct |
148 ms |
4972 KB |
Output is correct |
12 |
Correct |
144 ms |
4940 KB |
Output is correct |
13 |
Correct |
159 ms |
4976 KB |
Output is correct |
14 |
Correct |
140 ms |
5012 KB |
Output is correct |
15 |
Correct |
1 ms |
324 KB |
Output is correct |
16 |
Correct |
147 ms |
6200 KB |
Output is correct |
17 |
Correct |
184 ms |
11460 KB |
Output is correct |
18 |
Correct |
150 ms |
10808 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
324 KB |
Output is correct |
2 |
Correct |
147 ms |
6200 KB |
Output is correct |
3 |
Correct |
184 ms |
11460 KB |
Output is correct |
4 |
Correct |
150 ms |
10808 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
90 ms |
2644 KB |
Output is correct |
14 |
Correct |
61 ms |
1880 KB |
Output is correct |
15 |
Correct |
46 ms |
1492 KB |
Output is correct |
16 |
Correct |
41 ms |
1256 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
3 ms |
340 KB |
Output is correct |
23 |
Correct |
3 ms |
452 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
3 ms |
340 KB |
Output is correct |
11 |
Correct |
148 ms |
4972 KB |
Output is correct |
12 |
Correct |
144 ms |
4940 KB |
Output is correct |
13 |
Correct |
159 ms |
4976 KB |
Output is correct |
14 |
Correct |
140 ms |
5012 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
0 ms |
212 KB |
Output is correct |
23 |
Correct |
90 ms |
2644 KB |
Output is correct |
24 |
Correct |
61 ms |
1880 KB |
Output is correct |
25 |
Correct |
46 ms |
1492 KB |
Output is correct |
26 |
Correct |
41 ms |
1256 KB |
Output is correct |
27 |
Correct |
1 ms |
212 KB |
Output is correct |
28 |
Correct |
0 ms |
212 KB |
Output is correct |
29 |
Correct |
0 ms |
212 KB |
Output is correct |
30 |
Correct |
1 ms |
212 KB |
Output is correct |
31 |
Correct |
0 ms |
212 KB |
Output is correct |
32 |
Correct |
3 ms |
340 KB |
Output is correct |
33 |
Correct |
3 ms |
452 KB |
Output is correct |
34 |
Correct |
1 ms |
324 KB |
Output is correct |
35 |
Correct |
147 ms |
6200 KB |
Output is correct |
36 |
Correct |
184 ms |
11460 KB |
Output is correct |
37 |
Correct |
150 ms |
10808 KB |
Output is correct |
38 |
Correct |
1 ms |
212 KB |
Output is correct |
39 |
Correct |
1 ms |
320 KB |
Output is correct |
40 |
Correct |
0 ms |
212 KB |
Output is correct |
41 |
Correct |
0 ms |
212 KB |
Output is correct |
42 |
Correct |
0 ms |
212 KB |
Output is correct |
43 |
Correct |
1 ms |
212 KB |
Output is correct |
44 |
Correct |
1 ms |
212 KB |
Output is correct |
45 |
Correct |
4 ms |
468 KB |
Output is correct |
46 |
Correct |
6 ms |
596 KB |
Output is correct |
47 |
Correct |
1 ms |
340 KB |
Output is correct |
48 |
Correct |
32 ms |
2128 KB |
Output is correct |
49 |
Correct |
1 ms |
212 KB |
Output is correct |
50 |
Correct |
1 ms |
212 KB |
Output is correct |
51 |
Correct |
5 ms |
448 KB |
Output is correct |
52 |
Correct |
1 ms |
212 KB |
Output is correct |
53 |
Correct |
0 ms |
212 KB |
Output is correct |
54 |
Correct |
1 ms |
212 KB |
Output is correct |
55 |
Correct |
93 ms |
5512 KB |
Output is correct |
56 |
Correct |
56 ms |
3148 KB |
Output is correct |
57 |
Correct |
49 ms |
3152 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
90 ms |
2644 KB |
Output is correct |
10 |
Correct |
61 ms |
1880 KB |
Output is correct |
11 |
Correct |
46 ms |
1492 KB |
Output is correct |
12 |
Correct |
41 ms |
1256 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
3 ms |
340 KB |
Output is correct |
19 |
Correct |
3 ms |
452 KB |
Output is correct |
20 |
Correct |
1 ms |
324 KB |
Output is correct |
21 |
Correct |
147 ms |
6200 KB |
Output is correct |
22 |
Correct |
184 ms |
11460 KB |
Output is correct |
23 |
Correct |
150 ms |
10808 KB |
Output is correct |
24 |
Correct |
37 ms |
1896 KB |
Output is correct |
25 |
Correct |
94 ms |
5036 KB |
Output is correct |
26 |
Correct |
21 ms |
1324 KB |
Output is correct |
27 |
Correct |
185 ms |
10916 KB |
Output is correct |
28 |
Correct |
183 ms |
10800 KB |
Output is correct |
29 |
Correct |
185 ms |
10796 KB |
Output is correct |
30 |
Correct |
204 ms |
10800 KB |
Output is correct |
31 |
Correct |
244 ms |
10804 KB |
Output is correct |
32 |
Correct |
145 ms |
6188 KB |
Output is correct |
33 |
Correct |
199 ms |
11456 KB |
Output is correct |
34 |
Correct |
167 ms |
6184 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
3 ms |
340 KB |
Output is correct |
11 |
Correct |
148 ms |
4972 KB |
Output is correct |
12 |
Correct |
144 ms |
4940 KB |
Output is correct |
13 |
Correct |
159 ms |
4976 KB |
Output is correct |
14 |
Correct |
140 ms |
5012 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
0 ms |
212 KB |
Output is correct |
23 |
Correct |
90 ms |
2644 KB |
Output is correct |
24 |
Correct |
61 ms |
1880 KB |
Output is correct |
25 |
Correct |
46 ms |
1492 KB |
Output is correct |
26 |
Correct |
41 ms |
1256 KB |
Output is correct |
27 |
Correct |
1 ms |
212 KB |
Output is correct |
28 |
Correct |
0 ms |
212 KB |
Output is correct |
29 |
Correct |
0 ms |
212 KB |
Output is correct |
30 |
Correct |
1 ms |
212 KB |
Output is correct |
31 |
Correct |
0 ms |
212 KB |
Output is correct |
32 |
Correct |
3 ms |
340 KB |
Output is correct |
33 |
Correct |
3 ms |
452 KB |
Output is correct |
34 |
Correct |
1 ms |
212 KB |
Output is correct |
35 |
Correct |
1 ms |
320 KB |
Output is correct |
36 |
Correct |
0 ms |
212 KB |
Output is correct |
37 |
Correct |
0 ms |
212 KB |
Output is correct |
38 |
Correct |
0 ms |
212 KB |
Output is correct |
39 |
Correct |
1 ms |
212 KB |
Output is correct |
40 |
Correct |
1 ms |
212 KB |
Output is correct |
41 |
Correct |
4 ms |
468 KB |
Output is correct |
42 |
Correct |
6 ms |
596 KB |
Output is correct |
43 |
Correct |
1 ms |
340 KB |
Output is correct |
44 |
Correct |
32 ms |
2128 KB |
Output is correct |
45 |
Correct |
1 ms |
212 KB |
Output is correct |
46 |
Correct |
1 ms |
212 KB |
Output is correct |
47 |
Correct |
5 ms |
448 KB |
Output is correct |
48 |
Correct |
1 ms |
212 KB |
Output is correct |
49 |
Correct |
0 ms |
212 KB |
Output is correct |
50 |
Correct |
1 ms |
212 KB |
Output is correct |
51 |
Correct |
93 ms |
5512 KB |
Output is correct |
52 |
Correct |
56 ms |
3148 KB |
Output is correct |
53 |
Correct |
49 ms |
3152 KB |
Output is correct |
54 |
Correct |
37 ms |
1896 KB |
Output is correct |
55 |
Correct |
94 ms |
5036 KB |
Output is correct |
56 |
Correct |
21 ms |
1324 KB |
Output is correct |
57 |
Correct |
185 ms |
10916 KB |
Output is correct |
58 |
Correct |
183 ms |
10800 KB |
Output is correct |
59 |
Correct |
185 ms |
10796 KB |
Output is correct |
60 |
Correct |
204 ms |
10800 KB |
Output is correct |
61 |
Correct |
244 ms |
10804 KB |
Output is correct |
62 |
Correct |
145 ms |
6188 KB |
Output is correct |
63 |
Correct |
199 ms |
11456 KB |
Output is correct |
64 |
Correct |
167 ms |
6184 KB |
Output is correct |
65 |
Correct |
1 ms |
324 KB |
Output is correct |
66 |
Correct |
147 ms |
6200 KB |
Output is correct |
67 |
Correct |
184 ms |
11460 KB |
Output is correct |
68 |
Correct |
150 ms |
10808 KB |
Output is correct |
69 |
Correct |
27 ms |
1576 KB |
Output is correct |
70 |
Correct |
6 ms |
592 KB |
Output is correct |
71 |
Correct |
1 ms |
316 KB |
Output is correct |
72 |
Correct |
36 ms |
1824 KB |
Output is correct |
73 |
Correct |
8 ms |
648 KB |
Output is correct |
74 |
Correct |
2 ms |
332 KB |
Output is correct |
75 |
Correct |
185 ms |
10812 KB |
Output is correct |
76 |
Correct |
183 ms |
10800 KB |
Output is correct |
77 |
Correct |
191 ms |
10788 KB |
Output is correct |
78 |
Correct |
213 ms |
10924 KB |
Output is correct |
79 |
Correct |
232 ms |
10800 KB |
Output is correct |
80 |
Correct |
243 ms |
10804 KB |
Output is correct |
81 |
Correct |
175 ms |
8872 KB |
Output is correct |
82 |
Correct |
166 ms |
8816 KB |
Output is correct |