#include <bits/stdc++.h>
using namespace std;
const int N = 2 * 200000 + 7;
const int INF = (int) 1e9 + 7;
int n;
int r;
int my_rank;
int init_ord[N];
int ord[N];
int winner[N];
int loser[N];
bool wannap;
int thesol[N];
int rep(int x) {
x %= n;
if (x < 0) x += n;
return x;
}
enum TYPE {SMALL, EQUAL, BIG};
int cnt_big[N];
int cnt_equal[N];
TYPE type[N];
int low, high;
pair<int, TYPE> guys[2 * N];
TYPE gettype(int val) {
if (val == my_rank) {
return EQUAL;
}
if (val < my_rank) {
return SMALL;
}
if (val > my_rank) {
return BIG;
}
assert(0);
}
struct MySet {
map<TYPE, int> f;
void ins(TYPE val) {
f[val]++;
}
TYPE getsm() {
for (auto &it : f) {
if (it.second) {
it.second--;
return it.first;
}
}
assert(0);
}
void clr() {
f.clear();
}
};
int get_smart(int x) {
if (my_rank == 0) {
return 0;
}
if (my_rank <= n) {
int pos = 2 * x;
ord[pos] = my_rank;
for (int i = 0; i < pos; i++) {
ord[i] = init_ord[i];
}
for (int i = pos + 1; i < 2 * n; i++) {
ord[i] = init_ord[i - 1];
}
low = 1;
high = 0;
for (int i = 0; i < 2 * n; i++) {
guys[++high] = {i / 2, gettype(ord[i])};
}
TYPE ant = max(gettype(ord[0]), gettype(ord[1]));
MySet myset;
vector<TYPE> mxs;
for (int iter = 1; iter <= r; iter++) {
while (low <= high && guys[low].first < iter) myset.ins(guys[low++].second);
TYPE smallest = myset.getsm();
if (iter == 1) {
myset.clr();
}
int nw = iter - 1 + n;
TYPE mx = max(smallest, ant);
TYPE mn = min(smallest, ant);
mxs.push_back(mx);
ant = mn;
guys[++high] = {nw, mx};
}
int last_seen = -1, transfering = 0;
for (int i = 0; i < r; i++) {
if (mxs[i] == EQUAL) {
last_seen = i;
transfering++;
}
}
int thepos = n - (r - 1 - last_seen) - 1;
return thepos - n * transfering;
}
int pos = 2 * x;
ord[pos] = my_rank;
for (int i = 0; i < pos; i++) {
ord[i] = init_ord[i];
}
for (int i = pos + 1; i < 2 * n; i++) {
ord[i] = init_ord[i - 1];
}
for (int i = 0; i < 2 * n; i++) {
if (ord[i] == my_rank) {
type[i] = EQUAL;
}
if (ord[i] > my_rank) {
type[i] = BIG;
}
if (ord[i] < my_rank) {
type[i] = SMALL;
}
}
for (int i = 0; i < n; i++) {
cnt_big[i] = (type[2 * i] == BIG) + (type[2 * i + 1] == BIG);
cnt_equal[i] = (type[2 * i] == EQUAL) + (type[2 * i + 1] == EQUAL);
}
int big_in_hand = 0, equal_in_hand = 0, transfering = 0;
function<void()> handle0 = [&] () {
/// if EQUAL vs BIG => EQUAL remains, BIG goes
int the_sum = cnt_big[0] + cnt_equal[0];
big_in_hand += cnt_big[0];
equal_in_hand += cnt_equal[0];
cnt_big[0] = cnt_equal[0] = 0;
if (the_sum != 2) {
/// there is a small one and it pushes them away
transfering += (equal_in_hand);
return;
}
if (equal_in_hand) {
cnt_equal[0] = 1;
equal_in_hand = 0;
} else {
if (big_in_hand) {
cnt_big[0] = 1;
big_in_hand--;
}
}
transfering += (equal_in_hand);
};
function<void(int)> handlenot0 = [&] (int i) {
/// if EQUAL vs BIG => BIG remains, EQUAL goes
big_in_hand += cnt_big[i];
equal_in_hand += cnt_equal[i];
cnt_big[i] = cnt_equal[i] = 0;
if (big_in_hand) {
cnt_big[i] = 1;
big_in_hand--;
} else {
if (equal_in_hand) {
cnt_equal[i] = 1;
equal_in_hand = 0;
}
}
};
handle0();
for (int i = n - 1; i >= 1; i--) handlenot0(i);
handle0();
for (int i = n - 1; i >= 1; i--) handlenot0(i);
int thepos = -1;
for (int i = 0; i < n; i++) {
if (cnt_equal[i]) {
thepos = i;
}
}
assert(thepos != -1);
return thepos - n * transfering;
}
int get(int x) {
if (thesol[x] != INF) {
return thesol[x];
}
thesol[x] = get_smart(x);
return thesol[x];
}
int mn = (int) 1e9;
int best = -1;
void upd(int i) {
int x = get(i);
int y = rep(x);
if (y < mn) {
mn = y;
best = i;
} else {
if (y == mn) {
best = max(best, i);
}
}
}
signed main() {
ios::sync_with_stdio(0); cin.tie(0);
///freopen ("input", "r", stdin);
for (int i = 0; i < N; i++) {
thesol[i] = INF;
}
wannap = 0;
cin >> n >> r;
r = 2 * n + r % n;
cin >> my_rank;
my_rank--;
for (int i = 0; i < 2 * n - 1; i++) {
cin >> init_ord[i];
init_ord[i]--;
}
upd(0);
upd(n - 1);
{
int low = 0, high = n - 1;
while (low <= high) {
int mid = (low + high) / 2;
upd(mid);
if (get(mid) == get(0)) {
low = mid + 1;
} else {
high = mid - 1;
}
}
}
int L = get(0), R = get(n - 1);
for (int j = L; j <= R; j++) {
if (j % n) {
continue;
}
int low = 0, high = n - 1, the_value = INF;
while (low <= high) {
int mid = (low + high) / 2;
upd(mid);
if (get(mid) >= j) {
high = mid - 1;
the_value = mid;
} else {
low = mid + 1;
}
}
low = the_value;
high = n - 1;
while (low <= high) {
int mid = (low + high) / 2;
upd(mid);
if (get(mid) == get(the_value)) {
low = mid + 1;
} else {
high = mid - 1;
}
}
}
cout << best + 1 << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1900 KB |
Output is correct |
2 |
Correct |
2 ms |
1904 KB |
Output is correct |
3 |
Correct |
1 ms |
1868 KB |
Output is correct |
4 |
Correct |
6 ms |
2124 KB |
Output is correct |
5 |
Correct |
1 ms |
1896 KB |
Output is correct |
6 |
Correct |
1 ms |
1868 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1868 KB |
Output is correct |
2 |
Correct |
1 ms |
1868 KB |
Output is correct |
3 |
Correct |
4 ms |
1996 KB |
Output is correct |
4 |
Correct |
44 ms |
3232 KB |
Output is correct |
5 |
Correct |
840 ms |
15848 KB |
Output is correct |
6 |
Correct |
1 ms |
1868 KB |
Output is correct |
7 |
Correct |
2 ms |
1996 KB |
Output is correct |
8 |
Correct |
15 ms |
3076 KB |
Output is correct |
9 |
Correct |
42 ms |
3584 KB |
Output is correct |
10 |
Correct |
2 ms |
1996 KB |
Output is correct |
11 |
Correct |
58 ms |
3820 KB |
Output is correct |
12 |
Correct |
5 ms |
2124 KB |
Output is correct |
13 |
Correct |
358 ms |
12208 KB |
Output is correct |
14 |
Correct |
12 ms |
2372 KB |
Output is correct |
15 |
Correct |
86 ms |
4824 KB |
Output is correct |
16 |
Correct |
1 ms |
1868 KB |
Output is correct |
17 |
Correct |
4 ms |
1996 KB |
Output is correct |
18 |
Correct |
4 ms |
1996 KB |
Output is correct |
19 |
Correct |
7 ms |
2252 KB |
Output is correct |
20 |
Correct |
11 ms |
2252 KB |
Output is correct |
21 |
Correct |
40 ms |
3660 KB |
Output is correct |
22 |
Correct |
70 ms |
4604 KB |
Output is correct |
23 |
Correct |
498 ms |
15624 KB |
Output is correct |
24 |
Correct |
1 ms |
1924 KB |
Output is correct |
25 |
Correct |
2 ms |
1868 KB |
Output is correct |
26 |
Correct |
4 ms |
2076 KB |
Output is correct |
27 |
Correct |
18 ms |
2808 KB |
Output is correct |
28 |
Correct |
130 ms |
6620 KB |
Output is correct |
29 |
Correct |
2 ms |
1996 KB |
Output is correct |
30 |
Correct |
3 ms |
2072 KB |
Output is correct |
31 |
Correct |
11 ms |
2700 KB |
Output is correct |
32 |
Correct |
119 ms |
8132 KB |
Output is correct |
33 |
Correct |
1 ms |
1868 KB |
Output is correct |
34 |
Correct |
1 ms |
1868 KB |
Output is correct |
35 |
Correct |
2 ms |
1996 KB |
Output is correct |
36 |
Correct |
2 ms |
1996 KB |
Output is correct |
37 |
Correct |
12 ms |
2640 KB |
Output is correct |
38 |
Correct |
16 ms |
3000 KB |
Output is correct |
39 |
Correct |
1 ms |
1868 KB |
Output is correct |
40 |
Correct |
2 ms |
1868 KB |
Output is correct |
41 |
Correct |
2 ms |
1996 KB |
Output is correct |
42 |
Correct |
2 ms |
1996 KB |
Output is correct |
43 |
Correct |
3 ms |
2124 KB |
Output is correct |
44 |
Correct |
6 ms |
2252 KB |
Output is correct |
45 |
Correct |
11 ms |
2764 KB |
Output is correct |
46 |
Correct |
13 ms |
2764 KB |
Output is correct |
47 |
Correct |
133 ms |
8976 KB |
Output is correct |