#include "prize.h"
#include <iostream>
#include <vector>
#include <algorithm>
#include "bits/stdc++.h"
using namespace std;
int find_best(int n);
std::vector<int> ask(int i);
static const int max_q = 5000;
static int n;
static int query_count = 0;
static vector<int> g;
static vector<vector<int> > rank_count;
mt19937 rng((int)std::chrono::steady_clock::now().time_since_epoch().count());
int rnd(int x, int y) {
int u = uniform_int_distribution<int>(x, y)(rng); return u;
}
int find_best(int n) {
if(n <= 5000) {
for(int i=0; i<n; i++) {
vector<int> res = ask(i);
if(res[0] + res[1] == 0) {
return i;
}
}
}
int res[n][2];
for(int i=0; i<n; i++) {
for(int j=0; j<2; j++) {
res[i][j] = -1;
}
}
int t = ceil(sqrt(n));
vector<int> v;
int goodcnt = 0;
for(int i=0; i<t; i++) {
int x;
x = t * i + (t - 1);
if(x >= n) {
while(1) {
x = rnd(0, n - 1);
if(res[x][0] == -1) break;
}
}
vector<int> ok = ask(x);
goodcnt = max(goodcnt, ok[0] + ok[1]);
res[x][0] = ok[0], res[x][1] = ok[1];
if(ok[0] + ok[1] == 0) {
return x;
}
//cout << ok[0] + ok[1] << " ";
}
//cout << "\n";
//cout << goodcnt << "\n";
// solution idea: binary search
// find all 'good' indices, one of them holds the big prize
// 450 * log2(450) + C queries?
vector<int> big;
for(int i=0; i<n; i++) {
if(res[i][0] != -1 && res[i][0] + res[i][1] == goodcnt) {
big.push_back(i);
//cout << i << " ";
}
}
//cout << "\n";
//cout << big.size() << "\n";
queue<pair<pair<int, int>, int> > ranges;
if(big[0] != 0 && res[big[0]][0] > 0) ranges.push({{0, big[0] - 1}, res[big[0]][0]});
for(int i=1; i<big.size(); i++) {
if(big[i-1] + 1 == big[i]) continue;
if(res[big[i]][0] - res[big[i-1]][0] > 0) ranges.push({{big[i-1] + 1, big[i] - 1}, res[big[i]][0] - res[big[i-1]][0]});
}
if(big[big.size() - 1] != n - 1 && res[big[big.size() - 1]][1] > 0) ranges.push({{big[big.size() - 1] + 1, n - 1}, res[big[big.size() - 1]][1]});
//cout << ranges.size() << "\n";
while(ranges.size()) {
pair<pair<int, int>, int> f = ranges.front();
//cout << "split [" << f.first.first <<", "<<f.first.second <<"]: " <<f.second << "\n";
ranges.pop();
if(f.second * 1.1 >= f.first.second - f.first.first + 1) {
// just query each one, at most 2 * 450 here
for(int i = f.first.first; i <= f.first.second; i++) {
vector<int> qwq = ask(i);
if(qwq[0] + qwq[1] == 0) return i;
}
}
else {
int mid = (f.first.first + f.first.second) >> 1;
vector<int> qwq;
while(1) {
if(res[mid][0] == -1) {
qwq = ask(mid);
res[mid][0] = qwq[0];
res[mid][1] = qwq[1];
if(qwq[0] + qwq[1] == 0) return mid;
if(qwq[0] + qwq[1] == goodcnt) break;
}
mid = rnd(f.first.first, f.first.second);
}
/*
cout << "lchild " <<(f.first.first == 0 ? 0 : res[f.first.first - 1][0]) << ", ";
cout << "rchild "<<(f.first.second == n - 1 ? 0 : res[f.first.second + 1][1])<<"\n";
cout << "results "<<qwq[0] <<" " <<qwq[1] << "\n";
*/
if(f.first.first <= mid - 1 && qwq[0] - (f.first.first == 0 ? 0 : res[f.first.first - 1][0]) > 0) {
ranges.push({{f.first.first, mid - 1}, qwq[0] - (f.first.first == 0 ? 0 : res[f.first.first - 1][0])});
}
if(mid + 1 <= f.first.second && qwq[1] - (f.first.second == n - 1 ? 0 : res[f.first.second + 1][1]) > 0) {
ranges.push({{mid + 1, f.first.second}, qwq[1] - (f.first.second == n - 1 ? 0 : res[f.first.second + 1][1])});
}
}
}
}
Compilation message
prize.cpp: In function 'int find_best(int)':
prize.cpp:84:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
84 | for(int i=1; i<big.size(); i++) {
| ~^~~~~~~~~~~
prize.cpp:46:15: warning: control reaches end of non-void function [-Wreturn-type]
46 | vector<int> v;
| ^
prize.cpp: At global scope:
prize.cpp:18:12: warning: 'query_count' defined but not used [-Wunused-variable]
18 | static int query_count = 0;
| ^~~~~~~~~~~
prize.cpp:17:12: warning: 'n' defined but not used [-Wunused-variable]
17 | static int n;
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
1744 KB |
Output is correct |
2 |
Correct |
6 ms |
1832 KB |
Output is correct |
3 |
Correct |
3 ms |
1896 KB |
Output is correct |
4 |
Correct |
3 ms |
1848 KB |
Output is correct |
5 |
Correct |
4 ms |
1908 KB |
Output is correct |
6 |
Correct |
6 ms |
1832 KB |
Output is correct |
7 |
Correct |
6 ms |
1744 KB |
Output is correct |
8 |
Correct |
6 ms |
1828 KB |
Output is correct |
9 |
Correct |
3 ms |
1872 KB |
Output is correct |
10 |
Correct |
6 ms |
1872 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
1832 KB |
Output is correct |
2 |
Correct |
6 ms |
1828 KB |
Output is correct |
3 |
Correct |
4 ms |
1868 KB |
Output is correct |
4 |
Correct |
5 ms |
1828 KB |
Output is correct |
5 |
Correct |
7 ms |
1820 KB |
Output is correct |
6 |
Correct |
6 ms |
1824 KB |
Output is correct |
7 |
Correct |
5 ms |
1744 KB |
Output is correct |
8 |
Correct |
3 ms |
1868 KB |
Output is correct |
9 |
Correct |
4 ms |
1832 KB |
Output is correct |
10 |
Correct |
8 ms |
1868 KB |
Output is correct |
11 |
Correct |
7 ms |
1744 KB |
Output is correct |
12 |
Correct |
6 ms |
1736 KB |
Output is correct |
13 |
Correct |
7 ms |
1824 KB |
Output is correct |
14 |
Correct |
8 ms |
464 KB |
Output is correct |
15 |
Correct |
30 ms |
1872 KB |
Output is correct |
16 |
Correct |
40 ms |
1864 KB |
Output is correct |
17 |
Correct |
31 ms |
1824 KB |
Output is correct |
18 |
Correct |
38 ms |
1736 KB |
Output is correct |
19 |
Correct |
27 ms |
1836 KB |
Output is correct |
20 |
Correct |
22 ms |
1060 KB |
Output is correct |
21 |
Correct |
29 ms |
1836 KB |
Output is correct |
22 |
Correct |
30 ms |
1828 KB |
Output is correct |
23 |
Correct |
6 ms |
1736 KB |
Output is correct |
24 |
Correct |
8 ms |
1832 KB |
Output is correct |
25 |
Correct |
31 ms |
1736 KB |
Output is correct |
26 |
Correct |
17 ms |
1852 KB |
Output is correct |
27 |
Correct |
5 ms |
1744 KB |
Output is correct |
28 |
Correct |
34 ms |
1840 KB |
Output is correct |
29 |
Correct |
31 ms |
1856 KB |
Output is correct |
30 |
Correct |
22 ms |
1868 KB |
Output is correct |
31 |
Correct |
38 ms |
1832 KB |
Output is correct |
32 |
Correct |
7 ms |
1824 KB |
Output is correct |
33 |
Correct |
0 ms |
208 KB |
Output is correct |
34 |
Correct |
39 ms |
1828 KB |
Output is correct |
35 |
Correct |
7 ms |
1828 KB |
Output is correct |
36 |
Correct |
40 ms |
1824 KB |
Output is correct |
37 |
Correct |
5 ms |
1744 KB |
Output is correct |
38 |
Correct |
4 ms |
1860 KB |
Output is correct |
39 |
Correct |
28 ms |
1832 KB |
Output is correct |
40 |
Correct |
38 ms |
1872 KB |
Output is correct |
41 |
Correct |
44 ms |
1832 KB |
Output is correct |
42 |
Correct |
21 ms |
1848 KB |
Output is correct |
43 |
Correct |
27 ms |
1744 KB |
Output is correct |
44 |
Correct |
35 ms |
1840 KB |
Output is correct |
45 |
Correct |
26 ms |
1744 KB |
Output is correct |
46 |
Correct |
36 ms |
1860 KB |
Output is correct |
47 |
Correct |
26 ms |
1808 KB |
Output is correct |
48 |
Correct |
24 ms |
1736 KB |
Output is correct |
49 |
Correct |
43 ms |
1848 KB |
Output is correct |
50 |
Correct |
39 ms |
1832 KB |
Output is correct |
51 |
Correct |
40 ms |
1872 KB |
Output is correct |
52 |
Correct |
34 ms |
1828 KB |
Output is correct |
53 |
Correct |
8 ms |
1816 KB |
Output is correct |
54 |
Correct |
18 ms |
1844 KB |
Output is correct |
55 |
Correct |
31 ms |
1736 KB |
Output is correct |
56 |
Correct |
37 ms |
1744 KB |
Output is correct |
57 |
Correct |
39 ms |
1832 KB |
Output is correct |
58 |
Correct |
36 ms |
1744 KB |
Output is correct |
59 |
Correct |
36 ms |
1744 KB |
Output is correct |
60 |
Correct |
37 ms |
1820 KB |
Output is correct |
61 |
Correct |
5 ms |
1864 KB |
Output is correct |
62 |
Correct |
6 ms |
1744 KB |
Output is correct |
63 |
Correct |
5 ms |
1820 KB |
Output is correct |
64 |
Correct |
7 ms |
1828 KB |
Output is correct |
65 |
Correct |
4 ms |
1744 KB |
Output is correct |
66 |
Correct |
12 ms |
1852 KB |
Output is correct |
67 |
Correct |
5 ms |
1868 KB |
Output is correct |
68 |
Correct |
9 ms |
1832 KB |
Output is correct |
69 |
Correct |
7 ms |
1844 KB |
Output is correct |
70 |
Correct |
5 ms |
1828 KB |
Output is correct |
71 |
Correct |
34 ms |
1864 KB |
Output is correct |
72 |
Correct |
5 ms |
1868 KB |
Output is correct |
73 |
Correct |
34 ms |
1868 KB |
Output is correct |
74 |
Correct |
39 ms |
1872 KB |
Output is correct |
75 |
Correct |
8 ms |
1872 KB |
Output is correct |
76 |
Correct |
35 ms |
1872 KB |
Output is correct |
77 |
Correct |
38 ms |
1872 KB |
Output is correct |
78 |
Correct |
8 ms |
1864 KB |
Output is correct |
79 |
Correct |
26 ms |
1744 KB |
Output is correct |
80 |
Correct |
34 ms |
1872 KB |
Output is correct |
81 |
Correct |
37 ms |
1872 KB |
Output is correct |
82 |
Correct |
41 ms |
1868 KB |
Output is correct |
83 |
Correct |
7 ms |
1744 KB |
Output is correct |
84 |
Correct |
27 ms |
1868 KB |
Output is correct |
85 |
Correct |
33 ms |
1872 KB |
Output is correct |
86 |
Correct |
11 ms |
1864 KB |
Output is correct |
87 |
Correct |
7 ms |
1744 KB |
Output is correct |
88 |
Correct |
8 ms |
1744 KB |
Output is correct |
89 |
Correct |
10 ms |
1868 KB |
Output is correct |
90 |
Correct |
5 ms |
1872 KB |
Output is correct |
91 |
Correct |
10 ms |
1828 KB |
Output is correct |
92 |
Correct |
8 ms |
1868 KB |
Output is correct |
93 |
Partially correct |
38 ms |
1864 KB |
Partially correct - number of queries: 5068 |
94 |
Partially correct |
42 ms |
1744 KB |
Partially correct - number of queries: 5055 |
95 |
Correct |
31 ms |
1872 KB |
Output is correct |
96 |
Correct |
40 ms |
1864 KB |
Output is correct |
97 |
Correct |
30 ms |
1860 KB |
Output is correct |