/// In The Name Of God
#include <bits/stdc++.h>
#ifndef IOI2018
#include "prize.h"
#endif
#define f first
#define s second
#define pb push_back
#define pp pop_back
#define mp make_pair
#define sz(x) (int)x.size()
#define sqr(x) ((x) * 1ll * (x))
#define all(x) x.begin(), x.end()
#define rep(i, l, r) for (int i = (l); i < (r); i++)
#define per(i, l, r) for (int i = (l); i >= (r); i--)
#define Kazakhstan ios_base :: sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define nl '\n'
#define ioi exit(0);
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
const int N = (int)5e5 + 7;
const int inf = (int)1e9 + 7;
const int mod = (int)1e9 + 7;
const ll linf = (ll)1e18 + 7;
const int dx[] = {-1, 0, 1, 0, 1, -1, -1, 1};
const int dy[] = {0, 1, 0, -1, 1, -1, 1, -1};
using namespace std;
namespace com {
static const int max_q = 5000;
static int n;
static int query_count = 0;
static vector<int> g;
static vector<vector<int> > rank_count;
}
namespace grader {
using namespace com;
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;
}
}
#ifdef IOI2018
using namespace grader;
#endif
const int K = 500;
map <int, vector <int > > dp;
vector <int> get(int p) {
if (dp.count(p)) return dp[p];
return dp[p] = ask(p);
}
int mx;
int tin;
/// (n - 1) / k + cnt *
int solve(int p, int n) {
if (p >= n) assert(0);
{
while (1) {
vector <int> now = get(p);
if (now[0] + now[1] == 0) return p;
if (now[0] + now[1] == mx) break;
p++;
}
}
/*
vector <int> _c = get(p);
for (auto it : dp) {
if (it.f >= p && (it.s[0] == 0 || it.s[0] == _c[0])) {
p = it.f;
}
}*/
int l = p / K + 1, r = (n - 1) / K, res = p / K;
vector <int> cur = get(p);
while (l <= r) {
int mid = l + r >> 1;
vector <int> now = get(K * mid);
if (now[0] + now[1] == 0) return K * mid;
if (cur == now) res = mid, l = mid + 1;
else r = mid - 1;
}
//cerr << p << ' ' << res * K << nl;
l = max(p, res * K);
r = min(n - 1, (res + 1) * K - 1);
int nxt = -1;
while (l <= r) {
int mid = l + r >> 1;
vector <int> now = get(mid);
if (now[0] + now[1] == 0) return mid;
if (cur == now) nxt = mid, l = mid + 1;
else r = mid - 1;
}
++nxt;
return solve(nxt, n);
}
int find_best(int n) {
int id = -1;
int cnt = min(n, 473), done = 0;
per(i, n - 1, 0) {
vector <int> cur = ask(i);
if (!max(cur[0], cur[1])) {
return i;
}
mx = cur[0] + cur[1];
++done;
if (done == cnt) break;
}
return solve(0, n);
}
namespace read {
using namespace com;
void read() {
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;
ioi
cout << res << endl << "Query count: " << query_count << endl;
ioi
}
}
#ifdef IOI2018
int main() {
#ifdef IOI2018
freopen ("in.txt", "r", stdin);
freopen ("D.out", "w", stdout);
#endif
read :: read();
}
#endif
Compilation message
prize.cpp: In function 'int solve(int, int)':
prize.cpp:106:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int mid = l + r >> 1;
~~^~~
prize.cpp:117:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int mid = l + r >> 1;
~~^~~
prize.cpp: In function 'int find_best(int)':
prize.cpp:127:6: warning: unused variable 'id' [-Wunused-variable]
int id = -1;
^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
384 KB |
Output is correct |
2 |
Correct |
6 ms |
256 KB |
Output is correct |
3 |
Correct |
8 ms |
304 KB |
Output is correct |
4 |
Correct |
2 ms |
256 KB |
Output is correct |
5 |
Correct |
8 ms |
256 KB |
Output is correct |
6 |
Correct |
6 ms |
256 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
7 ms |
256 KB |
Output is correct |
9 |
Correct |
5 ms |
256 KB |
Output is correct |
10 |
Correct |
6 ms |
256 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
384 KB |
Output is correct |
2 |
Correct |
6 ms |
256 KB |
Output is correct |
3 |
Correct |
7 ms |
256 KB |
Output is correct |
4 |
Correct |
3 ms |
384 KB |
Output is correct |
5 |
Correct |
7 ms |
384 KB |
Output is correct |
6 |
Correct |
4 ms |
512 KB |
Output is correct |
7 |
Correct |
8 ms |
384 KB |
Output is correct |
8 |
Correct |
4 ms |
404 KB |
Output is correct |
9 |
Correct |
6 ms |
256 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
8 ms |
392 KB |
Output is correct |
12 |
Correct |
4 ms |
308 KB |
Output is correct |
13 |
Correct |
5 ms |
388 KB |
Output is correct |
14 |
Correct |
7 ms |
412 KB |
Output is correct |
15 |
Correct |
14 ms |
404 KB |
Output is correct |
16 |
Correct |
33 ms |
820 KB |
Output is correct |
17 |
Correct |
7 ms |
256 KB |
Output is correct |
18 |
Correct |
6 ms |
384 KB |
Output is correct |
19 |
Correct |
6 ms |
392 KB |
Output is correct |
20 |
Correct |
16 ms |
376 KB |
Output is correct |
21 |
Incorrect |
47 ms |
1556 KB |
Incorrect |
22 |
Halted |
0 ms |
0 KB |
- |