#include "prize.h"
#include <bits/stdc++.h>
#define pb push_back
#define F first
#define S second
#define all(x) x.begin(), x.end()
using namespace std;
typedef pair<int, int> pii;
const int N = 2e5 + 10;
pii A[N];
pii Ask(int i){
if(A[i] != pii(-1, -1)) return A[i];
vector<int> res = ask(i - 1);
pii tmp = {res[0], res[1]};
return A[i] = tmp;
}
int mx;
vector<int> V;
int fen[N];
void Add(int idx, int d){
//if(d == -1) cerr << "--- " << idx << '\n';
for(; idx < N; idx += idx & -idx)
fen[idx] += d;
}
int Get(int idx){
int res = 0;
for(; idx; idx -= idx & -idx)
res += fen[idx];
return res;
}
int Get0(int L, int R){
if(R < L) return 0;
return (R - L + 1) - (Get(R) - Get(L - 1));
}
int Bin(int x){
int res = 0, res2;
for(int lg = 18; lg >= 0; lg--){
res2 = res | (1 << lg);
if(res2 >= N) continue;
if(fen[res2] < x){
x -= fen[res2];
res = res2;
}
}
return res + 1;
}
struct fun {
int L, R, bl, br, rq;
fun (int _L, int _R, int _bl, int _br, int _rq){
L = _L;
R = _R;
bl = _bl;
br = _br;
rq = _rq;
}
};
vector< fun > slv;
int find_best(int n) {
fill(A, A + n, pii(-1, -1));
memset(fen, 0, sizeof fen);
mx = -1; V.clear();
for(int i = 1; i <= n; i++) Add(i, 1);
pii fst = Ask(n / 2);
mx = fst.F + fst.S;
V = {};
slv.pb( fun(1, n, 0, 0, mx) );
int L, R;
while(!slv.empty()){
fun la = slv.back();
slv.pop_back();
//cerr << "! " << la.L << ' ' << la.R << " : " << la.rq << '\n';
if(la.rq == 0) continue;
L = la.L;
R = la.R;
int sm = Get(R) - Get(L - 1);
assert(sm >= la.rq);
int mid = Bin(Get(L - 1) + ((sm + 1) / 2));
//cerr << "# " << mid << '\n';
pii tmp = Ask(mid);
//
if(tmp.F + tmp.S > mx){
mx = tmp.F + tmp.S;
for(auto x : V) Add(x, -1);
V.clear();
slv.clear();
slv.pb(fun(1, n, 0, 0, mx - Get0(1, n)));
continue;
}
if(tmp.F + tmp.S == mx){
int c0 = Get0(L, mid - 1);
//cerr << "c0 : " << c0 << '\n';
slv.pb(fun(L, mid - 1, la.bl, tmp.S, tmp.F - la.bl - c0));
slv.pb(fun(mid + 1, R, tmp.F, la.br, la.rq - (tmp.F - la.bl - c0)));
//slv.pb({{L, mid - 1}, });
//slv.pb({{mid + 1, R}, });
V.pb(mid);
continue;
}
Add(mid, -1);
//slv.pb(la);
slv.pb(fun(L, R, la.bl, la.br, la.rq - 1));
}
for(int i = 1; i <= n; i++)
if(A[i] == pii(0, 0))
return i - 1;
assert(false);
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
2688 KB |
Output is correct |
2 |
Correct |
6 ms |
2688 KB |
Output is correct |
3 |
Correct |
6 ms |
2680 KB |
Output is correct |
4 |
Correct |
6 ms |
2688 KB |
Output is correct |
5 |
Correct |
6 ms |
2684 KB |
Output is correct |
6 |
Correct |
6 ms |
2680 KB |
Output is correct |
7 |
Correct |
6 ms |
2688 KB |
Output is correct |
8 |
Correct |
6 ms |
2688 KB |
Output is correct |
9 |
Correct |
6 ms |
2688 KB |
Output is correct |
10 |
Correct |
6 ms |
2688 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
2688 KB |
Output is correct |
2 |
Correct |
6 ms |
2688 KB |
Output is correct |
3 |
Correct |
6 ms |
2688 KB |
Output is correct |
4 |
Correct |
6 ms |
2812 KB |
Output is correct |
5 |
Correct |
6 ms |
2688 KB |
Output is correct |
6 |
Correct |
6 ms |
2688 KB |
Output is correct |
7 |
Correct |
6 ms |
2688 KB |
Output is correct |
8 |
Correct |
6 ms |
2816 KB |
Output is correct |
9 |
Correct |
6 ms |
2688 KB |
Output is correct |
10 |
Correct |
6 ms |
2688 KB |
Output is correct |
11 |
Correct |
11 ms |
2688 KB |
Output is correct |
12 |
Correct |
10 ms |
2688 KB |
Output is correct |
13 |
Correct |
10 ms |
2680 KB |
Output is correct |
14 |
Correct |
9 ms |
1280 KB |
Output is correct |
15 |
Correct |
41 ms |
2688 KB |
Output is correct |
16 |
Correct |
47 ms |
2680 KB |
Output is correct |
17 |
Correct |
30 ms |
2748 KB |
Output is correct |
18 |
Correct |
37 ms |
2732 KB |
Output is correct |
19 |
Correct |
42 ms |
2680 KB |
Output is correct |
20 |
Correct |
19 ms |
1960 KB |
Output is correct |
21 |
Correct |
46 ms |
2688 KB |
Output is correct |
22 |
Correct |
36 ms |
2680 KB |
Output is correct |
23 |
Correct |
7 ms |
2688 KB |
Output is correct |
24 |
Correct |
8 ms |
2720 KB |
Output is correct |
25 |
Correct |
38 ms |
2680 KB |
Output is correct |
26 |
Correct |
49 ms |
2808 KB |
Output is correct |
27 |
Correct |
7 ms |
2688 KB |
Output is correct |
28 |
Correct |
41 ms |
2688 KB |
Output is correct |
29 |
Correct |
39 ms |
2688 KB |
Output is correct |
30 |
Correct |
47 ms |
2680 KB |
Output is correct |
31 |
Correct |
49 ms |
2688 KB |
Output is correct |
32 |
Correct |
11 ms |
2688 KB |
Output is correct |
33 |
Correct |
1 ms |
1152 KB |
Output is correct |
34 |
Correct |
45 ms |
2688 KB |
Output is correct |
35 |
Correct |
7 ms |
2720 KB |
Output is correct |
36 |
Correct |
44 ms |
2680 KB |
Output is correct |
37 |
Correct |
8 ms |
2720 KB |
Output is correct |
38 |
Correct |
7 ms |
2688 KB |
Output is correct |
39 |
Correct |
43 ms |
2688 KB |
Output is correct |
40 |
Correct |
39 ms |
2688 KB |
Output is correct |
41 |
Correct |
42 ms |
2688 KB |
Output is correct |
42 |
Correct |
30 ms |
2748 KB |
Output is correct |
43 |
Correct |
28 ms |
2748 KB |
Output is correct |
44 |
Correct |
42 ms |
2688 KB |
Output is correct |
45 |
Correct |
26 ms |
2748 KB |
Output is correct |
46 |
Correct |
29 ms |
2748 KB |
Output is correct |
47 |
Correct |
41 ms |
2688 KB |
Output is correct |
48 |
Correct |
29 ms |
2748 KB |
Output is correct |
49 |
Correct |
39 ms |
2716 KB |
Output is correct |
50 |
Correct |
42 ms |
2680 KB |
Output is correct |
51 |
Correct |
39 ms |
2736 KB |
Output is correct |
52 |
Correct |
45 ms |
2688 KB |
Output is correct |
53 |
Correct |
8 ms |
2688 KB |
Output is correct |
54 |
Correct |
45 ms |
2680 KB |
Output is correct |
55 |
Correct |
52 ms |
2808 KB |
Output is correct |
56 |
Correct |
33 ms |
2748 KB |
Output is correct |
57 |
Correct |
55 ms |
2724 KB |
Output is correct |
58 |
Correct |
55 ms |
2808 KB |
Output is correct |
59 |
Correct |
50 ms |
2688 KB |
Output is correct |
60 |
Correct |
28 ms |
2744 KB |
Output is correct |
61 |
Correct |
7 ms |
2688 KB |
Output is correct |
62 |
Correct |
7 ms |
2688 KB |
Output is correct |
63 |
Correct |
7 ms |
2688 KB |
Output is correct |
64 |
Correct |
7 ms |
2688 KB |
Output is correct |
65 |
Correct |
7 ms |
2680 KB |
Output is correct |
66 |
Correct |
11 ms |
2688 KB |
Output is correct |
67 |
Correct |
11 ms |
2688 KB |
Output is correct |
68 |
Correct |
10 ms |
2688 KB |
Output is correct |
69 |
Correct |
11 ms |
2688 KB |
Output is correct |
70 |
Correct |
10 ms |
2688 KB |
Output is correct |
71 |
Correct |
60 ms |
2688 KB |
Output is correct |
72 |
Correct |
12 ms |
2688 KB |
Output is correct |
73 |
Correct |
52 ms |
2812 KB |
Output is correct |
74 |
Correct |
51 ms |
2688 KB |
Output is correct |
75 |
Correct |
8 ms |
2688 KB |
Output is correct |
76 |
Correct |
55 ms |
2688 KB |
Output is correct |
77 |
Correct |
45 ms |
2688 KB |
Output is correct |
78 |
Correct |
11 ms |
2688 KB |
Output is correct |
79 |
Correct |
33 ms |
2688 KB |
Output is correct |
80 |
Correct |
51 ms |
2936 KB |
Output is correct |
81 |
Correct |
56 ms |
2808 KB |
Output is correct |
82 |
Correct |
59 ms |
2688 KB |
Output is correct |
83 |
Correct |
7 ms |
2688 KB |
Output is correct |
84 |
Correct |
24 ms |
2856 KB |
Output is correct |
85 |
Correct |
45 ms |
2688 KB |
Output is correct |
86 |
Correct |
12 ms |
2688 KB |
Output is correct |
87 |
Correct |
6 ms |
2688 KB |
Output is correct |
88 |
Correct |
12 ms |
2688 KB |
Output is correct |
89 |
Correct |
9 ms |
2688 KB |
Output is correct |
90 |
Correct |
6 ms |
2688 KB |
Output is correct |
91 |
Correct |
10 ms |
2688 KB |
Output is correct |
92 |
Correct |
9 ms |
2688 KB |
Output is correct |
93 |
Correct |
9 ms |
2688 KB |
Output is correct |
94 |
Correct |
10 ms |
2688 KB |
Output is correct |
95 |
Correct |
8 ms |
2688 KB |
Output is correct |
96 |
Correct |
7 ms |
2720 KB |
Output is correct |
97 |
Correct |
6 ms |
2688 KB |
Output is correct |