P1177 【模板】快速排序

可以用快排的板测

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
#include<bits/stdc++.h>
using namespace std;
const int MAXn = 100000;

inline int read() {
register char c;
while (c = getchar(), c < '0' || c>'9');
register int x(c - '0');
while (c = getchar(), c >= '0' && c <= '9') {
x = x * 10 + c - '0';
}
return x;
}

int heap[MAXn+10];
int heapn;
int n;
void up(int p) {
int f = p / 2;
while (p > 1) {
if (heap[p] < heap[f]) {
swap(heap[p], heap[f]);
p = f;
f /= 2;
}
else break;
}
}
void down(int p) {
int s = p * 2;
while (s <= heapn) {
if (heap[s] > heap[s + 1] && s < heapn) {
s++;
}
if (heap[s] < heap[p]) {
swap(heap[s], heap[p]);
p = s;
s *= 2;
}
else break;
}
}
void insert(int x) {
heap[++heapn] = x;
up(heapn);
}
void pop(int p) {
heap[p] = heap[heapn--];
up(p);
down(p);
}
void pop_root() {
heap[1] = heap[heapn--];
down(1);
}
int get_root() {
return heap[1];
}

int main(){
n = read();
for(int i = 0; i < n; i++) {
insert(read());
}
for(int i = 0; i < n; i++) {
printf("%d ", get_root());
pop_root();
}
}