/**
* It's not that hard to come up with a almost-full solution. First, let non-lollipop
* be any prize with val < v. The number of non-lollipops is O(sqrt(n)), and there're
* at most 5 types of prizes.
*
* One can also notice that if two prizes u, v have the same value, then the sum of
* reponse for u and v is the same. Therefore, query any O(sqrt(n)) positions and we
* will find the number of non-lollipops. Finally, we know that lollipops account for
* most of the prizes, and their "left response" is increasing from left to right. We
* can treat (a[0]) from lollipops as an increasing sequence, and apply binary search
* to find all non-lollipops. Naturally, we will find the diamond.
* Doing O(sqrt(n)) separate binary searches require about 7000-9000 steps. In impl1,
* I optimize my search by applying those binary searches simultaneously. Namely, for
* each range [l,r] we're working with, query pos (l+r)/2 first. The monotonicity
* helps us determine the range of values for [l,(l+r)/2) and ((l+r)/2,r]. The
* approach is analogous to the well-known divide-and-conquer optimization. I know
* that this method has the same complexity, yet the constant, though I don't know,
* is smaller.
*
* I scored 95.56 points with impl1, with the max query count being 5444. This means
* we're quite close~ Indeed, "parallel binary search" takes less queries than
* O(sqrt(n)) instances of binary search. In impl1.5, I lowered the num of searches
* to find a lollipop: it turns out that my computation was incorrect (yielding a
* suboptimal bound). By using a python script, I brute-forced the max num of
* non-lollipops - 472, achieved with (1, 4, 21, 446, 199528).
*
* Queries needed: sqrt(n) * log(n) (unsure about the const)
* Implementation 1.5
*/
#include <bits/stdc++.h>
#include "prize.h"
typedef std::vector<int> vec;
const int MAX_TYPES = 5;
const int MAX_LOL = 472; // max num of non-lollipops
int non_lol; // number of non-lollipops
int search(int l, int r, int val_lower, int val_upper) {
if (l > r || val_lower == val_upper)
return -1;
int mid = (l + r) / 2, pt = mid, t = -1;
for (; l <= pt; pt--) {
vec res = ask(pt);
if (res[0] + res[1] == 0) // diamond found
return pt;
if (res[0] + res[1] == non_lol) {
t = res[0];
break;
}
}
return std::max(search(l, pt - 1, val_lower, t),
search(mid + 1, r, t + (mid - pt), val_upper));
}
int find_best(int n) {
non_lol = 0;
std::set<int> distinct_sum;
for (int k = 0; k <= MAX_LOL; k++) {
int pos = std::min(n / MAX_LOL * k, n - 1); // roughly evenly-spaced
vec res = ask(pos);
int s = res[0] + res[1];
if (s == 0)
return pos;
non_lol = std::max(non_lol, s);
distinct_sum.emplace(s);
if (int(distinct_sum.size()) >= MAX_TYPES)
break;
}
return search(0, n - 1, 0, MAX_LOL);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
208 KB |
Output is correct |
2 |
Correct |
2 ms |
288 KB |
Output is correct |
3 |
Correct |
5 ms |
208 KB |
Output is correct |
4 |
Correct |
3 ms |
336 KB |
Output is correct |
5 |
Correct |
6 ms |
208 KB |
Output is correct |
6 |
Correct |
1 ms |
208 KB |
Output is correct |
7 |
Correct |
2 ms |
208 KB |
Output is correct |
8 |
Correct |
4 ms |
208 KB |
Output is correct |
9 |
Correct |
4 ms |
208 KB |
Output is correct |
10 |
Correct |
5 ms |
208 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
272 KB |
Output is correct |
2 |
Correct |
2 ms |
288 KB |
Output is correct |
3 |
Correct |
3 ms |
208 KB |
Output is correct |
4 |
Correct |
5 ms |
292 KB |
Output is correct |
5 |
Correct |
3 ms |
208 KB |
Output is correct |
6 |
Correct |
1 ms |
208 KB |
Output is correct |
7 |
Correct |
5 ms |
268 KB |
Output is correct |
8 |
Correct |
5 ms |
208 KB |
Output is correct |
9 |
Correct |
2 ms |
296 KB |
Output is correct |
10 |
Correct |
6 ms |
208 KB |
Output is correct |
11 |
Correct |
7 ms |
208 KB |
Output is correct |
12 |
Correct |
7 ms |
208 KB |
Output is correct |
13 |
Correct |
10 ms |
208 KB |
Output is correct |
14 |
Correct |
11 ms |
208 KB |
Output is correct |
15 |
Correct |
30 ms |
288 KB |
Output is correct |
16 |
Correct |
48 ms |
208 KB |
Output is correct |
17 |
Correct |
37 ms |
292 KB |
Output is correct |
18 |
Correct |
34 ms |
208 KB |
Output is correct |
19 |
Correct |
46 ms |
288 KB |
Output is correct |
20 |
Correct |
36 ms |
208 KB |
Output is correct |
21 |
Correct |
46 ms |
292 KB |
Output is correct |
22 |
Correct |
37 ms |
208 KB |
Output is correct |
23 |
Correct |
6 ms |
208 KB |
Output is correct |
24 |
Correct |
10 ms |
296 KB |
Output is correct |
25 |
Correct |
45 ms |
208 KB |
Output is correct |
26 |
Correct |
18 ms |
288 KB |
Output is correct |
27 |
Correct |
6 ms |
292 KB |
Output is correct |
28 |
Correct |
43 ms |
272 KB |
Output is correct |
29 |
Correct |
40 ms |
292 KB |
Output is correct |
30 |
Correct |
23 ms |
208 KB |
Output is correct |
31 |
Correct |
48 ms |
292 KB |
Output is correct |
32 |
Correct |
4 ms |
292 KB |
Output is correct |
33 |
Correct |
5 ms |
208 KB |
Output is correct |
34 |
Correct |
39 ms |
208 KB |
Output is correct |
35 |
Correct |
6 ms |
296 KB |
Output is correct |
36 |
Correct |
42 ms |
288 KB |
Output is correct |
37 |
Correct |
11 ms |
208 KB |
Output is correct |
38 |
Correct |
7 ms |
208 KB |
Output is correct |
39 |
Correct |
30 ms |
208 KB |
Output is correct |
40 |
Correct |
45 ms |
296 KB |
Output is correct |
41 |
Correct |
38 ms |
208 KB |
Output is correct |
42 |
Correct |
44 ms |
288 KB |
Output is correct |
43 |
Correct |
33 ms |
208 KB |
Output is correct |
44 |
Correct |
20 ms |
280 KB |
Output is correct |
45 |
Correct |
37 ms |
292 KB |
Output is correct |
46 |
Correct |
47 ms |
288 KB |
Output is correct |
47 |
Correct |
34 ms |
208 KB |
Output is correct |
48 |
Correct |
36 ms |
208 KB |
Output is correct |
49 |
Correct |
43 ms |
288 KB |
Output is correct |
50 |
Correct |
35 ms |
208 KB |
Output is correct |
51 |
Correct |
42 ms |
208 KB |
Output is correct |
52 |
Correct |
33 ms |
208 KB |
Output is correct |
53 |
Correct |
5 ms |
208 KB |
Output is correct |
54 |
Correct |
40 ms |
208 KB |
Output is correct |
55 |
Correct |
32 ms |
272 KB |
Output is correct |
56 |
Correct |
40 ms |
208 KB |
Output is correct |
57 |
Correct |
20 ms |
296 KB |
Output is correct |
58 |
Correct |
40 ms |
208 KB |
Output is correct |
59 |
Correct |
38 ms |
288 KB |
Output is correct |
60 |
Correct |
28 ms |
208 KB |
Output is correct |
61 |
Correct |
7 ms |
208 KB |
Output is correct |
62 |
Correct |
8 ms |
208 KB |
Output is correct |
63 |
Correct |
3 ms |
288 KB |
Output is correct |
64 |
Correct |
6 ms |
208 KB |
Output is correct |
65 |
Correct |
7 ms |
296 KB |
Output is correct |
66 |
Correct |
4 ms |
272 KB |
Output is correct |
67 |
Correct |
3 ms |
208 KB |
Output is correct |
68 |
Correct |
9 ms |
292 KB |
Output is correct |
69 |
Correct |
9 ms |
284 KB |
Output is correct |
70 |
Correct |
6 ms |
208 KB |
Output is correct |
71 |
Partially correct |
40 ms |
208 KB |
Partially correct - number of queries: 5232 |
72 |
Correct |
6 ms |
208 KB |
Output is correct |
73 |
Partially correct |
21 ms |
292 KB |
Partially correct - number of queries: 5177 |
74 |
Partially correct |
36 ms |
208 KB |
Partially correct - number of queries: 5204 |
75 |
Correct |
5 ms |
208 KB |
Output is correct |
76 |
Correct |
45 ms |
208 KB |
Output is correct |
77 |
Correct |
42 ms |
208 KB |
Output is correct |
78 |
Correct |
9 ms |
208 KB |
Output is correct |
79 |
Correct |
21 ms |
208 KB |
Output is correct |
80 |
Correct |
21 ms |
288 KB |
Output is correct |
81 |
Correct |
20 ms |
336 KB |
Output is correct |
82 |
Correct |
37 ms |
208 KB |
Output is correct |
83 |
Correct |
4 ms |
208 KB |
Output is correct |
84 |
Correct |
39 ms |
208 KB |
Output is correct |
85 |
Correct |
51 ms |
208 KB |
Output is correct |
86 |
Correct |
16 ms |
292 KB |
Output is correct |
87 |
Correct |
10 ms |
208 KB |
Output is correct |
88 |
Correct |
25 ms |
208 KB |
Output is correct |
89 |
Correct |
26 ms |
208 KB |
Output is correct |
90 |
Correct |
6 ms |
208 KB |
Output is correct |
91 |
Correct |
29 ms |
208 KB |
Output is correct |
92 |
Correct |
28 ms |
292 KB |
Output is correct |
93 |
Correct |
36 ms |
208 KB |
Output is correct |
94 |
Correct |
39 ms |
208 KB |
Output is correct |
95 |
Correct |
37 ms |
208 KB |
Output is correct |
96 |
Correct |
28 ms |
208 KB |
Output is correct |
97 |
Correct |
42 ms |
208 KB |
Output is correct |