#include "prize.h"
#include <bits/stdc++.h>
using namespace std;
map<int, pair<int, int>> memo;
vector<int> query(int i) {
vector<int> res;
if (memo.count(i)) {
res.emplace_back(memo[i].first);
res.emplace_back(memo[i].second);
} else {
res = ask(i);
memo[i] = {res[0], res[1]};
}
return res;
}
int numValuable;
vector<int> candidates; // non-lollipop boxes
void binarySearch(int leftValuable, int rightValuable, int left, int right) {
if (left > right || leftValuable + rightValuable == numValuable) {
return;
}
int mid = (left + right) / 2;
vector<int> res = query(mid);
if (res[0] + res[1] == numValuable) { // mid is a lollipop box
binarySearch(leftValuable, res[1], left, mid - 1);
binarySearch(res[0], rightValuable, mid + 1, right);
} else { // mid is a non-lollipop box
candidates.emplace_back(mid);
int nearestLeft, nearestRight;
for (int i = mid + 1; i <= right; i++) {
res = query(i);
if (res[0] + res[1] == numValuable) {
nearestRight = i;
binarySearch(query(nearestRight)[0], rightValuable, nearestRight + 1, right);
break;
} else { // i is a non-lollipop box
candidates.emplace_back(i);
}
}
for (int i = mid - 1; i >= left; i--) {
res = query(i);
if (res[0] + res[1] == numValuable) {
nearestLeft = i;
binarySearch(leftValuable, query(nearestLeft)[1], left, nearestLeft - 1);
break;
} else { // i is a non-lollipop box
candidates.emplace_back(i);
}
}
}
}
int find_best(int n) {
numValuable = 0;
for (int i = 0; i < min(n, 500); i++) { // least valuable box must satisfy sum^2 <= n, and since n <= 200000 then sum <= 500 will suffice
vector<int> res = query(i);
if (res[0] + res[1] > numValuable) {
numValuable = res[0] + res[1];
}
if (numValuable > 30) { // second least valuable box must satisfy (sum^2)^2 <= n, and since n <= 200000 then sum <= 30 will suffice
break; // we will only check at most one least valuable box, the rest will be valuable and checked later anyways
}
}
binarySearch(0, 0, 0, n - 1);
int ans = -1;
for (auto &c : candidates) {
vector<int> res = query(c);
if (res[0] == 0 && res[1] == 0) {
ans = c;
}
}
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
248 KB |
Output is correct |
2 |
Correct |
9 ms |
376 KB |
Output is correct |
3 |
Correct |
7 ms |
376 KB |
Output is correct |
4 |
Correct |
9 ms |
248 KB |
Output is correct |
5 |
Correct |
10 ms |
248 KB |
Output is correct |
6 |
Correct |
7 ms |
400 KB |
Output is correct |
7 |
Correct |
10 ms |
248 KB |
Output is correct |
8 |
Correct |
9 ms |
308 KB |
Output is correct |
9 |
Correct |
9 ms |
248 KB |
Output is correct |
10 |
Correct |
12 ms |
248 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
316 KB |
Output is correct |
2 |
Correct |
9 ms |
248 KB |
Output is correct |
3 |
Correct |
7 ms |
400 KB |
Output is correct |
4 |
Correct |
9 ms |
248 KB |
Output is correct |
5 |
Correct |
10 ms |
248 KB |
Output is correct |
6 |
Correct |
9 ms |
380 KB |
Output is correct |
7 |
Correct |
10 ms |
248 KB |
Output is correct |
8 |
Correct |
10 ms |
248 KB |
Output is correct |
9 |
Correct |
7 ms |
400 KB |
Output is correct |
10 |
Correct |
10 ms |
248 KB |
Output is correct |
11 |
Correct |
7 ms |
400 KB |
Output is correct |
12 |
Correct |
9 ms |
376 KB |
Output is correct |
13 |
Correct |
9 ms |
248 KB |
Output is correct |
14 |
Correct |
15 ms |
376 KB |
Output is correct |
15 |
Correct |
46 ms |
632 KB |
Output is correct |
16 |
Correct |
44 ms |
504 KB |
Output is correct |
17 |
Correct |
49 ms |
524 KB |
Output is correct |
18 |
Correct |
41 ms |
504 KB |
Output is correct |
19 |
Correct |
44 ms |
504 KB |
Output is correct |
20 |
Correct |
37 ms |
504 KB |
Output is correct |
21 |
Correct |
56 ms |
604 KB |
Output is correct |
22 |
Correct |
35 ms |
504 KB |
Output is correct |
23 |
Correct |
11 ms |
248 KB |
Output is correct |
24 |
Correct |
9 ms |
248 KB |
Output is correct |
25 |
Correct |
49 ms |
560 KB |
Output is correct |
26 |
Correct |
46 ms |
504 KB |
Output is correct |
27 |
Correct |
11 ms |
248 KB |
Output is correct |
28 |
Correct |
42 ms |
632 KB |
Output is correct |
29 |
Correct |
24 ms |
608 KB |
Output is correct |
30 |
Correct |
48 ms |
632 KB |
Output is correct |
31 |
Correct |
30 ms |
668 KB |
Output is correct |
32 |
Correct |
7 ms |
528 KB |
Output is correct |
33 |
Correct |
5 ms |
248 KB |
Output is correct |
34 |
Correct |
44 ms |
504 KB |
Output is correct |
35 |
Correct |
12 ms |
300 KB |
Output is correct |
36 |
Correct |
43 ms |
632 KB |
Output is correct |
37 |
Correct |
9 ms |
376 KB |
Output is correct |
38 |
Correct |
11 ms |
248 KB |
Output is correct |
39 |
Correct |
46 ms |
632 KB |
Output is correct |
40 |
Correct |
37 ms |
632 KB |
Output is correct |
41 |
Correct |
51 ms |
564 KB |
Output is correct |
42 |
Correct |
48 ms |
564 KB |
Output is correct |
43 |
Correct |
41 ms |
632 KB |
Output is correct |
44 |
Correct |
52 ms |
504 KB |
Output is correct |
45 |
Correct |
27 ms |
588 KB |
Output is correct |
46 |
Correct |
46 ms |
508 KB |
Output is correct |
47 |
Correct |
45 ms |
632 KB |
Output is correct |
48 |
Correct |
45 ms |
504 KB |
Output is correct |
49 |
Correct |
51 ms |
564 KB |
Output is correct |
50 |
Correct |
45 ms |
780 KB |
Output is correct |
51 |
Correct |
30 ms |
668 KB |
Output is correct |
52 |
Correct |
44 ms |
604 KB |
Output is correct |
53 |
Correct |
14 ms |
248 KB |
Output is correct |
54 |
Correct |
46 ms |
836 KB |
Output is correct |
55 |
Correct |
49 ms |
508 KB |
Output is correct |
56 |
Correct |
39 ms |
564 KB |
Output is correct |
57 |
Correct |
44 ms |
504 KB |
Output is correct |
58 |
Correct |
58 ms |
504 KB |
Output is correct |
59 |
Correct |
46 ms |
556 KB |
Output is correct |
60 |
Correct |
42 ms |
512 KB |
Output is correct |
61 |
Correct |
10 ms |
248 KB |
Output is correct |
62 |
Correct |
8 ms |
348 KB |
Output is correct |
63 |
Correct |
11 ms |
376 KB |
Output is correct |
64 |
Correct |
11 ms |
248 KB |
Output is correct |
65 |
Correct |
5 ms |
248 KB |
Output is correct |
66 |
Correct |
9 ms |
248 KB |
Output is correct |
67 |
Correct |
9 ms |
248 KB |
Output is correct |
68 |
Correct |
9 ms |
248 KB |
Output is correct |
69 |
Correct |
7 ms |
400 KB |
Output is correct |
70 |
Correct |
7 ms |
252 KB |
Output is correct |
71 |
Correct |
50 ms |
632 KB |
Output is correct |
72 |
Correct |
10 ms |
248 KB |
Output is correct |
73 |
Correct |
50 ms |
548 KB |
Output is correct |
74 |
Correct |
51 ms |
504 KB |
Output is correct |
75 |
Correct |
11 ms |
408 KB |
Output is correct |
76 |
Correct |
42 ms |
632 KB |
Output is correct |
77 |
Correct |
48 ms |
584 KB |
Output is correct |
78 |
Correct |
9 ms |
248 KB |
Output is correct |
79 |
Correct |
33 ms |
376 KB |
Output is correct |
80 |
Correct |
50 ms |
632 KB |
Output is correct |
81 |
Correct |
49 ms |
504 KB |
Output is correct |
82 |
Correct |
44 ms |
504 KB |
Output is correct |
83 |
Correct |
11 ms |
248 KB |
Output is correct |
84 |
Correct |
44 ms |
504 KB |
Output is correct |
85 |
Correct |
51 ms |
504 KB |
Output is correct |
86 |
Correct |
10 ms |
248 KB |
Output is correct |
87 |
Correct |
5 ms |
248 KB |
Output is correct |
88 |
Correct |
9 ms |
248 KB |
Output is correct |
89 |
Correct |
10 ms |
252 KB |
Output is correct |
90 |
Correct |
10 ms |
424 KB |
Output is correct |
91 |
Correct |
7 ms |
248 KB |
Output is correct |
92 |
Correct |
10 ms |
380 KB |
Output is correct |
93 |
Correct |
12 ms |
248 KB |
Output is correct |
94 |
Correct |
15 ms |
376 KB |
Output is correct |
95 |
Correct |
12 ms |
248 KB |
Output is correct |
96 |
Correct |
11 ms |
248 KB |
Output is correct |
97 |
Correct |
10 ms |
248 KB |
Output is correct |