#include "prize.h"
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
//#define int long long
//#pragma GCC optimize("Ofast")
//#pragma comment(linker, "/stack:200000000")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4")
#define file(s) freopen(s".in","r",stdin); freopen(s".out","w",stdout);
#define fastio ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define all(x) x.begin(), x.end()
#define sz(s) (int)s.size()
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define s second
#define f first
typedef pair < long long, long long > pll;
typedef pair < int, int > pii;
typedef unsigned long long ull;
typedef vector < pii > vpii;
typedef vector < int > vi;
typedef long double ldb;
typedef long long ll;
typedef double db;
typedef tree < int, null_type, less < int >, rb_tree_tag, tree_order_statistics_node_update > ordered_set;
const int inf = 1e9, maxn = 2e5 + 48, mod = 998244353, N = 2e5 + 12;
const int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1}, block = 300;
const pii base = mp(1171, 3307), Mod = mp(1e9 + 7, 1e9 + 9);
const db eps = 1e-12, pi = acos(-1);
const ll INF = 1e18;
int t[N << 2], n, T[N];
bool was[N];
pii tt[N];
void update (int pos, int x, int v = 1, int tl = 0, int tr = n - 1) {
t[v] += x;
if (tl == tr)
return;
int tm = (tl + tr) >> 1;
if (pos <= tm)
update(pos, x, v << 1, tl, tm);
else
update(pos, x, v << 1 | 1, tm + 1, tr);
}
int p[N];
int get2 (int k, int v = 1, int tl = 0, int tr = n - 1) {
if (tl == tr)
return tl;
int tm = (tl + tr) >> 1;
if (t[v << 1] < k)
return get2(k - t[v << 1], v << 1 | 1, tm + 1, tr);
return get2(k, v << 1, tl, tm);
}
pii get (int pos) {
if (was[pos])
return tt[pos];
was[pos] = 1;
vi v = ask(pos);
return tt[pos] = mp(v[0], v[1]);
}
void inc (int pos) {
while (pos < n) {
++T[pos];
pos |= pos + 1;
}
}
int get1 (int r) {
int res = 0;
while (r >= 0) {
res += T[r];
r = (r & (r + 1)) - 1;
}
return res;
}
int find_best(int _n) {
n = _n;
srand(time(0));
for (int i = 0; i < n; ++i)
update(i, 1);
int m = min(450, n);
for (int i = 0; i < n; ++i)
p[i] = i;
random_shuffle(p, p + n);
reverse(p, p + n);
vector < pii > V;
for (int i = 0; i < m; ++i) {
V.pb({get(p[i]).f + get(p[i]).s, p[i]});
if ((get(p[i]).f + get(p[i]).s) * 1ll * (get(p[i]).f + get(p[i]).s) >= n)
break;
}
sort(all(V));
if (!V[0].f)
return V[0].s;
int cnt = V.back().f;
set < int > q;
for (auto x : V) {
q.insert(x.s);
if (x.f != V.back().f) {
update(x.s, -1);
inc(x.s);
--cnt;
}
}
int l = 1;
while (cnt) {
int r = t[1];
int mxPos = inf, mnPos = -1;
for (auto x : q)
if (get(x).f > get1(x - 1))
mxPos = min(mxPos, x);
else if (get(x).f == get1(x))
mnPos = max(mnPos, x);
while (l <= r) {
int mid = (l + r) >> 1;
int pos = get2(mid);
if (pos > mxPos) {
r = mid - 1;
continue;
}
if (pos < mnPos) {
l = mid + 1;
continue;
}
q.insert(pos);
if (get(pos).f + get(pos).s != V.back().f) {
cnt--;
update(pos, -1);
inc(pos);
if (get(pos).f + get(pos).s == 0)
return pos;
break;
}
if (get(pos).f != get1(pos))
r = mid - 1;
else
l = mid + 1;
}
}
return 0;
}
Compilation message
prize.cpp: In function 'int find_best(int)':
prize.cpp:111:5: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
111 | for (int i = 0; i < n; ++i)
| ^~~
prize.cpp:113:2: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
113 | random_shuffle(p, p + n);
| ^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
4344 KB |
Output is correct |
2 |
Correct |
19 ms |
4416 KB |
Output is correct |
3 |
Correct |
23 ms |
4472 KB |
Output is correct |
4 |
Correct |
20 ms |
4408 KB |
Output is correct |
5 |
Correct |
21 ms |
4472 KB |
Output is correct |
6 |
Correct |
20 ms |
4480 KB |
Output is correct |
7 |
Correct |
19 ms |
4408 KB |
Output is correct |
8 |
Correct |
20 ms |
4408 KB |
Output is correct |
9 |
Correct |
21 ms |
4472 KB |
Output is correct |
10 |
Correct |
23 ms |
4480 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
4516 KB |
Output is correct |
2 |
Correct |
21 ms |
4472 KB |
Output is correct |
3 |
Correct |
22 ms |
4472 KB |
Output is correct |
4 |
Correct |
21 ms |
4352 KB |
Output is correct |
5 |
Correct |
20 ms |
4344 KB |
Output is correct |
6 |
Correct |
21 ms |
4472 KB |
Output is correct |
7 |
Correct |
21 ms |
4472 KB |
Output is correct |
8 |
Correct |
21 ms |
4344 KB |
Output is correct |
9 |
Correct |
20 ms |
4480 KB |
Output is correct |
10 |
Correct |
20 ms |
4480 KB |
Output is correct |
11 |
Correct |
23 ms |
4728 KB |
Output is correct |
12 |
Correct |
23 ms |
4472 KB |
Output is correct |
13 |
Correct |
25 ms |
4856 KB |
Output is correct |
14 |
Correct |
8 ms |
896 KB |
Output is correct |
15 |
Correct |
29 ms |
4728 KB |
Output is correct |
16 |
Correct |
87 ms |
5496 KB |
Output is correct |
17 |
Correct |
16 ms |
3320 KB |
Output is correct |
18 |
Correct |
109 ms |
5852 KB |
Output is correct |
19 |
Correct |
21 ms |
4472 KB |
Output is correct |
20 |
Correct |
17 ms |
2264 KB |
Output is correct |
21 |
Correct |
39 ms |
4284 KB |
Output is correct |
22 |
Correct |
22 ms |
4580 KB |
Output is correct |
23 |
Correct |
19 ms |
4396 KB |
Output is correct |
24 |
Correct |
21 ms |
4472 KB |
Output is correct |
25 |
Correct |
55 ms |
4716 KB |
Output is correct |
26 |
Correct |
51 ms |
4600 KB |
Output is correct |
27 |
Correct |
22 ms |
4444 KB |
Output is correct |
28 |
Correct |
94 ms |
5672 KB |
Output is correct |
29 |
Correct |
77 ms |
5624 KB |
Output is correct |
30 |
Correct |
112 ms |
5880 KB |
Output is correct |
31 |
Correct |
16 ms |
3200 KB |
Output is correct |
32 |
Correct |
24 ms |
4728 KB |
Output is correct |
33 |
Correct |
1 ms |
384 KB |
Output is correct |
34 |
Correct |
35 ms |
4088 KB |
Output is correct |
35 |
Correct |
25 ms |
4600 KB |
Output is correct |
36 |
Correct |
28 ms |
3832 KB |
Output is correct |
37 |
Correct |
21 ms |
4608 KB |
Output is correct |
38 |
Correct |
20 ms |
4480 KB |
Output is correct |
39 |
Correct |
44 ms |
4476 KB |
Output is correct |
40 |
Correct |
95 ms |
5880 KB |
Output is correct |
41 |
Correct |
57 ms |
4728 KB |
Output is correct |
42 |
Correct |
58 ms |
4728 KB |
Output is correct |
43 |
Correct |
49 ms |
4648 KB |
Output is correct |
44 |
Correct |
38 ms |
4484 KB |
Output is correct |
45 |
Correct |
32 ms |
3964 KB |
Output is correct |
46 |
Correct |
16 ms |
3196 KB |
Output is correct |
47 |
Correct |
37 ms |
4368 KB |
Output is correct |
48 |
Correct |
74 ms |
5244 KB |
Output is correct |
49 |
Correct |
19 ms |
3448 KB |
Output is correct |
50 |
Correct |
109 ms |
5840 KB |
Output is correct |
51 |
Correct |
45 ms |
4216 KB |
Output is correct |
52 |
Correct |
16 ms |
3192 KB |
Output is correct |
53 |
Correct |
22 ms |
4600 KB |
Output is correct |
54 |
Correct |
42 ms |
4344 KB |
Output is correct |
55 |
Correct |
16 ms |
3200 KB |
Output is correct |
56 |
Correct |
95 ms |
5896 KB |
Output is correct |
57 |
Correct |
60 ms |
5172 KB |
Output is correct |
58 |
Correct |
75 ms |
5112 KB |
Output is correct |
59 |
Correct |
58 ms |
4732 KB |
Output is correct |
60 |
Correct |
66 ms |
4600 KB |
Output is correct |
61 |
Correct |
23 ms |
4600 KB |
Output is correct |
62 |
Correct |
21 ms |
4472 KB |
Output is correct |
63 |
Correct |
22 ms |
4600 KB |
Output is correct |
64 |
Correct |
21 ms |
4568 KB |
Output is correct |
65 |
Correct |
22 ms |
4600 KB |
Output is correct |
66 |
Correct |
26 ms |
3300 KB |
Output is correct |
67 |
Correct |
16 ms |
3200 KB |
Output is correct |
68 |
Correct |
25 ms |
4472 KB |
Output is correct |
69 |
Correct |
25 ms |
3200 KB |
Output is correct |
70 |
Correct |
22 ms |
4472 KB |
Output is correct |
71 |
Partially correct |
131 ms |
6012 KB |
Partially correct - number of queries: 5208 |
72 |
Correct |
25 ms |
5120 KB |
Output is correct |
73 |
Partially correct |
137 ms |
6012 KB |
Partially correct - number of queries: 5145 |
74 |
Partially correct |
122 ms |
6008 KB |
Partially correct - number of queries: 5173 |
75 |
Correct |
21 ms |
4604 KB |
Output is correct |
76 |
Correct |
109 ms |
5880 KB |
Output is correct |
77 |
Correct |
106 ms |
6008 KB |
Output is correct |
78 |
Correct |
26 ms |
4984 KB |
Output is correct |
79 |
Correct |
52 ms |
5756 KB |
Output is correct |
80 |
Correct |
113 ms |
5944 KB |
Output is correct |
81 |
Correct |
117 ms |
6008 KB |
Output is correct |
82 |
Correct |
120 ms |
5880 KB |
Output is correct |
83 |
Correct |
24 ms |
4608 KB |
Output is correct |
84 |
Correct |
95 ms |
6008 KB |
Output is correct |
85 |
Correct |
126 ms |
5900 KB |
Output is correct |
86 |
Correct |
30 ms |
5112 KB |
Output is correct |
87 |
Correct |
22 ms |
4728 KB |
Output is correct |
88 |
Correct |
24 ms |
5400 KB |
Output is correct |
89 |
Correct |
29 ms |
5112 KB |
Output is correct |
90 |
Correct |
22 ms |
4472 KB |
Output is correct |
91 |
Correct |
22 ms |
5248 KB |
Output is correct |
92 |
Correct |
24 ms |
5368 KB |
Output is correct |
93 |
Correct |
28 ms |
5376 KB |
Output is correct |
94 |
Correct |
25 ms |
5368 KB |
Output is correct |
95 |
Correct |
27 ms |
5496 KB |
Output is correct |
96 |
Correct |
24 ms |
5368 KB |
Output is correct |
97 |
Correct |
26 ms |
5248 KB |
Output is correct |