#include "prize.h"
#include <bits/stdc++.h>
using namespace std;
const int MAX = 2e5 + 5;
int tree[MAX];
void update(int pos, int val){
while(pos < MAX){
tree[pos] += val;
pos += pos & -pos;
}
}
int ask(int l, int r){
if(r < l) return 0;
if(l != 1) return ask(1, r) - ask(1, l - 1);
int ans = 0;
while(r > 0){
ans += tree[r];
r -= r & -r;
}
return ans;
}
int intRand(){
return (rand() << 14) | rand();
}
vector<int> mp[MAX];
vector<int> query(int i){
if(!mp[i].empty()) return mp[i];
return mp[i] = ask(i - 1);
}
int find_best(int n) {
vector<int> order(n);
srand(time(0));
iota(order.begin(), order.end(), 1);
random_shuffle(order.begin(), order.end());
vector<int> a;
int cnt = 0;
for (int j = 0; j < min(1000, (int)order.size()); ++j)
{
int i = order[j];
a = query(i);
if(a[0] + a[1] == 0) return i - 1;
if(a[0] + a[1] < cnt){
update(i, 1);
}
else{
cnt = a[0] + a[1];
}
}
for(int j = 0; j <= cnt; ++j){
int l = 1, r = n;
while(l <= r){
int mid = (l + r) / 2;
a = query(mid);
if(a[0] + a[1] == 0){
return mid - 1;
}
if(a[0] + a[1] < cnt && !ask(mid, mid)){
update(mid, 1);
break;
}
a[0] -= ask(1, mid - 1);
a[1] -= ask(mid + 1, n);
if(a[1] > 0){
l = mid + 1;
}
else{
r = mid - 1;
}
}
}
}
Compilation message
prize.cpp: In function 'int find_best(int)':
prize.cpp:41:24: warning: control reaches end of non-void function [-Wreturn-type]
41 | vector<int> order(n);
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
5760 KB |
Output is correct |
2 |
Correct |
8 ms |
5816 KB |
Output is correct |
3 |
Correct |
9 ms |
5816 KB |
Output is correct |
4 |
Correct |
9 ms |
5892 KB |
Output is correct |
5 |
Correct |
11 ms |
5764 KB |
Output is correct |
6 |
Correct |
13 ms |
5764 KB |
Output is correct |
7 |
Correct |
9 ms |
5828 KB |
Output is correct |
8 |
Correct |
13 ms |
5760 KB |
Output is correct |
9 |
Correct |
12 ms |
5804 KB |
Output is correct |
10 |
Correct |
11 ms |
5840 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
5748 KB |
Output is correct |
2 |
Correct |
9 ms |
5816 KB |
Output is correct |
3 |
Correct |
8 ms |
5892 KB |
Output is correct |
4 |
Correct |
12 ms |
5808 KB |
Output is correct |
5 |
Correct |
13 ms |
5804 KB |
Output is correct |
6 |
Correct |
14 ms |
5712 KB |
Output is correct |
7 |
Correct |
9 ms |
5892 KB |
Output is correct |
8 |
Correct |
13 ms |
5768 KB |
Output is correct |
9 |
Correct |
13 ms |
5788 KB |
Output is correct |
10 |
Correct |
17 ms |
5796 KB |
Output is correct |
11 |
Correct |
14 ms |
6008 KB |
Output is correct |
12 |
Correct |
10 ms |
6176 KB |
Output is correct |
13 |
Correct |
11 ms |
5768 KB |
Output is correct |
14 |
Execution timed out |
3061 ms |
5084 KB |
Time limit exceeded |
15 |
Halted |
0 ms |
0 KB |
- |