#ifndef HOME
#include "prize.h"
#endif
#include <bits/stdc++.h>
using namespace std;
#ifdef HOME
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
static const int max_q = 10000;
static int n;
static int query_count = 0;
static vector<int> g;
static vector<vector<int> > rank_count;
vector<int> ask(int i) {
query_count++;
if(query_count > max_q) {
cerr << "Query limit exceeded" << endl;
exit(0);
}
if(i < 0 || i >= n) {
cerr << "Bad index: " << i << endl;
exit(0);
}
vector<int> res(2);
res[0] = rank_count[g[i] - 1][i + 1];
res[1] = rank_count[g[i] - 1][n] - res[0];
return res;
}
#endif
void solve(int le, int ri, vector<int> lef, vector<int> rig, int &ans) {
if (ans != -1) {
return; }
if (lef[0] + lef[1] == 0) {
ans = le; return; }
if (rig[0] + rig[1] == 0) {
ans = ri; return; }
if (le >= ri - 1) {
return; }
if (lef[0] + lef[1] == rig[0] + rig[1] and lef[0] == rig[0]) {
return; }
if (lef[0] + lef[1] < rig[0] + rig[1]) {
solve(le + 1, ri, ask(le + 1), rig, ans); return; }
if (lef[0] + lef[1] > rig[0] + rig[1]) {
solve(le, ri - 1, lef, ask(ri - 1), ans); return; }
int md = (le + ri) >> 1;
vector<int> aux = ask(md);
solve(le, md, lef, aux, ans);
solve(md, ri, aux, rig, ans); }
int find_best(int n) {
int ans = -1;
solve(0, n - 1, ask(0), ask(n - 1), ans);
return ans; }
#ifdef HOME
int main() {
freopen("prize.in", "r", stdin);
freopen("prize.out", "w", stdout);
cin >> n;
g.resize(n);
for(int i = 0; i < n; i++) {
cin >> g[i];
if(g[i] < 1) {
cerr << "Invalid rank " << g[i] << " at index " << i << endl;
exit(0);
}
}
int max_rank = *max_element(g.begin(), g.end());
rank_count.resize(max_rank + 1, vector<int>(n + 1, 0));
for(int r = 0; r <= max_rank; r++) {
for(int i = 1; i <= n; i++) {
rank_count[r][i] = rank_count[r][i - 1];
if(g[i - 1] == r)
rank_count[r][i]++;
}
}
for(int i = 0; i <= n; i++)
for(int r = 1; r <= max_rank; r++)
rank_count[r][i] += rank_count[r - 1][i];
int res = find_best(n);
cout << res << endl << "Query count: " << query_count << endl;
return 0;
}
#endif
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
384 KB |
Output is correct |
2 |
Correct |
3 ms |
400 KB |
Output is correct |
3 |
Correct |
2 ms |
256 KB |
Output is correct |
4 |
Correct |
3 ms |
256 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
256 KB |
Output is correct |
7 |
Correct |
2 ms |
404 KB |
Output is correct |
8 |
Correct |
2 ms |
308 KB |
Output is correct |
9 |
Correct |
2 ms |
256 KB |
Output is correct |
10 |
Correct |
3 ms |
256 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
256 KB |
Output is correct |
2 |
Correct |
2 ms |
532 KB |
Output is correct |
3 |
Correct |
3 ms |
376 KB |
Output is correct |
4 |
Correct |
3 ms |
384 KB |
Output is correct |
5 |
Correct |
3 ms |
256 KB |
Output is correct |
6 |
Correct |
3 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
512 KB |
Output is correct |
8 |
Correct |
3 ms |
256 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
256 KB |
Output is correct |
11 |
Correct |
4 ms |
384 KB |
Output is correct |
12 |
Correct |
2 ms |
384 KB |
Output is correct |
13 |
Correct |
3 ms |
316 KB |
Output is correct |
14 |
Correct |
3 ms |
256 KB |
Output is correct |
15 |
Correct |
7 ms |
256 KB |
Output is correct |
16 |
Correct |
30 ms |
256 KB |
Output is correct |
17 |
Correct |
3 ms |
256 KB |
Output is correct |
18 |
Correct |
35 ms |
256 KB |
Output is correct |
19 |
Correct |
3 ms |
312 KB |
Output is correct |
20 |
Correct |
11 ms |
384 KB |
Output is correct |
21 |
Correct |
13 ms |
256 KB |
Output is correct |
22 |
Correct |
6 ms |
256 KB |
Output is correct |
23 |
Correct |
2 ms |
384 KB |
Output is correct |
24 |
Correct |
2 ms |
256 KB |
Output is correct |
25 |
Correct |
27 ms |
308 KB |
Output is correct |
26 |
Correct |
19 ms |
304 KB |
Output is correct |
27 |
Correct |
3 ms |
256 KB |
Output is correct |
28 |
Correct |
36 ms |
256 KB |
Output is correct |
29 |
Correct |
31 ms |
256 KB |
Output is correct |
30 |
Correct |
22 ms |
384 KB |
Output is correct |
31 |
Correct |
3 ms |
256 KB |
Output is correct |
32 |
Correct |
5 ms |
256 KB |
Output is correct |
33 |
Correct |
2 ms |
384 KB |
Output is correct |
34 |
Correct |
13 ms |
384 KB |
Output is correct |
35 |
Correct |
4 ms |
256 KB |
Output is correct |
36 |
Correct |
13 ms |
256 KB |
Output is correct |
37 |
Correct |
2 ms |
392 KB |
Output is correct |
38 |
Correct |
3 ms |
508 KB |
Output is correct |
39 |
Correct |
17 ms |
256 KB |
Output is correct |
40 |
Correct |
24 ms |
396 KB |
Output is correct |
41 |
Correct |
12 ms |
396 KB |
Output is correct |
42 |
Correct |
11 ms |
376 KB |
Output is correct |
43 |
Correct |
21 ms |
256 KB |
Output is correct |
44 |
Correct |
9 ms |
384 KB |
Output is correct |
45 |
Correct |
14 ms |
256 KB |
Output is correct |
46 |
Correct |
3 ms |
384 KB |
Output is correct |
47 |
Correct |
15 ms |
256 KB |
Output is correct |
48 |
Correct |
16 ms |
392 KB |
Output is correct |
49 |
Correct |
3 ms |
412 KB |
Output is correct |
50 |
Correct |
34 ms |
384 KB |
Output is correct |
51 |
Correct |
15 ms |
256 KB |
Output is correct |
52 |
Correct |
2 ms |
256 KB |
Output is correct |
53 |
Correct |
3 ms |
384 KB |
Output is correct |
54 |
Correct |
25 ms |
384 KB |
Output is correct |
55 |
Correct |
2 ms |
384 KB |
Output is correct |
56 |
Correct |
32 ms |
384 KB |
Output is correct |
57 |
Correct |
21 ms |
384 KB |
Output is correct |
58 |
Correct |
28 ms |
256 KB |
Output is correct |
59 |
Correct |
23 ms |
256 KB |
Output is correct |
60 |
Correct |
20 ms |
384 KB |
Output is correct |
61 |
Correct |
2 ms |
308 KB |
Output is correct |
62 |
Correct |
3 ms |
256 KB |
Output is correct |
63 |
Correct |
3 ms |
248 KB |
Output is correct |
64 |
Correct |
3 ms |
404 KB |
Output is correct |
65 |
Correct |
2 ms |
384 KB |
Output is correct |
66 |
Correct |
2 ms |
256 KB |
Output is correct |
67 |
Correct |
6 ms |
384 KB |
Output is correct |
68 |
Correct |
5 ms |
308 KB |
Output is correct |
69 |
Correct |
3 ms |
384 KB |
Output is correct |
70 |
Correct |
4 ms |
384 KB |
Output is correct |
71 |
Correct |
18 ms |
384 KB |
Output is correct |
72 |
Correct |
8 ms |
256 KB |
Output is correct |
73 |
Correct |
45 ms |
384 KB |
Output is correct |
74 |
Correct |
30 ms |
256 KB |
Output is correct |
75 |
Correct |
3 ms |
256 KB |
Output is correct |
76 |
Correct |
31 ms |
384 KB |
Output is correct |
77 |
Correct |
36 ms |
256 KB |
Output is correct |
78 |
Correct |
6 ms |
256 KB |
Output is correct |
79 |
Correct |
19 ms |
256 KB |
Output is correct |
80 |
Correct |
36 ms |
256 KB |
Output is correct |
81 |
Correct |
45 ms |
256 KB |
Output is correct |
82 |
Correct |
36 ms |
256 KB |
Output is correct |
83 |
Correct |
3 ms |
384 KB |
Output is correct |
84 |
Correct |
25 ms |
512 KB |
Output is correct |
85 |
Correct |
45 ms |
256 KB |
Output is correct |
86 |
Correct |
5 ms |
256 KB |
Output is correct |
87 |
Correct |
2 ms |
256 KB |
Output is correct |
88 |
Correct |
5 ms |
384 KB |
Output is correct |
89 |
Correct |
4 ms |
384 KB |
Output is correct |
90 |
Correct |
2 ms |
384 KB |
Output is correct |
91 |
Correct |
3 ms |
512 KB |
Output is correct |
92 |
Correct |
2 ms |
384 KB |
Output is correct |
93 |
Correct |
4 ms |
256 KB |
Output is correct |
94 |
Correct |
4 ms |
384 KB |
Output is correct |
95 |
Correct |
5 ms |
384 KB |
Output is correct |
96 |
Correct |
5 ms |
256 KB |
Output is correct |
97 |
Correct |
3 ms |
384 KB |
Output is correct |