#include "prize.h"
#include <algorithm>
#include <iostream>
#include <chrono>
#include <random>
#include <vector>
using namespace std;
#ifdef MANSON
mt19937 rng(305);
#else
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#endif // MANSON
namespace
{
const int maxN = 200000;
const int mag = 1200;
int n;
vector<int> cache[maxN];
inline vector<int> Ask(int i)
{
if (cache[i].size()) return cache[i];
return cache[i] = ask(i);
}
int mx = 0;
inline bool Cheapest(int i)
{
vector<int> res = Ask(i);
bool f = (res[0] + res[1] >= mx);
// cerr << i << " is" << (f ? " " : "n't ") << " a cheapy.\n";
return f;
}
int Solve(int l, int r)
{
static uniform_int_distribution<int> rnd(1, 2);
if (rnd(rng) == 2)
{
while (l <= r && !Cheapest(l))
{
vector<int> res = Ask(l);
if (res[0] + res[1] == 0) return l;
++l;
}
if (l > r) return -1;
int len = 1;
for (int x = (1 << 17); x >= 1; x >>= 1)
{
int R = l + (len + x) - 1;
if (R > r) continue;
// if (!Cheapest(R)) continue;
vector<int> res1 = Ask(l), res2 = Ask(R);
if (res1[1] == res2[1])
len += x;
}
if (l + len <= r)
return Solve(l + len, r);
return -1;
}
while (l <= r && !Cheapest(r))
{
vector<int> res = Ask(r);
if (res[0] + res[1] == 0) return r;
--r;
}
if (l > r) return -1;
int len = 1;
for (int x = (1 << 17); x >= 1; x >>= 1)
{
int L = r - (len + x) + 1;
if (L < l) continue;
if (!Cheapest(L)) continue;
vector<int> res1 = Ask(L), res2 = Ask(r);
if (res1[1] == res2[1])
len += x;
}
if (l <= r - len)
return Solve(l, r - len);
return -1;
}
}
int find_best(int n_) {
n = n_;
vector<int> p(n);
iota(p.begin(), p.end(), 0);
shuffle(p.begin(), p.end(), rng);
if (p.size() > 446) p.erase(p.begin() + 446, p.end());
for (int i: p)
{
vector<int> res = Ask(i);
mx = max(mx, res[0] + res[1]);
if (res[0] + res[1] >= 22)
break;
if (res[0] + res[1] == 0) return i;
}
vector<int> x;
for (int i = 0; i * mag < n; ++i)
x.push_back(i);
shuffle(x.begin(), x.end(), rng);
for (int i: x)
{
int res = Solve(i * mag, min(i * mag + mag, n - 1));
if (res != -1)
return res;
}
}
Compilation message
prize.cpp: In function 'int find_best(int)':
prize.cpp:107:1: warning: control reaches end of non-void function [-Wreturn-type]
}
^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
5880 KB |
Output is correct |
2 |
Correct |
27 ms |
5880 KB |
Output is correct |
3 |
Correct |
16 ms |
5936 KB |
Output is correct |
4 |
Correct |
22 ms |
6008 KB |
Output is correct |
5 |
Correct |
20 ms |
5880 KB |
Output is correct |
6 |
Correct |
19 ms |
5888 KB |
Output is correct |
7 |
Correct |
17 ms |
5936 KB |
Output is correct |
8 |
Correct |
20 ms |
5880 KB |
Output is correct |
9 |
Correct |
24 ms |
5880 KB |
Output is correct |
10 |
Correct |
17 ms |
5888 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
5888 KB |
Output is correct |
2 |
Correct |
17 ms |
5948 KB |
Output is correct |
3 |
Correct |
25 ms |
5880 KB |
Output is correct |
4 |
Correct |
18 ms |
5952 KB |
Output is correct |
5 |
Correct |
18 ms |
5884 KB |
Output is correct |
6 |
Correct |
17 ms |
5888 KB |
Output is correct |
7 |
Correct |
22 ms |
5880 KB |
Output is correct |
8 |
Correct |
18 ms |
5964 KB |
Output is correct |
9 |
Correct |
16 ms |
5952 KB |
Output is correct |
10 |
Correct |
18 ms |
5888 KB |
Output is correct |
11 |
Correct |
14 ms |
5752 KB |
Output is correct |
12 |
Correct |
16 ms |
5752 KB |
Output is correct |
13 |
Correct |
16 ms |
5760 KB |
Output is correct |
14 |
Correct |
16 ms |
5120 KB |
Output is correct |
15 |
Correct |
20 ms |
5976 KB |
Output is correct |
16 |
Correct |
38 ms |
6060 KB |
Output is correct |
17 |
Correct |
52 ms |
5888 KB |
Output is correct |
18 |
Correct |
21 ms |
5968 KB |
Output is correct |
19 |
Correct |
26 ms |
5832 KB |
Output is correct |
20 |
Correct |
39 ms |
5496 KB |
Output is correct |
21 |
Correct |
31 ms |
5892 KB |
Output is correct |
22 |
Correct |
32 ms |
5880 KB |
Output is correct |
23 |
Correct |
20 ms |
5880 KB |
Output is correct |
24 |
Correct |
15 ms |
5752 KB |
Output is correct |
25 |
Partially correct |
40 ms |
6092 KB |
Partially correct - number of queries: 5327 |
26 |
Correct |
38 ms |
6004 KB |
Output is correct |
27 |
Correct |
19 ms |
5888 KB |
Output is correct |
28 |
Correct |
16 ms |
5888 KB |
Output is correct |
29 |
Correct |
57 ms |
6008 KB |
Output is correct |
30 |
Correct |
19 ms |
5760 KB |
Output is correct |
31 |
Correct |
24 ms |
5880 KB |
Output is correct |
32 |
Correct |
15 ms |
5888 KB |
Output is correct |
33 |
Correct |
8 ms |
4992 KB |
Output is correct |
34 |
Correct |
39 ms |
5880 KB |
Output is correct |
35 |
Correct |
25 ms |
5880 KB |
Output is correct |
36 |
Correct |
31 ms |
5880 KB |
Output is correct |
37 |
Correct |
20 ms |
5880 KB |
Output is correct |
38 |
Correct |
17 ms |
5940 KB |
Output is correct |
39 |
Correct |
23 ms |
5976 KB |
Output is correct |
40 |
Correct |
36 ms |
6016 KB |
Output is correct |
41 |
Correct |
37 ms |
5888 KB |
Output is correct |
42 |
Correct |
39 ms |
5888 KB |
Output is correct |
43 |
Correct |
47 ms |
5880 KB |
Output is correct |
44 |
Correct |
29 ms |
5880 KB |
Output is correct |
45 |
Correct |
45 ms |
6092 KB |
Output is correct |
46 |
Correct |
20 ms |
5828 KB |
Output is correct |
47 |
Correct |
55 ms |
6008 KB |
Output is correct |
48 |
Correct |
18 ms |
5884 KB |
Output is correct |
49 |
Correct |
54 ms |
6092 KB |
Output is correct |
50 |
Correct |
29 ms |
5932 KB |
Output is correct |
51 |
Partially correct |
57 ms |
5924 KB |
Partially correct - number of queries: 5211 |
52 |
Correct |
32 ms |
5880 KB |
Output is correct |
53 |
Correct |
19 ms |
5900 KB |
Output is correct |
54 |
Correct |
16 ms |
5752 KB |
Output is correct |
55 |
Partially correct |
55 ms |
6008 KB |
Partially correct - number of queries: 5024 |
56 |
Correct |
49 ms |
5880 KB |
Output is correct |
57 |
Partially correct |
55 ms |
6144 KB |
Partially correct - number of queries: 5285 |
58 |
Correct |
32 ms |
5880 KB |
Output is correct |
59 |
Correct |
39 ms |
5888 KB |
Output is correct |
60 |
Correct |
43 ms |
6000 KB |
Output is correct |
61 |
Correct |
20 ms |
5888 KB |
Output is correct |
62 |
Correct |
31 ms |
5880 KB |
Output is correct |
63 |
Correct |
20 ms |
5864 KB |
Output is correct |
64 |
Correct |
24 ms |
5888 KB |
Output is correct |
65 |
Correct |
22 ms |
5880 KB |
Output is correct |
66 |
Correct |
20 ms |
5888 KB |
Output is correct |
67 |
Correct |
24 ms |
5880 KB |
Output is correct |
68 |
Correct |
22 ms |
5880 KB |
Output is correct |
69 |
Correct |
15 ms |
5888 KB |
Output is correct |
70 |
Correct |
23 ms |
5880 KB |
Output is correct |
71 |
Partially correct |
58 ms |
6136 KB |
Partially correct - number of queries: 5235 |
72 |
Correct |
23 ms |
5888 KB |
Output is correct |
73 |
Correct |
52 ms |
6008 KB |
Output is correct |
74 |
Partially correct |
51 ms |
6008 KB |
Partially correct - number of queries: 5174 |
75 |
Correct |
24 ms |
6016 KB |
Output is correct |
76 |
Correct |
53 ms |
6008 KB |
Output is correct |
77 |
Partially correct |
70 ms |
6008 KB |
Partially correct - number of queries: 5471 |
78 |
Correct |
20 ms |
5888 KB |
Output is correct |
79 |
Correct |
38 ms |
6136 KB |
Output is correct |
80 |
Partially correct |
53 ms |
6008 KB |
Partially correct - number of queries: 5384 |
81 |
Partially correct |
58 ms |
6008 KB |
Partially correct - number of queries: 5253 |
82 |
Partially correct |
59 ms |
6008 KB |
Partially correct - number of queries: 5311 |
83 |
Correct |
24 ms |
5888 KB |
Output is correct |
84 |
Correct |
54 ms |
6008 KB |
Output is correct |
85 |
Partially correct |
59 ms |
6008 KB |
Partially correct - number of queries: 5312 |
86 |
Correct |
27 ms |
5888 KB |
Output is correct |
87 |
Correct |
19 ms |
5880 KB |
Output is correct |
88 |
Correct |
30 ms |
5888 KB |
Output is correct |
89 |
Correct |
19 ms |
5888 KB |
Output is correct |
90 |
Correct |
21 ms |
6000 KB |
Output is correct |
91 |
Correct |
26 ms |
5888 KB |
Output is correct |
92 |
Correct |
19 ms |
5884 KB |
Output is correct |
93 |
Correct |
31 ms |
5888 KB |
Output is correct |
94 |
Correct |
28 ms |
5852 KB |
Output is correct |
95 |
Correct |
27 ms |
6012 KB |
Output is correct |
96 |
Correct |
29 ms |
5888 KB |
Output is correct |
97 |
Partially correct |
56 ms |
6008 KB |
Partially correct - number of queries: 5346 |