QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#270957 | #5150. Alternating Algorithm | ckiseki# | WA | 0ms | 3888kb | C++20 | 5.2kb | 2023-12-01 18:54:30 | 2023-12-01 18:54:30 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define all(x) begin(x), end(x)
#ifdef CKISEKI
#define safe cerr << __PRETTY_FUNCTION__ << " line " << __LINE__ << " safe\n"
#define debug(a...) debug_(#a, a)
#define orange(a...) orange_(#a, a)
#include <experimental/iterator>
void debug_(auto s, auto ...a) {
cerr << "\e[1;32m(" << s << ") = (";
int f = 0;
(..., (cerr << (f++ ? ", " : "") << a));
cerr << ")\e[0m\n";
}
void orange_(auto s, auto L, auto R) {
cerr << "\e[1;33m[ " << s << " ] = [ ";
using namespace experimental;
copy(L, R, make_ostream_joiner(cerr, ", "));
cerr << " ]\e[0m\n";
}
#else
#define safe ((void)0)
#define debug(...) safe
#define orange(...) safe
#endif
#include <ext/random>
__gnu_cxx::sfmt19937 rnd(7122);
const int inf = 1e9;
struct treap {
int index;
int val;
int mn;
int size;
uint32_t pri;
treap *lc, *rc, *pa;
treap(int i) : index(i), val(i), mn(i), size(1), pri(rnd()), lc(nullptr), rc(nullptr), pa(nullptr) {}
void pull() {
pa = nullptr;
size = 0;
val = inf;
mn = inf;
if (lc) {
val = min(val, lc->val);
size += lc->size;
mn = min(mn, lc->mn);
lc->pa = this;
}
val = min(val, index - size);
size += 1;
mn = min(mn, index);
if (rc) {
val = min(val, rc->val - size * 2);
size += rc->size;
mn = min(mn, rc->mn);
rc->pa = this;
}
}
};
int SZ(treap *x) { return x ? x->size : 0; }
treap *merge(treap *L, treap *R) {
if (not L or not R) return L ? L : R;
if (L->pri > R->pri)
return L->rc = merge(L->rc, R), L->pull(), L;
else
return R->lc = merge(L, R->lc), R->pull(), R;
}
void splitBySize(treap *o, int k, treap *&L, treap *&R) {
if (not o) L = R = nullptr;
else if (int s = SZ(o->lc) + 1; s <= k)
L = o, splitBySize(o->rc, k - s, L->rc, R), L->pull();
else
R = o, splitBySize(o->lc, k, L, R->lc), R->pull();
} // SZ(L) == k
void split(treap *o, int v, treap *&L, treap *&R) { // put <= v into L
if (not o) L = R = nullptr;
else if (o->index <= v)
L = o, split(o->rc, v, L->rc, R), L->pull();
else
R = o, split(o->lc, v, L, R->lc), R->pull();
}
int getRank(treap *o) {
int r = SZ(o->lc) + 1;
for (; o->pa; o = o->pa)
if (o->pa->rc == o)
r += SZ(o->pa->lc) + 1;
return r;
}
void dump(treap *o) {
vector<int> to_dump;
const auto dfs = [&](auto self, treap *p) {
if (!p) return;
self(self, p->lc);
to_dump.emplace_back(p->index);
self(self, p->rc);
};
dfs(dfs, o);
orange(all(to_dump));
}
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int n;
cin >> n;
vector<int> arr(n + 1);
for (int &ai : arr)
cin >> ai;
vector<pair<int,int>> evt;
for (int i = 0; i <= n; i++) {
evt.emplace_back(arr[i], i);
}
ranges::sort(evt);
// initially all 1
vector<int> before(n + 1);
vector<int> one_step_after(n + 1);
for (int i = 0; i <= n; i++) {
before[i] = 1;
}
one_step_after = before;
// TODO insert into treap
int ans = 0;
treap *root = nullptr;
const auto update = [&](int p, int v) {
if (v == 0) {
treap *A, *B, *C;
split(root, p - 1, A, B);
auto Z = B;
split(Z, p, B, C);
root = merge(A, C);
} else {
treap *A, *B;
split(root, p - 1, A, B);
assert (!B || B->mn != p);
root = merge(merge(A, new treap(p)), B);
}
};
const auto query = [&]() {
assert (root);
dump(root);
debug(root->val, root->size);
// suppose go root->size - 1 step
if (root->val + root->size - 1 + root->size - 1 < n) {
int res = root->size - 1 + n - (root->val + root->size - 1 + root->size - 1);
debug(root->val + root->size - 1);
return res;
}
if (root->mn + root->size - 1 == n) {
return 0;
}
treap *A, *B;
int l = 1, r = root->size;
while (r - l > 1) {
int m = (l + r) / 2;
splitBySize(root, m, A, B);
int cur_val = (A ? A->val : inf);
root = merge(A, B);
if (m - 1 + cur_val + root->size - 1 < n) {
l = m;
} else {
r = m;
}
}
return l;
};
for (int i = 0; i <= n; i++)
update(i, 1);
set<int> st;
for (int i = 0; i <= n; i++)
st.emplace(i);
debug(root ? root->size : 0);
for (auto [x, y] : evt) {
auto cur = query() + (*st.begin() + st.size() - 1 != n);
debug(*st.begin(), st.size());
ans = max(ans, cur);
debug(cur);
// set before to 0
before[y] = 0;
st.erase(y);
for (int i : {y - 1, y, y + 1}) {
if (i < 0 || i % 2 == 1) continue;
if (i > n) continue;
if (i + 1 > n) {
int &a = one_step_after[i];
if (a != before[i])
update(i, before[i]);
a = before[i];
continue;
}
int &a = one_step_after[i];
int &b = one_step_after[i + 1];
if (before[i] == 1 && before[i + 1] == 0) {
if (a != 0)
update(i, 0);
if (b != 1)
update(i + 1, 1);
a = 0;
b = 1;
} else {
if (a != before[i])
update(i, before[i]);
if (b != before[i + 1])
update(i + 1, before[i + 1]);
a = before[i];
b = before[i + 1];
}
}
}
cout << ans << '\n';
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3648kb
input:
3 8 13 4 10
output:
3
result:
ok single line: '3'
Test #2:
score: 0
Accepted
time: 0ms
memory: 3660kb
input:
5 13 12 14 10 14 12
output:
3
result:
ok single line: '3'
Test #3:
score: 0
Accepted
time: 0ms
memory: 3888kb
input:
2 2 2 1
output:
3
result:
ok single line: '3'
Test #4:
score: 0
Accepted
time: 0ms
memory: 3596kb
input:
1 300172042 474444146
output:
0
result:
ok single line: '0'
Test #5:
score: 0
Accepted
time: 0ms
memory: 3596kb
input:
1 636357447 557539481
output:
1
result:
ok single line: '1'
Test #6:
score: 0
Accepted
time: 0ms
memory: 3716kb
input:
2 139715426 368724097 417561009
output:
0
result:
ok single line: '0'
Test #7:
score: 0
Accepted
time: 0ms
memory: 3656kb
input:
2 77784868 542697475 509604021
output:
2
result:
ok single line: '2'
Test #8:
score: 0
Accepted
time: 0ms
memory: 3612kb
input:
2 698395658 71848686 699775597
output:
1
result:
ok single line: '1'
Test #9:
score: 0
Accepted
time: 0ms
memory: 3820kb
input:
2 487635147 571273621 442673389
output:
3
result:
ok single line: '3'
Test #10:
score: 0
Accepted
time: 0ms
memory: 3816kb
input:
2 502022170 254766224 258867503
output:
2
result:
ok single line: '2'
Test #11:
score: 0
Accepted
time: 0ms
memory: 3600kb
input:
2 783651505 271735448 154090385
output:
3
result:
ok single line: '3'
Test #12:
score: 0
Accepted
time: 0ms
memory: 3660kb
input:
3 423187900 701340783 708457090 788989478
output:
0
result:
ok single line: '0'
Test #13:
score: 0
Accepted
time: 0ms
memory: 3628kb
input:
3 172068101 507957913 237246316 805323765
output:
2
result:
ok single line: '2'
Test #14:
score: 0
Accepted
time: 0ms
memory: 3656kb
input:
3 309846480 218704879 536482379 754210806
output:
1
result:
ok single line: '1'
Test #15:
score: 0
Accepted
time: 0ms
memory: 3692kb
input:
3 150235215 485036833 52089968 645641645
output:
3
result:
ok single line: '3'
Test #16:
score: 0
Accepted
time: 0ms
memory: 3544kb
input:
3 735389981 669677621 733676260 858050940
output:
2
result:
ok single line: '2'
Test #17:
score: 0
Accepted
time: 0ms
memory: 3856kb
input:
3 635103474 551413670 85269704 730878535
output:
3
result:
ok single line: '3'
Test #18:
score: 0
Accepted
time: 0ms
memory: 3856kb
input:
3 287528440 314452762 846234936 452787633
output:
1
result:
ok single line: '1'
Test #19:
score: 0
Accepted
time: 0ms
memory: 3816kb
input:
3 276069955 969481471 992185356 536479156
output:
2
result:
ok single line: '2'
Test #20:
score: 0
Accepted
time: 0ms
memory: 3592kb
input:
3 225096493 88165689 415816372 360778803
output:
1
result:
ok single line: '1'
Test #21:
score: 0
Accepted
time: 0ms
memory: 3660kb
input:
3 651934487 760368054 975264908 206290402
output:
3
result:
ok single line: '3'
Test #22:
score: 0
Accepted
time: 0ms
memory: 3860kb
input:
3 668819975 16012633 798541220 258404088
output:
2
result:
ok single line: '2'
Test #23:
score: 0
Accepted
time: 0ms
memory: 3664kb
input:
3 303955151 276719749 324951113 63908344
output:
3
result:
ok single line: '3'
Test #24:
score: 0
Accepted
time: 0ms
memory: 3548kb
input:
3 419862649 709195111 424612582 548104611
output:
3
result:
ok single line: '3'
Test #25:
score: 0
Accepted
time: 0ms
memory: 3628kb
input:
3 46436854 762650424 543885894 63420906
output:
3
result:
ok single line: '3'
Test #26:
score: 0
Accepted
time: 0ms
memory: 3648kb
input:
3 663885616 817966829 428282021 750799481
output:
3
result:
ok single line: '3'
Test #27:
score: 0
Accepted
time: 0ms
memory: 3592kb
input:
3 453815838 784866392 626401113 33629018
output:
3
result:
ok single line: '3'
Test #28:
score: 0
Accepted
time: 0ms
memory: 3592kb
input:
3 612031283 905623341 296446821 317142883
output:
4
result:
ok single line: '4'
Test #29:
score: 0
Accepted
time: 0ms
memory: 3544kb
input:
3 690093550 720639503 493410469 329723725
output:
4
result:
ok single line: '4'
Test #30:
score: 0
Accepted
time: 0ms
memory: 3664kb
input:
3 640270086 11003869 302770972 380428351
output:
3
result:
ok single line: '3'
Test #31:
score: 0
Accepted
time: 0ms
memory: 3824kb
input:
3 813904638 53916473 202342438 178710524
output:
3
result:
ok single line: '3'
Test #32:
score: 0
Accepted
time: 0ms
memory: 3628kb
input:
3 858480562 107901831 70069694 624943715
output:
3
result:
ok single line: '3'
Test #33:
score: 0
Accepted
time: 0ms
memory: 3608kb
input:
3 972814426 208602080 487914166 199127689
output:
3
result:
ok single line: '3'
Test #34:
score: 0
Accepted
time: 0ms
memory: 3664kb
input:
3 527326624 369552716 30514207 190802344
output:
4
result:
ok single line: '4'
Test #35:
score: 0
Accepted
time: 0ms
memory: 3600kb
input:
3 885560774 510753464 330831417 122397162
output:
4
result:
ok single line: '4'
Test #36:
score: 0
Accepted
time: 0ms
memory: 3596kb
input:
1 0 0
output:
0
result:
ok single line: '0'
Test #37:
score: 0
Accepted
time: 0ms
memory: 3652kb
input:
1 1 0
output:
1
result:
ok single line: '1'
Test #38:
score: 0
Accepted
time: 0ms
memory: 3600kb
input:
1 0 1
output:
0
result:
ok single line: '0'
Test #39:
score: 0
Accepted
time: 0ms
memory: 3660kb
input:
1 1000000000 0
output:
1
result:
ok single line: '1'
Test #40:
score: 0
Accepted
time: 0ms
memory: 3856kb
input:
5 870923667 831419329 551216223 626357192 564992248 642950852
output:
6
result:
ok single line: '6'
Test #41:
score: 0
Accepted
time: 0ms
memory: 3596kb
input:
5 436264160 745635157 20618089 707614372 862629566 987729003
output:
3
result:
ok single line: '3'
Test #42:
score: 0
Accepted
time: 0ms
memory: 3528kb
input:
5 112182501 364650582 622093010 819594012 467586768 328068426
output:
4
result:
ok single line: '4'
Test #43:
score: 0
Accepted
time: 0ms
memory: 3668kb
input:
6 440802446 672072796 870079224 289645602 358794408 131990964 936527350
output:
5
result:
ok single line: '5'
Test #44:
score: 0
Accepted
time: 0ms
memory: 3632kb
input:
6 624312076 675425489 51650975 686013685 309942426 127494361 289215201
output:
6
result:
ok single line: '6'
Test #45:
score: 0
Accepted
time: 0ms
memory: 3656kb
input:
7 406067722 563548194 1821761 121198244 605039142 435891339 752521249 231257069
output:
5
result:
ok single line: '5'
Test #46:
score: 0
Accepted
time: 0ms
memory: 3600kb
input:
8 903342821 163731172 682809514 389316549 725357000 720997713 96340788 793801888 869342849
output:
8
result:
ok single line: '8'
Test #47:
score: -100
Wrong Answer
time: 0ms
memory: 3628kb
input:
9 363831817 704177455 355821226 562295495 935390976 836136856 341398270 776676829 529678510 52558572
output:
8
result:
wrong answer 1st lines differ - expected: '9', found: '8'