#include "prize.h"
#include <cstdlib>
#include <map>
#include <iostream>
using namespace std;
map<int, map<int, pair<int, int>>> resmap;
void resmap_push(int i, int lc, int rc) {
resmap[lc + rc][i] = {lc, rc};
}
map<int, vector<int>> askmp;
vector<int> myask(int i) {
if (askmp.find(i) == askmp.end()) {
askmp[i] = ask(i);
resmap_push(i, askmp[i][0], askmp[i][1]);
}
return askmp[i];
}
int gogo(int fr, int to, int lc, int rc, int mxc) {
// cout << "go " << fr << ' ' << to << endl;
if (lc + rc == mxc) {
return -1;
}
for (auto &rsmp : resmap) {
auto &mp = rsmp.second;
auto lb = mp.lower_bound(fr);
auto ub = mp.upper_bound(to);
pair<int, int> lres, rres;
// cout << "check " << rsmp.first << endl;
if (lb == mp.begin()) {
// cout << "no lb" << endl;
lres = {0, rsmp.first};
} else {
lb--;
// cout << "lb " << lb->first << endl;
lres = lb->second;
}
if (ub == mp.end()) {
// cout << "no ub" << endl;
rres = {rsmp.first, 0};
} else {
// cout << "ub " << ub->first << endl;
rres = ub->second;
}
if (lres.first == rres.first) {
// cout << "skip" << endl;
return -1;
}
}
int mid = (fr + to) / 2;
int lto = mid, rfr = mid + 1;
vector<int> res;
int n = to - fr;
for (int i = 0; i < n; i++) {
if (i & 1) {
mid -= i;
} else {
mid += i;
}
lto = min(lto, mid);
rfr = max(rfr, mid + 1);
res = myask(mid);
int c = res[0] + res[1];
if (c == 0) {
return mid;
} else if (c == mxc) {
int lrc, rlc;
if (mid == lto) {
lrc = res[1];
rlc = res[0] + i;
} else {
lrc = res[1] + i;
rlc = res[0];
}
int rt;
if (rand() & 1) {
rt = gogo(fr, lto, lc, lrc, mxc);
if (rt == -1) {
rt = gogo(rfr, to, rlc, rc, mxc);
}
} else {
rt = gogo(rfr, to, rlc, rc, mxc);
if (rt == -1) {
rt = gogo(fr, lto, lc, lrc, mxc);
}
}
return rt;
}
}
return -1;
}
int find_best(int n) {
srand(9987555);
resmap.clear();
int mxc = 0;
int lc = 0;
for(int i = 0; i < n && i < 500; i++) {
std::vector<int> res = myask(i);
lc = res[0];
if(res[0] + res[1] == 0)
return i;
if (res[0] + res[1] > mxc) {
mxc = res[0] + res[1];
}
}
return gogo(0, n, 0, 0, mxc);
}
Compilation message
prize.cpp: In function 'int find_best(int)':
prize.cpp:101:9: warning: variable 'lc' set but not used [-Wunused-but-set-variable]
int lc = 0;
^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2164 KB |
Output is correct |
2 |
Correct |
0 ms |
2164 KB |
Output is correct |
3 |
Correct |
4 ms |
2164 KB |
Output is correct |
4 |
Correct |
3 ms |
2164 KB |
Output is correct |
5 |
Correct |
0 ms |
2164 KB |
Output is correct |
6 |
Correct |
0 ms |
2032 KB |
Output is correct |
7 |
Correct |
0 ms |
2164 KB |
Output is correct |
8 |
Correct |
1 ms |
2164 KB |
Output is correct |
9 |
Correct |
3 ms |
2164 KB |
Output is correct |
10 |
Correct |
0 ms |
2164 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
2164 KB |
Output is correct |
2 |
Correct |
4 ms |
2164 KB |
Output is correct |
3 |
Correct |
4 ms |
2164 KB |
Output is correct |
4 |
Correct |
0 ms |
2164 KB |
Output is correct |
5 |
Correct |
2 ms |
2164 KB |
Output is correct |
6 |
Correct |
0 ms |
2032 KB |
Output is correct |
7 |
Correct |
2 ms |
2164 KB |
Output is correct |
8 |
Correct |
3 ms |
2164 KB |
Output is correct |
9 |
Correct |
4 ms |
2164 KB |
Output is correct |
10 |
Correct |
5 ms |
2164 KB |
Output is correct |
11 |
Correct |
2 ms |
2164 KB |
Output is correct |
12 |
Correct |
6 ms |
2164 KB |
Output is correct |
13 |
Correct |
0 ms |
2164 KB |
Output is correct |
14 |
Correct |
0 ms |
2164 KB |
Output is correct |
15 |
Correct |
3 ms |
2296 KB |
Output is correct |
16 |
Correct |
2 ms |
2164 KB |
Output is correct |
17 |
Correct |
2 ms |
2164 KB |
Output is correct |
18 |
Correct |
1 ms |
2164 KB |
Output is correct |
19 |
Correct |
3 ms |
2164 KB |
Output is correct |
20 |
Correct |
5 ms |
2296 KB |
Output is correct |
21 |
Correct |
3 ms |
2296 KB |
Output is correct |
22 |
Correct |
5 ms |
2296 KB |
Output is correct |
23 |
Correct |
9 ms |
2164 KB |
Output is correct |
24 |
Correct |
2 ms |
2164 KB |
Output is correct |
25 |
Correct |
4 ms |
2164 KB |
Output is correct |
26 |
Correct |
9 ms |
2164 KB |
Output is correct |
27 |
Correct |
0 ms |
2164 KB |
Output is correct |
28 |
Correct |
1 ms |
2164 KB |
Output is correct |
29 |
Correct |
2 ms |
2164 KB |
Output is correct |
30 |
Correct |
4 ms |
2164 KB |
Output is correct |
31 |
Correct |
1 ms |
2164 KB |
Output is correct |
32 |
Correct |
3 ms |
2164 KB |
Output is correct |
33 |
Correct |
0 ms |
2032 KB |
Output is correct |
34 |
Correct |
1 ms |
2296 KB |
Output is correct |
35 |
Correct |
7 ms |
2164 KB |
Output is correct |
36 |
Correct |
6 ms |
2296 KB |
Output is correct |
37 |
Correct |
2 ms |
2164 KB |
Output is correct |
38 |
Correct |
16 ms |
2164 KB |
Output is correct |
39 |
Correct |
4 ms |
2296 KB |
Output is correct |
40 |
Correct |
2 ms |
2164 KB |
Output is correct |
41 |
Correct |
4 ms |
2296 KB |
Output is correct |
42 |
Correct |
7 ms |
2296 KB |
Output is correct |
43 |
Correct |
1 ms |
2296 KB |
Output is correct |
44 |
Correct |
3 ms |
2296 KB |
Output is correct |
45 |
Correct |
3 ms |
2296 KB |
Output is correct |
46 |
Correct |
2 ms |
2164 KB |
Output is correct |
47 |
Correct |
0 ms |
2296 KB |
Output is correct |
48 |
Correct |
8 ms |
2296 KB |
Output is correct |
49 |
Correct |
6 ms |
2296 KB |
Output is correct |
50 |
Correct |
8 ms |
2164 KB |
Output is correct |
51 |
Correct |
1 ms |
2428 KB |
Output is correct |
52 |
Correct |
12 ms |
2428 KB |
Output is correct |
53 |
Correct |
10 ms |
2164 KB |
Output is correct |
54 |
Correct |
2 ms |
2296 KB |
Output is correct |
55 |
Correct |
4 ms |
2164 KB |
Output is correct |
56 |
Correct |
7 ms |
2164 KB |
Output is correct |
57 |
Correct |
2 ms |
2296 KB |
Output is correct |
58 |
Correct |
2 ms |
2164 KB |
Output is correct |
59 |
Correct |
4 ms |
2164 KB |
Output is correct |
60 |
Correct |
5 ms |
2296 KB |
Output is correct |
61 |
Correct |
3 ms |
2164 KB |
Output is correct |
62 |
Correct |
0 ms |
2164 KB |
Output is correct |
63 |
Correct |
9 ms |
2164 KB |
Output is correct |
64 |
Correct |
0 ms |
2164 KB |
Output is correct |
65 |
Correct |
6 ms |
2164 KB |
Output is correct |
66 |
Correct |
1 ms |
2164 KB |
Output is correct |
67 |
Correct |
12 ms |
2164 KB |
Output is correct |
68 |
Correct |
4 ms |
2164 KB |
Output is correct |
69 |
Correct |
5 ms |
2164 KB |
Output is correct |
70 |
Correct |
1 ms |
2164 KB |
Output is correct |
71 |
Correct |
7 ms |
2692 KB |
Output is correct |
72 |
Correct |
2 ms |
2164 KB |
Output is correct |
73 |
Correct |
17 ms |
2692 KB |
Output is correct |
74 |
Correct |
5 ms |
2560 KB |
Output is correct |
75 |
Correct |
2 ms |
2164 KB |
Output is correct |
76 |
Correct |
13 ms |
2428 KB |
Output is correct |
77 |
Correct |
6 ms |
2428 KB |
Output is correct |
78 |
Correct |
2 ms |
2164 KB |
Output is correct |
79 |
Correct |
12 ms |
2296 KB |
Output is correct |
80 |
Correct |
28 ms |
2560 KB |
Output is correct |
81 |
Correct |
5 ms |
2428 KB |
Output is correct |
82 |
Correct |
4 ms |
2296 KB |
Output is correct |
83 |
Correct |
0 ms |
2164 KB |
Output is correct |
84 |
Correct |
0 ms |
2428 KB |
Output is correct |
85 |
Correct |
15 ms |
2428 KB |
Output is correct |
86 |
Correct |
4 ms |
2164 KB |
Output is correct |
87 |
Correct |
0 ms |
2164 KB |
Output is correct |
88 |
Correct |
9 ms |
2164 KB |
Output is correct |
89 |
Correct |
4 ms |
2164 KB |
Output is correct |
90 |
Correct |
1 ms |
2164 KB |
Output is correct |
91 |
Correct |
1 ms |
2164 KB |
Output is correct |
92 |
Correct |
8 ms |
2164 KB |
Output is correct |
93 |
Correct |
1 ms |
2164 KB |
Output is correct |
94 |
Correct |
11 ms |
2164 KB |
Output is correct |
95 |
Correct |
4 ms |
2164 KB |
Output is correct |
96 |
Correct |
1 ms |
2164 KB |
Output is correct |
97 |
Correct |
1 ms |
2164 KB |
Output is correct |