/*************************************
* author: marvinthang *
* created: 25.06.2023 08:50:11 *
*************************************/
#include "prize.h"
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define left ___left
#define right ___right
#define TIME (1.0 * clock() / CLOCKS_PER_SEC)
#define MASK(i) (1LL << (i))
#define BIT(x, i) ((x) >> (i) & 1)
#define __builtin_popcount __builtin_popcountll
#define ALL(v) (v).begin(), (v).end()
#define REP(i, n) for (int i = 0, _n = (n); i < _n; ++i)
#define REPD(i, n) for (int i = (n); i--; )
#define FOR(i, a, b) for (int i = (a), _b = (b); i < _b; ++i)
#define FORD(i, b, a) for (int i = (b), _a = (a); --i >= _a; )
#define FORE(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i)
#define FORDE(i, b, a) for (int i = (b), _a = (a); i >= _a; --i)
#define scan_op(...) istream & operator >> (istream &in, __VA_ARGS__ &u)
#define print_op(...) ostream & operator << (ostream &out, const __VA_ARGS__ &u)
#ifdef LOCAL
#include "debug.h"
#else
#define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
#define DB(...) 23
#define db(...) 23
#define debug(...) 23
#endif
template <class U, class V> scan_op(pair <U, V>) { return in >> u.first >> u.second; }
template <class T> scan_op(vector <T>) { for (size_t i = 0; i < u.size(); ++i) in >> u[i]; return in; }
template <class U, class V> print_op(pair <U, V>) { return out << '(' << u.first << ", " << u.second << ')'; }
template <size_t i, class T> ostream & print_tuple_utils(ostream &out, const T &tup) { if constexpr(i == tuple_size<T>::value) return out << ")"; else return print_tuple_utils<i + 1, T>(out << (i ? ", " : "(") << get<i>(tup), tup); }
template <class ...U> print_op(tuple<U...>) { return print_tuple_utils<0, tuple<U...>>(out, u); }
template <class Con, class = decltype(begin(declval<Con>()))> typename enable_if <!is_same<Con, string>::value, ostream&>::type operator << (ostream &out, const Con &con) { out << '{'; for (__typeof(con.begin()) it = con.begin(); it != con.end(); ++it) out << (it == con.begin() ? "" : ", ") << *it; return out << '}'; }
template <class A, class B> bool minimize(A &a, B b) { if (a > b) { a = b; return true; } return false; }
template <class A, class B> bool maximize(A &a, B b) { if (a < b) { a = b; return true; } return false; }
// end of template
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template <class T> T rand(T l, T h) { return uniform_int_distribution <T> (l, h) (rng); }
template <class T> T rand(T h) { return uniform_int_distribution <T> (0, h - 1) (rng); }
int res;
vector <set <int>> ss;
vector <int> cnt_left;
void solve(int l, int r) {
if (~res || l > r) return;
int m = l + r >> 1;
vector <int> a = ask(m);
cnt_left[m] = a[0];
int sum = a[0] + a[1];
if (!sum) {
res = m;
return;
}
auto it = ss[sum].insert(m).fi;
if (it == ss[sum].begin()) {
if (a[0]) solve(l, m - 1);
} else {
if (cnt_left[*prev(it)] < a[0]) solve(l, m - 1);
}
if (~res) return;
if (++it != ss[sum].end()) {
if (cnt_left[*it] > a[0]) solve(m + 1, r);
} else {
if (a[1]) solve(m + 1, r);
}
}
int find_best(int n) {
res = -1;
ss.assign(n, set<int>());
cnt_left.assign(n, -1);
solve(0, n - 1);
return res;
}
#ifdef LOCAL
#include "prize.h"
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
static const int max_q = 5000;
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;
}
int main() {
file("prize");
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);
if (g[res] != 1) return !(cout << "WA!\n");
cout << res << endl << "Query count: " << query_count << endl;
return 0;
}
#endif
Compilation message
prize.cpp: In function 'void solve(int, int)':
prize.cpp:58:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
58 | int m = l + r >> 1;
| ~~^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
10412 KB |
Output is correct |
2 |
Correct |
5 ms |
10404 KB |
Output is correct |
3 |
Correct |
6 ms |
10404 KB |
Output is correct |
4 |
Correct |
5 ms |
10448 KB |
Output is correct |
5 |
Correct |
5 ms |
10448 KB |
Output is correct |
6 |
Correct |
5 ms |
10448 KB |
Output is correct |
7 |
Correct |
5 ms |
10408 KB |
Output is correct |
8 |
Correct |
5 ms |
10480 KB |
Output is correct |
9 |
Correct |
5 ms |
10408 KB |
Output is correct |
10 |
Correct |
5 ms |
10448 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
10448 KB |
Output is correct |
2 |
Correct |
6 ms |
10448 KB |
Output is correct |
3 |
Correct |
5 ms |
10540 KB |
Output is correct |
4 |
Correct |
5 ms |
10448 KB |
Output is correct |
5 |
Correct |
5 ms |
10448 KB |
Output is correct |
6 |
Correct |
5 ms |
10412 KB |
Output is correct |
7 |
Correct |
5 ms |
10408 KB |
Output is correct |
8 |
Correct |
6 ms |
10448 KB |
Output is correct |
9 |
Correct |
5 ms |
10448 KB |
Output is correct |
10 |
Correct |
5 ms |
10448 KB |
Output is correct |
11 |
Correct |
6 ms |
10504 KB |
Output is correct |
12 |
Correct |
5 ms |
10404 KB |
Output is correct |
13 |
Correct |
8 ms |
10448 KB |
Output is correct |
14 |
Correct |
3 ms |
1360 KB |
Output is correct |
15 |
Correct |
10 ms |
10456 KB |
Output is correct |
16 |
Correct |
37 ms |
10584 KB |
Output is correct |
17 |
Correct |
5 ms |
10412 KB |
Output is correct |
18 |
Correct |
21 ms |
10700 KB |
Output is correct |
19 |
Correct |
6 ms |
10408 KB |
Output is correct |
20 |
Correct |
7 ms |
5456 KB |
Output is correct |
21 |
Correct |
16 ms |
10528 KB |
Output is correct |
22 |
Correct |
7 ms |
10448 KB |
Output is correct |
23 |
Correct |
5 ms |
10448 KB |
Output is correct |
24 |
Correct |
6 ms |
10408 KB |
Output is correct |
25 |
Correct |
26 ms |
10568 KB |
Output is correct |
26 |
Correct |
21 ms |
10564 KB |
Output is correct |
27 |
Correct |
5 ms |
10448 KB |
Output is correct |
28 |
Correct |
32 ms |
10608 KB |
Output is correct |
29 |
Correct |
17 ms |
10676 KB |
Output is correct |
30 |
Correct |
47 ms |
10656 KB |
Output is correct |
31 |
Correct |
5 ms |
10404 KB |
Output is correct |
32 |
Correct |
7 ms |
10508 KB |
Output is correct |
33 |
Correct |
0 ms |
208 KB |
Output is correct |
34 |
Correct |
10 ms |
10528 KB |
Output is correct |
35 |
Correct |
6 ms |
10408 KB |
Output is correct |
36 |
Correct |
9 ms |
10524 KB |
Output is correct |
37 |
Correct |
5 ms |
10500 KB |
Output is correct |
38 |
Correct |
7 ms |
10484 KB |
Output is correct |
39 |
Correct |
12 ms |
10572 KB |
Output is correct |
40 |
Correct |
20 ms |
10656 KB |
Output is correct |
41 |
Correct |
29 ms |
10568 KB |
Output is correct |
42 |
Correct |
15 ms |
10600 KB |
Output is correct |
43 |
Correct |
24 ms |
10568 KB |
Output is correct |
44 |
Correct |
17 ms |
10532 KB |
Output is correct |
45 |
Correct |
14 ms |
10512 KB |
Output is correct |
46 |
Correct |
5 ms |
10388 KB |
Output is correct |
47 |
Correct |
21 ms |
10496 KB |
Output is correct |
48 |
Correct |
32 ms |
10616 KB |
Output is correct |
49 |
Correct |
8 ms |
10428 KB |
Output is correct |
50 |
Correct |
45 ms |
10576 KB |
Output is correct |
51 |
Correct |
20 ms |
10508 KB |
Output is correct |
52 |
Correct |
6 ms |
10420 KB |
Output is correct |
53 |
Correct |
6 ms |
10500 KB |
Output is correct |
54 |
Correct |
12 ms |
10552 KB |
Output is correct |
55 |
Correct |
5 ms |
10456 KB |
Output is correct |
56 |
Correct |
21 ms |
10688 KB |
Output is correct |
57 |
Correct |
31 ms |
10632 KB |
Output is correct |
58 |
Correct |
25 ms |
10568 KB |
Output is correct |
59 |
Correct |
27 ms |
10528 KB |
Output is correct |
60 |
Correct |
27 ms |
10528 KB |
Output is correct |
61 |
Correct |
6 ms |
10408 KB |
Output is correct |
62 |
Correct |
6 ms |
10396 KB |
Output is correct |
63 |
Correct |
6 ms |
10500 KB |
Output is correct |
64 |
Correct |
6 ms |
10496 KB |
Output is correct |
65 |
Correct |
5 ms |
10476 KB |
Output is correct |
66 |
Correct |
5 ms |
10408 KB |
Output is correct |
67 |
Correct |
5 ms |
10468 KB |
Output is correct |
68 |
Correct |
5 ms |
10408 KB |
Output is correct |
69 |
Correct |
5 ms |
10448 KB |
Output is correct |
70 |
Correct |
5 ms |
10448 KB |
Output is correct |
71 |
Correct |
35 ms |
10864 KB |
Output is correct |
72 |
Correct |
9 ms |
10512 KB |
Output is correct |
73 |
Correct |
47 ms |
10656 KB |
Output is correct |
74 |
Correct |
39 ms |
10644 KB |
Output is correct |
75 |
Correct |
6 ms |
10448 KB |
Output is correct |
76 |
Correct |
39 ms |
10580 KB |
Output is correct |
77 |
Correct |
52 ms |
10640 KB |
Output is correct |
78 |
Correct |
10 ms |
10500 KB |
Output is correct |
79 |
Correct |
13 ms |
10548 KB |
Output is correct |
80 |
Correct |
36 ms |
10648 KB |
Output is correct |
81 |
Correct |
45 ms |
10656 KB |
Output is correct |
82 |
Correct |
41 ms |
10568 KB |
Output is correct |
83 |
Correct |
6 ms |
10448 KB |
Output is correct |
84 |
Correct |
38 ms |
10568 KB |
Output is correct |
85 |
Correct |
50 ms |
10724 KB |
Output is correct |
86 |
Correct |
9 ms |
10408 KB |
Output is correct |
87 |
Correct |
6 ms |
10496 KB |
Output is correct |
88 |
Correct |
10 ms |
10504 KB |
Output is correct |
89 |
Correct |
9 ms |
10516 KB |
Output is correct |
90 |
Correct |
5 ms |
10536 KB |
Output is correct |
91 |
Correct |
8 ms |
10448 KB |
Output is correct |
92 |
Correct |
6 ms |
10448 KB |
Output is correct |
93 |
Correct |
8 ms |
10448 KB |
Output is correct |
94 |
Correct |
8 ms |
10504 KB |
Output is correct |
95 |
Correct |
6 ms |
10448 KB |
Output is correct |
96 |
Correct |
6 ms |
10508 KB |
Output is correct |
97 |
Correct |
5 ms |
10448 KB |
Output is correct |