QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#401377 | #2555. Two Bullets | nguyentunglam | WA | 2ms | 10132kb | C++17 | 4.4kb | 2024-04-28 16:28:37 | 2024-04-28 16:28:38 |
Judging History
answer
#include<bits/stdc++.h>
#define all(v) v.begin(), v.end()
#define endl "\n"
using namespace std;
const int N = 1e5 + 10, K = 100;
int a[N], deg[N], rev_idx[N], cost[N];
int n;
int st[N / K], ed[N / K];
vector<int> rrh[N / K];
struct IT {
int lazy[K * 4];
pair<int, int> T[K * 4];
void build(int s, int l, int r) {
if (l == r) {
T[s] = make_pair(0, l);
return;
}
int mid = l + r >> 1;
build(s << 1, l, mid);
build(s << 1 | 1, mid + 1, r);
T[s] = min(T[s << 1], T[s << 1 | 1]);
}
void push(int s, int l, int r) {
if (!lazy[s]) return;
T[s].first += lazy[s];
if (l != r) {
lazy[s << 1] += lazy[s];
lazy[s << 1 | 1] += lazy[s];
}
lazy[s] = 0;
}
void up(int s, int l, int r, int from, int to, int val) {
push(s, l, r);
if (l > to || r < from) return;
if (from <= l && r <= to) {
lazy[s] = val;
push(s, l, r);
return;
}
int mid = l + r >> 1;
up(s << 1, l, mid, from, to, val);
up(s << 1 | 1, mid + 1, r, from, to, val);
T[s] = min(T[s << 1], T[s << 1 | 1]);
}
} it[N / K];
int bit[N];
void add (int pos, int val) {
while (pos) {
bit[pos] += val;
pos -= pos & -pos;
}
}
int get(int pos) {
int ret = 0;
while (pos <= n) {
ret += bit[pos];
pos += pos & -pos;
}
return ret;
}
vector<int> q[2];
int32_t main() {
#define task ""
cin.tie(0) -> sync_with_stdio(0);
if (fopen("task.inp", "r")) {
freopen("task.inp", "r", stdin);
freopen("task.out", "w", stdout);
}
if (fopen(task".inp", "r")) {
freopen (task".inp", "r", stdin);
freopen (task".out", "w", stdout);
}
cin >> n;
for(int i = 1; i <= n; i++) cin >> a[i];
for(int i = 1; i <= n; i++) rev_idx[a[i]] = i;
for(int i = 1; i <= n; i++) {
cost[i] = get(a[i]);
add(a[i], 1);
}
for(int i = n, mn = 1e9; i >= 1; i--) {
deg[i] = a[i] > mn;
mn = min(mn, a[i]);
}
auto idx = [&] (int block, int val) {
return upper_bound(all(rrh[block]), val) - rrh[block].begin();
};
auto handle = [&] (int block) {
while (it[block].T[1].first == 0) {
int x = it[block].T[1].second;
it[block].up(1, 1, ed[block] - st[block] + 1, x, x, 1e9);
int y = rev_idx[x];
q[deg[y]].push_back(y);
}
};
for(int block = 0; block <= n / K; block++) {
st[block] = max(1, block * K);
ed[block] = min(n, block * K + K - 1);
for(int i = st[block]; i <= ed[block]; i++) rrh[block].push_back(a[i]);
sort(all(rrh[block]));
rrh[block].resize(unique(all(rrh[block])) - rrh[block].begin());
// cout << ed[block] - st[block] + 1 << endl;
it[block].build(1, 1, ed[block] - st[block] + 1);
for(int i = st[block]; i <= ed[block]; i++) {
int pos = idx(block, a[i]);
it[block].up(1, 1, ed[block] - st[block] + 1, pos, pos, cost[i]);
}
handle(block);
}
// exit(0);
auto del = [&] (int i) {
int group = i / K;
for(int j = i + 1; j <= ed[group]; j++) if (a[i] > a[j]) {
int pos = idx(group, a[j]);
it[group].up(1, 1, ed[group] - st[group] + 1, pos, pos, -1);
}
// exit(0);
handle(group);
for(int block = group + 1; block <= n / K; block++) {
int pos = idx(block, a[i]);
if (!pos) continue;
it[block].up(1, 1, ed[block] - st[block] + 1, 1, pos, -1);
handle(group);
}
};
vector<pair<int, int> > ans;
// del(1);
// cout << q[0].size() << endl;
// for(int &j : q[0]) cout << j << " "; cout << endl;
// exit(0);
while (1) {
if (q[1].size() >= 2) {
int x = q[1].back(); q[1].pop_back();
int y = q[1].back(); q[1].pop_back();
ans.emplace_back(a[x], a[y]);
del(x);
del(y);
}
else if (q[1].size() == 1) {
int x = q[1].back(); q[1].pop_back();
if (q[0].empty()) ans.emplace_back(a[x], a[x]);
else {
int y = q[0].back(); q[0].pop_back();
ans.emplace_back(a[x], a[y]);
}
del(x);
}
else {
if (q[0].empty()) break;
int x = q[0].back(); q[0].pop_back();
if (q[0].empty()) {
ans.emplace_back(a[x], a[x]);
break;
}
int y = q[0].back(); q[0].pop_back();
ans.emplace_back(a[x], a[y]);
}
}
cout << ans.size() << endl;
for(auto &[x, y] : ans) cout << x << " " << y << endl;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 9856kb
input:
8 4 3 8 2 1 7 6 5
output:
4 8 4 3 7 6 2 1 5
result:
ok da
Test #2:
score: 0
Accepted
time: 1ms
memory: 8392kb
input:
8 5 6 7 1 2 8 3 4
output:
4 8 7 6 5 4 3 2 1
result:
ok da
Test #3:
score: 0
Accepted
time: 2ms
memory: 10132kb
input:
4 1 2 4 3
output:
2 4 2 3 1
result:
ok da
Test #4:
score: 0
Accepted
time: 2ms
memory: 8596kb
input:
2 1 2
output:
1 2 1
result:
ok da
Test #5:
score: 0
Accepted
time: 2ms
memory: 9184kb
input:
2 2 1
output:
2 2 2 1 1
result:
ok da
Test #6:
score: 0
Accepted
time: 1ms
memory: 9804kb
input:
3 1 2 3
output:
2 3 2 1 1
result:
ok da
Test #7:
score: 0
Accepted
time: 1ms
memory: 8944kb
input:
3 1 3 2
output:
2 3 1 2 2
result:
ok da
Test #8:
score: 0
Accepted
time: 2ms
memory: 8988kb
input:
3 2 1 3
output:
2 2 3 1 1
result:
ok da
Test #9:
score: 0
Accepted
time: 1ms
memory: 8388kb
input:
3 2 3 1
output:
2 3 2 1 1
result:
ok da
Test #10:
score: 0
Accepted
time: 0ms
memory: 8564kb
input:
3 3 1 2
output:
2 3 3 2 1
result:
ok da
Test #11:
score: 0
Accepted
time: 1ms
memory: 8828kb
input:
3 3 2 1
output:
3 3 3 2 2 1 1
result:
ok da
Test #12:
score: 0
Accepted
time: 2ms
memory: 9556kb
input:
4 1 2 3 4
output:
2 4 3 2 1
result:
ok da
Test #13:
score: 0
Accepted
time: 1ms
memory: 8848kb
input:
4 1 2 4 3
output:
2 4 2 3 1
result:
ok da
Test #14:
score: 0
Accepted
time: 0ms
memory: 8828kb
input:
4 1 3 2 4
output:
2 3 4 2 1
result:
ok da
Test #15:
score: 0
Accepted
time: 0ms
memory: 8824kb
input:
4 1 3 4 2
output:
2 4 3 2 1
result:
ok da
Test #16:
score: 0
Accepted
time: 1ms
memory: 8532kb
input:
4 1 4 2 3
output:
2 4 1 3 2
result:
ok da
Test #17:
score: 0
Accepted
time: 1ms
memory: 8788kb
input:
4 1 4 3 2
output:
3 4 1 3 3 2 2
result:
ok da
Test #18:
score: 0
Accepted
time: 0ms
memory: 9268kb
input:
4 2 1 3 4
output:
2 2 4 1 3
result:
ok da
Test #19:
score: 0
Accepted
time: 1ms
memory: 9176kb
input:
4 2 1 4 3
output:
2 4 2 1 3
result:
ok da
Test #20:
score: 0
Accepted
time: 1ms
memory: 8916kb
input:
4 2 3 1 4
output:
2 3 2 1 4
result:
ok da
Test #21:
score: 0
Accepted
time: 2ms
memory: 8384kb
input:
4 2 3 4 1
output:
3 4 3 2 2 1 1
result:
ok da
Test #22:
score: 0
Accepted
time: 2ms
memory: 9004kb
input:
4 2 4 1 3
output:
2 4 2 1 3
result:
ok da
Test #23:
score: 0
Accepted
time: 1ms
memory: 9792kb
input:
4 2 4 3 1
output:
3 4 2 3 3 1 1
result:
ok da
Test #24:
score: 0
Accepted
time: 2ms
memory: 10012kb
input:
4 3 1 2 4
output:
2 3 4 2 1
result:
ok da
Test #25:
score: 0
Accepted
time: 1ms
memory: 8604kb
input:
4 3 1 4 2
output:
2 4 3 2 1
result:
ok da
Test #26:
score: 0
Accepted
time: 1ms
memory: 9728kb
input:
4 3 2 1 4
output:
3 3 4 2 2 1 1
result:
ok da
Test #27:
score: 0
Accepted
time: 1ms
memory: 9732kb
input:
4 3 2 4 1
output:
3 4 3 2 2 1 1
result:
ok da
Test #28:
score: 0
Accepted
time: 1ms
memory: 9720kb
input:
4 3 4 1 2
output:
2 4 3 2 1
result:
ok da
Test #29:
score: 0
Accepted
time: 0ms
memory: 8860kb
input:
4 3 4 2 1
output:
3 4 3 2 2 1 1
result:
ok da
Test #30:
score: 0
Accepted
time: 0ms
memory: 9212kb
input:
4 4 1 2 3
output:
3 4 4 3 2 1 1
result:
ok da
Test #31:
score: 0
Accepted
time: 1ms
memory: 9788kb
input:
4 4 1 3 2
output:
3 4 4 3 1 2 2
result:
ok da
Test #32:
score: 0
Accepted
time: 1ms
memory: 9728kb
input:
4 4 2 1 3
output:
3 4 4 2 3 1 1
result:
ok da
Test #33:
score: 0
Accepted
time: 0ms
memory: 9728kb
input:
4 4 2 3 1
output:
3 4 4 3 2 1 1
result:
ok da
Test #34:
score: 0
Accepted
time: 1ms
memory: 8608kb
input:
4 4 3 1 2
output:
3 4 4 3 3 2 1
result:
ok da
Test #35:
score: 0
Accepted
time: 1ms
memory: 8612kb
input:
4 4 3 2 1
output:
4 4 4 3 3 2 2 1 1
result:
ok da
Test #36:
score: 0
Accepted
time: 1ms
memory: 9136kb
input:
16 13 7 10 1 9 15 4 11 12 2 8 16 3 5 14 6
output:
8 16 15 14 13 12 11 10 7 9 1 8 4 3 2 6 5
result:
ok da
Test #37:
score: 0
Accepted
time: 0ms
memory: 8472kb
input:
16 12 16 11 15 10 9 8 4 14 13 7 2 6 5 3 1
output:
10 16 12 11 15 14 10 9 13 8 8 7 4 2 6 5 5 3 3 1 1
result:
ok da
Test #38:
score: 0
Accepted
time: 0ms
memory: 8588kb
input:
16 2 4 5 1 9 10 3 6 14 7 8 11 12 16 13 15
output:
8 16 14 10 9 5 4 2 3 1 8 7 6 13 12 11 15
result:
ok da
Test #39:
score: 0
Accepted
time: 2ms
memory: 9828kb
input:
16 13 7 10 1 9 15 4 11 12 2 8 16 3 5 14 6
output:
8 16 15 14 13 12 11 10 7 9 1 8 4 3 2 6 5
result:
ok da
Test #40:
score: 0
Accepted
time: 1ms
memory: 8312kb
input:
16 16 13 10 7 6 15 14 12 5 11 4 9 3 8 1 2
output:
9 16 16 15 13 10 14 12 7 6 11 9 5 4 8 3 3 2 1
result:
ok da
Test #41:
score: 0
Accepted
time: 0ms
memory: 9600kb
input:
16 2 3 1 8 12 4 5 6 7 14 15 9 10 16 11 13
output:
8 16 15 14 12 8 3 2 7 1 6 5 4 11 10 9 13
result:
ok da
Test #42:
score: 0
Accepted
time: 1ms
memory: 8468kb
input:
16 13 7 10 1 9 15 4 11 12 2 8 16 3 5 14 6
output:
8 16 15 14 13 12 11 10 7 9 1 8 4 3 2 6 5
result:
ok da
Test #43:
score: 0
Accepted
time: 2ms
memory: 9760kb
input:
16 16 13 10 15 8 14 12 7 4 11 9 6 1 5 3 2
output:
10 16 16 15 13 10 14 12 8 7 11 9 4 6 6 5 1 3 3 2 2
result:
ok da
Test #44:
score: 0
Accepted
time: 0ms
memory: 10024kb
input:
16 3 5 1 6 8 9 2 11 12 4 7 14 15 10 16 13
output:
8 16 15 14 12 11 9 8 6 5 3 2 1 4 7 10 13
result:
ok da
Test #45:
score: 0
Accepted
time: 0ms
memory: 9120kb
input:
16 13 7 10 1 9 15 4 11 12 2 8 16 3 5 14 6
output:
8 16 15 14 13 12 11 10 7 9 1 8 4 3 2 6 5
result:
ok da
Test #46:
score: 0
Accepted
time: 1ms
memory: 8576kb
input:
16 14 13 12 11 16 15 8 10 6 9 4 7 3 1 5 2
output:
9 16 14 13 15 12 12 11 11 10 8 6 9 7 4 3 5 2 1
result:
ok da
Test #47:
score: 0
Accepted
time: 2ms
memory: 9776kb
input:
16 1 7 2 3 9 11 4 5 6 12 15 8 10 16 13 14
output:
8 16 15 12 11 9 7 6 5 4 3 2 8 10 14 13 1
result:
ok da
Test #48:
score: 0
Accepted
time: 1ms
memory: 8444kb
input:
16 13 7 10 1 9 15 4 11 12 2 8 16 3 5 14 6
output:
8 16 15 14 13 12 11 10 7 9 1 8 4 3 2 6 5
result:
ok da
Test #49:
score: 0
Accepted
time: 1ms
memory: 8540kb
input:
16 14 13 16 11 9 15 12 6 5 10 8 7 2 1 4 3
output:
8 16 14 13 15 12 11 10 9 8 6 5 7 4 2 1 3
result:
ok da
Test #50:
score: 0
Accepted
time: 1ms
memory: 8412kb
input:
16 6 7 8 1 9 2 3 4 11 12 5 10 13 14 15 16
output:
8 12 11 9 8 7 6 5 4 3 2 1 10 16 15 14 13
result:
ok da
Test #51:
score: 0
Accepted
time: 0ms
memory: 9088kb
input:
16 13 7 10 1 9 15 4 11 12 2 8 16 3 5 14 6
output:
8 16 15 14 13 12 11 10 7 9 1 8 4 3 2 6 5
result:
ok da
Test #52:
score: 0
Accepted
time: 1ms
memory: 9152kb
input:
16 15 16 14 12 11 9 7 5 4 13 2 10 1 8 6 3
output:
10 16 15 14 14 13 12 11 11 10 9 8 7 6 5 4 4 2 3 1 1
result:
ok da
Test #53:
score: 0
Accepted
time: 0ms
memory: 8388kb
input:
16 1 6 2 7 8 9 10 13 3 4 5 11 14 12 16 15
output:
8 16 14 13 10 9 8 7 6 5 4 3 2 12 11 15 1
result:
ok da
Test #54:
score: -100
Wrong Answer
time: 0ms
memory: 8496kb
input:
495 237 201 155 129 345 454 113 11 492 357 300 295 198 442 14 79 288 431 343 64 285 101 316 15 34 293 3 393 384 47 296 402 488 328 128 409 110 72 249 115 386 450 167 214 489 227 172 220 336 59 206 315 278 63 395 478 490 165 164 303 449 145 31 418 119 179 373 320 93 255 183 38 58 491 375 416 430 326 ...
output:
3 100 98 90 69 47 99
result:
FAIL removed all buildings