mirror of
https://git.sb/baoshuo/OI-codes.git
synced 2024-11-08 15:18:46 +00:00
1552. [Cerc2007] robotic sort
https://hydro.ac/d/bzoj/record/6322e9fabdf9bc31d1609bbc
This commit is contained in:
parent
7579397dc7
commit
e429788bf5
183
BZOJ/1552/1552.cpp
Normal file
183
BZOJ/1552/1552.cpp
Normal file
@ -0,0 +1,183 @@
|
||||
#include <iostream>
|
||||
#include <algorithm>
|
||||
#include <chrono>
|
||||
#include <limits>
|
||||
|
||||
using std::cin;
|
||||
using std::cout;
|
||||
const char endl = '\n';
|
||||
|
||||
const int N = 1e5 + 5;
|
||||
|
||||
struct node {
|
||||
node *lchild, *rchild;
|
||||
size_t size;
|
||||
unsigned key;
|
||||
int value, min;
|
||||
bool reversed;
|
||||
|
||||
node()
|
||||
: lchild(nullptr),
|
||||
rchild(nullptr),
|
||||
size(0),
|
||||
key(rand()),
|
||||
value(0),
|
||||
min(std::numeric_limits<int>::max()),
|
||||
reversed(false) {}
|
||||
|
||||
node(int _value)
|
||||
: lchild(nullptr),
|
||||
rchild(nullptr),
|
||||
size(1),
|
||||
key(rand()),
|
||||
value(_value),
|
||||
min(_value),
|
||||
reversed(false) {}
|
||||
|
||||
~node() {
|
||||
if (lchild != nullptr) delete lchild;
|
||||
if (rchild != nullptr) delete rchild;
|
||||
}
|
||||
|
||||
inline size_t lsize() {
|
||||
return lchild == nullptr ? 0 : lchild->size;
|
||||
}
|
||||
|
||||
inline size_t rsize() {
|
||||
return rchild == nullptr ? 0 : rchild->size;
|
||||
}
|
||||
|
||||
inline void pushup() {
|
||||
size = lsize() + 1 + rsize();
|
||||
min = value;
|
||||
|
||||
if (lchild != nullptr) {
|
||||
min = std::min(min, lchild->min);
|
||||
}
|
||||
|
||||
if (rchild != nullptr) {
|
||||
min = std::min(min, rchild->min);
|
||||
}
|
||||
}
|
||||
|
||||
inline void pushdown() {
|
||||
if (reversed) {
|
||||
std::swap(lchild, rchild);
|
||||
if (lchild != nullptr) lchild->reversed = !lchild->reversed;
|
||||
if (rchild != nullptr) rchild->reversed = !rchild->reversed;
|
||||
reversed = false;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
int n, b[N];
|
||||
std::pair<int, int> a[N];
|
||||
node *root;
|
||||
|
||||
std::pair<node *, node *> split(node *u, int k) {
|
||||
if (u == nullptr) return std::make_pair(nullptr, nullptr);
|
||||
|
||||
u->pushdown();
|
||||
|
||||
if (k <= u->lsize()) {
|
||||
auto o = split(u->lchild, k);
|
||||
|
||||
u->lchild = o.second;
|
||||
u->pushup();
|
||||
o.second = u;
|
||||
|
||||
return o;
|
||||
}
|
||||
|
||||
auto o = split(u->rchild, k - u->lsize() - 1);
|
||||
|
||||
u->rchild = o.first;
|
||||
u->pushup();
|
||||
o.first = u;
|
||||
|
||||
return o;
|
||||
}
|
||||
|
||||
node *merge(node *x, node *y) {
|
||||
if (x == nullptr) return y;
|
||||
if (y == nullptr) return x;
|
||||
|
||||
if (x->key < y->key) {
|
||||
x->pushdown();
|
||||
x->rchild = merge(x->rchild, y);
|
||||
x->pushup();
|
||||
|
||||
return x;
|
||||
}
|
||||
|
||||
y->pushdown();
|
||||
y->lchild = merge(x, y->lchild);
|
||||
y->pushup();
|
||||
|
||||
return y;
|
||||
}
|
||||
|
||||
void reverse(int k) {
|
||||
auto x = split(root, k);
|
||||
auto y = split(x.first, k - 1);
|
||||
if (y.first != nullptr) y.first->reversed = !y.first->reversed;
|
||||
delete y.second;
|
||||
root = merge(y.first, x.second);
|
||||
}
|
||||
|
||||
int find(node *p) {
|
||||
int k = 1;
|
||||
|
||||
while (p != nullptr) {
|
||||
p->pushdown();
|
||||
|
||||
if (p->lchild != nullptr && p->min == p->lchild->min) {
|
||||
p = p->lchild;
|
||||
} else if (p->rchild != nullptr && p->min == p->rchild->min) {
|
||||
k += p->lsize() + 1;
|
||||
p = p->rchild;
|
||||
} else {
|
||||
return k + p->lsize();
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
int main() {
|
||||
std::ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
cin >> n;
|
||||
|
||||
for (int i = 1; i <= n; i++) {
|
||||
cin >> a[i].first;
|
||||
|
||||
a[i].second = i;
|
||||
}
|
||||
|
||||
std::sort(a + 1, a + 1 + n);
|
||||
|
||||
for (int i = 1; i <= n; i++) {
|
||||
b[a[i].second] = i;
|
||||
}
|
||||
|
||||
for (int i = 1; i <= n; i++) {
|
||||
root = merge(root, new node(b[i]));
|
||||
}
|
||||
|
||||
for (int i = 1; i <= n; i++) {
|
||||
int k = find(root);
|
||||
|
||||
reverse(k);
|
||||
|
||||
cout << k + i - 1 << ' ';
|
||||
}
|
||||
|
||||
cout << endl;
|
||||
|
||||
// Cleanup
|
||||
delete root;
|
||||
|
||||
return 0;
|
||||
}
|
BIN
BZOJ/1552/data/1.in
(Stored with Git LFS)
Normal file
BIN
BZOJ/1552/data/1.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
BZOJ/1552/data/1.out
(Stored with Git LFS)
Normal file
BIN
BZOJ/1552/data/1.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
BZOJ/1552/data/10.in
(Stored with Git LFS)
Normal file
BIN
BZOJ/1552/data/10.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
BZOJ/1552/data/10.out
(Stored with Git LFS)
Normal file
BIN
BZOJ/1552/data/10.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
BZOJ/1552/data/11.in
(Stored with Git LFS)
Normal file
BIN
BZOJ/1552/data/11.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
BZOJ/1552/data/11.out
(Stored with Git LFS)
Normal file
BIN
BZOJ/1552/data/11.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
BZOJ/1552/data/2.in
(Stored with Git LFS)
Normal file
BIN
BZOJ/1552/data/2.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
BZOJ/1552/data/2.out
(Stored with Git LFS)
Normal file
BIN
BZOJ/1552/data/2.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
BZOJ/1552/data/3.in
(Stored with Git LFS)
Normal file
BIN
BZOJ/1552/data/3.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
BZOJ/1552/data/3.out
(Stored with Git LFS)
Normal file
BIN
BZOJ/1552/data/3.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
BZOJ/1552/data/4.in
(Stored with Git LFS)
Normal file
BIN
BZOJ/1552/data/4.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
BZOJ/1552/data/4.out
(Stored with Git LFS)
Normal file
BIN
BZOJ/1552/data/4.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
BZOJ/1552/data/5.in
(Stored with Git LFS)
Normal file
BIN
BZOJ/1552/data/5.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
BZOJ/1552/data/5.out
(Stored with Git LFS)
Normal file
BIN
BZOJ/1552/data/5.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
BZOJ/1552/data/6.in
(Stored with Git LFS)
Normal file
BIN
BZOJ/1552/data/6.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
BZOJ/1552/data/6.out
(Stored with Git LFS)
Normal file
BIN
BZOJ/1552/data/6.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
BZOJ/1552/data/7.in
(Stored with Git LFS)
Normal file
BIN
BZOJ/1552/data/7.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
BZOJ/1552/data/7.out
(Stored with Git LFS)
Normal file
BIN
BZOJ/1552/data/7.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
BZOJ/1552/data/8.in
(Stored with Git LFS)
Normal file
BIN
BZOJ/1552/data/8.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
BZOJ/1552/data/8.out
(Stored with Git LFS)
Normal file
BIN
BZOJ/1552/data/8.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
BZOJ/1552/data/9.in
(Stored with Git LFS)
Normal file
BIN
BZOJ/1552/data/9.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
BZOJ/1552/data/9.out
(Stored with Git LFS)
Normal file
BIN
BZOJ/1552/data/9.out
(Stored with Git LFS)
Normal file
Binary file not shown.
Loading…
Reference in New Issue
Block a user