#include "prize.h"
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 69;
vector <int> a;
int f[N], mx;
int n;
map <int, vector<int>> mp;
vector <int> to;
vector <int> ask2(int x){
if (mp.find(x) != mp.end()) return mp[x];
return mp[x] = ask(x);
}
void upd(int x){
x++;
for (int i = x; i <= n; i += i & (-i)) f[i]++;
}
int query(int x){
x++;
int res = 0;
for (int i = x; i; i -= i & (-i)) res += f[i];
return res;
}
void report(int x){
upd(a[x]);
a.erase(a.begin() + x);
}
int find(){
to.clear();
set <pair<int, int>> s1;
s1.insert({0, a.size() - 1});
// while (l != r){
// // cout << l << " " << r << "\n";
// int m = (l + r)/2;
// auto g = ask2(a[m]);
// int g1 = query(a[m]);
// // cout << m << " " << g[0] << " " << g1 << "\n";
// if (g[0] + g[1] < mx){
// if (g[0] + g[1] == 0) return a[m];
// report(m);
// return -1;
// }
// if (g1 < g[0]) {
// r = m - 1;
// } else {
// l = m + 1;
// }
// }
while (true){
bool good = true;
set <pair<int, int>> s2;
for (auto [x, y] : s1) {
if (x != y) good = false;
}
if (good) break;
for (auto [l, r] : s1){
if (l == r) continue;
int m = (l + r)/2;
auto g = ask2(a[m]);
int g1 = query(a[m]);
int g2 = query(n - 1) - query(a[m]);
if (g[0] + g[1] < mx){
if (g[0] + g[1] == 0) return a[m];
to.push_back(m);
}
if (g1 < g[0]) s2.insert({l, m - 1});
if (g2 < g[1]) s2.insert({m + 1, r});
}
s1 = s2;
}
// cout << l << " " << r << "\n";
for (auto [l, r] : s1){
auto g = ask2(a[l]);
if (g[0] + g[1] == 0) return a[l];
to.push_back(l);
}
sort(to.begin(), to.end(), greater<int>());
for (auto x : to) report(x);
return -1;
}
int find_best(int m) {
n = m;
vector<vector<int>> ok;
for (int i = 0; i < min(n, 500); i++){
auto get = ask2(i);
ok.push_back(get);
mx = max(mx, get[0] + get[1]);
}
// if (mx != 1){
// cout << n << "\n";
// for (auto x : ok){
// for (auto y : x) cout << y << " ";
// cout << "\n";
// }
// return 0;
// }
for (int i = 0; i < n; i++) a.push_back(i);
while (true){
int get = find();
if (get > -1) return get;
}
assert(false);
return 0;
}
// int main(){
// // int t; cin >> t;
// // while (t--){
// // int n = 3 + rand() % 10;
// // int x = rand() % n;
// // a.clear();
// // mx = 0;
// // for (int i = 0; i <= n; i++) f[i] = 0, a1[i] = a2[i] = 0;
// // for (int i = 0; i < x; i++) a2[i] = 1;
// // for (int i = x + 1; i < n; i++) a1[i] = 1;
// // int get = find_best(n);
// // if (get != x){
// // cout << n << " " << x << "\n";
// // return 0;
// // }
// // }
// int n; cin >> n;
// for (int i = 0; i < n; i++) cin >> a1[i];
// for (int i = 0; i < n; i++) cin >> a2[i];
// cout << find_best(n);
// return 0;
// }
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
1448 KB |
Output is correct |
2 |
Correct |
5 ms |
1500 KB |
Output is correct |
3 |
Correct |
4 ms |
1524 KB |
Output is correct |
4 |
Correct |
3 ms |
1512 KB |
Output is correct |
5 |
Correct |
7 ms |
1596 KB |
Output is correct |
6 |
Correct |
4 ms |
1512 KB |
Output is correct |
7 |
Correct |
5 ms |
1484 KB |
Output is correct |
8 |
Correct |
6 ms |
1536 KB |
Output is correct |
9 |
Correct |
5 ms |
1576 KB |
Output is correct |
10 |
Correct |
6 ms |
1484 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
1448 KB |
Output is correct |
2 |
Correct |
6 ms |
1484 KB |
Output is correct |
3 |
Correct |
6 ms |
1484 KB |
Output is correct |
4 |
Correct |
7 ms |
1472 KB |
Output is correct |
5 |
Correct |
3 ms |
1532 KB |
Output is correct |
6 |
Correct |
4 ms |
1508 KB |
Output is correct |
7 |
Correct |
4 ms |
1552 KB |
Output is correct |
8 |
Correct |
6 ms |
1456 KB |
Output is correct |
9 |
Correct |
8 ms |
1452 KB |
Output is correct |
10 |
Correct |
6 ms |
1484 KB |
Output is correct |
11 |
Incorrect |
53 ms |
2760 KB |
Incorrect |
12 |
Halted |
0 ms |
0 KB |
- |