-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathcoci07c2p4.cpp
77 lines (77 loc) · 1.86 KB
/
coci07c2p4.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Ivan Carvalho
// Solution to https://dmoj.ca/problem/coci07c2p4
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 10;
const int BUCKET = 320;
deque<int> balde[BUCKET + 20];
int n, m, qualbalde[MAXN];
void balance_insert() {
int idx = 0;
while (true) {
if (balde[idx].empty()) break;
if (balde[idx].size() > BUCKET) {
balde[idx + 1].push_front(balde[idx].back());
qualbalde[balde[idx].back()] = idx + 1;
balde[idx].pop_back();
}
idx++;
}
}
void balance_remove() {
int idx = 0;
while (true) {
if (balde[idx + 1].empty()) break;
if (balde[idx].size() < BUCKET) {
balde[idx].push_back(balde[idx + 1].front());
qualbalde[balde[idx + 1].front()] = idx;
balde[idx + 1].pop_front();
}
idx++;
}
}
void search_delte_show(int u) {
int bucket_id = qualbalde[u];
int pos = qualbalde[u] * BUCKET;
int dist = 0;
deque<int>::iterator it = balde[bucket_id].begin();
while (true) {
if ((*it) == u) break;
dist++;
it++;
}
printf("%d\n", abs(pos + dist - u));
balde[bucket_id].erase(it);
balance_remove();
qualbalde[u] = u / BUCKET;
it = balde[qualbalde[u]].begin() + (u % BUCKET);
balde[qualbalde[u]].insert(it, u);
balance_insert();
}
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++) {
int x;
scanf("%d", &x);
x--;
qualbalde[x] = i / BUCKET;
balde[qualbalde[x]].push_back(x);
}
int ini = 0, fim = n - 1, flag = 0;
while (ini <= fim) {
int davez;
if (flag == 0) {
davez = ini;
ini++;
flag = 1;
} else {
davez = fim;
fim--;
flag = 0;
}
search_delte_show(davez);
}
return 0;
}
0;
}