# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
544729 | 2022-04-02T16:13:31 Z | rainboy | Swap (BOI16_swap) | C | 56 ms | 4000 KB |
#include <stdio.h> #include <string.h> #define N 200000 int ancestor(int i, int j) { return i == j || i < j && ancestor(i, j / 2); } int main() { static int aa[1 + N]; static char swap[1 + N * 2]; int n, i, j; scanf("%d", &n); for (i = 1; i <= n; i++) scanf("%d", &aa[i]); memset(swap, -1, n * sizeof *swap); for (j = 1; j <= n; j++) { int j_; i = j * 2 > n ? -1 : (j * 2 < n && aa[j * 2 + 1] < aa[j * 2] ? j * 2 + 1 : j * 2); for (j_ = j; j_ >= 1; j_ /= 2) if ((j_ & 1) == 0) { if (swap[j_] != 1 && (i == -1 || aa[i] > aa[j_])) i = j_; if (swap[j_] == 0) break; } else { if (swap[j_] != 1 && (i == -1 || aa[i] > aa[j_])) i = j_; if (j_ > 1 && swap[j_ ^ 1] != 0 && swap[j_] != 0 && (i == -1 || aa[i] > aa[j_ ^ 1])) i = j_ ^ 1; if (j_ == 1 || swap[j_ ^ 1] == 1 || swap[j_] == 0) break; } if (i == j * 2) swap[j * 2] = 1, swap[j * 2 + 1] = 0; else if (i == j * 2 + 1) swap[j * 2 + 1] = 1; else { swap[j * 2] = swap[j * 2 + 1] = 0; if (ancestor(i, j)) swap[i] = 0; else swap[i ^ 1] = swap[i] = 1, i ^= 1; for (j_ = j; j_ != i; j_ /= 2) { if ((j_ & 1) == 1) swap[j_ ^ 1] = 0; swap[j_] = 1; } } } for (i = 2; i <= n; i++) { int tmp; if (swap[i]) tmp = aa[i], aa[i] = aa[i / 2], aa[i / 2] = tmp; } for (i = 1; i <= n; i++) printf("%d ", aa[i]); printf("\n"); return 0; }
Compilation message
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 0 ms | 212 KB | Output is correct |
2 | Correct | 0 ms | 212 KB | Output is correct |
3 | Correct | 0 ms | 212 KB | Output is correct |
4 | Correct | 0 ms | 296 KB | Output is correct |
5 | Correct | 0 ms | 212 KB | Output is correct |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 0 ms | 212 KB | Output is correct |
2 | Correct | 0 ms | 212 KB | Output is correct |
3 | Correct | 0 ms | 212 KB | Output is correct |
4 | Correct | 0 ms | 296 KB | Output is correct |
5 | Correct | 0 ms | 212 KB | Output is correct |
6 | Correct | 0 ms | 212 KB | Output is correct |
7 | Correct | 0 ms | 296 KB | Output is correct |
8 | Correct | 1 ms | 212 KB | Output is correct |
9 | Correct | 0 ms | 212 KB | Output is correct |
10 | Correct | 0 ms | 212 KB | Output is correct |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 0 ms | 212 KB | Output is correct |
2 | Correct | 0 ms | 212 KB | Output is correct |
3 | Correct | 0 ms | 212 KB | Output is correct |
4 | Correct | 0 ms | 296 KB | Output is correct |
5 | Correct | 0 ms | 212 KB | Output is correct |
6 | Correct | 0 ms | 212 KB | Output is correct |
7 | Correct | 0 ms | 296 KB | Output is correct |
8 | Correct | 1 ms | 212 KB | Output is correct |
9 | Correct | 0 ms | 212 KB | Output is correct |
10 | Correct | 0 ms | 212 KB | Output is correct |
11 | Correct | 1 ms | 276 KB | Output is correct |
12 | Correct | 1 ms | 212 KB | Output is correct |
13 | Correct | 1 ms | 296 KB | Output is correct |
14 | Correct | 1 ms | 212 KB | Output is correct |
15 | Correct | 1 ms | 212 KB | Output is correct |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 0 ms | 212 KB | Output is correct |
2 | Correct | 0 ms | 212 KB | Output is correct |
3 | Correct | 0 ms | 212 KB | Output is correct |
4 | Correct | 0 ms | 296 KB | Output is correct |
5 | Correct | 0 ms | 212 KB | Output is correct |
6 | Correct | 0 ms | 212 KB | Output is correct |
7 | Correct | 0 ms | 296 KB | Output is correct |
8 | Correct | 1 ms | 212 KB | Output is correct |
9 | Correct | 0 ms | 212 KB | Output is correct |
10 | Correct | 0 ms | 212 KB | Output is correct |
11 | Correct | 1 ms | 276 KB | Output is correct |
12 | Correct | 1 ms | 212 KB | Output is correct |
13 | Correct | 1 ms | 296 KB | Output is correct |
14 | Correct | 1 ms | 212 KB | Output is correct |
15 | Correct | 1 ms | 212 KB | Output is correct |
16 | Correct | 16 ms | 1108 KB | Output is correct |
17 | Correct | 14 ms | 1108 KB | Output is correct |
18 | Correct | 13 ms | 1080 KB | Output is correct |
19 | Correct | 14 ms | 1076 KB | Output is correct |
20 | Correct | 14 ms | 1072 KB | Output is correct |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 0 ms | 212 KB | Output is correct |
2 | Correct | 0 ms | 212 KB | Output is correct |
3 | Correct | 0 ms | 212 KB | Output is correct |
4 | Correct | 0 ms | 296 KB | Output is correct |
5 | Correct | 0 ms | 212 KB | Output is correct |
6 | Correct | 0 ms | 212 KB | Output is correct |
7 | Correct | 0 ms | 296 KB | Output is correct |
8 | Correct | 1 ms | 212 KB | Output is correct |
9 | Correct | 0 ms | 212 KB | Output is correct |
10 | Correct | 0 ms | 212 KB | Output is correct |
11 | Correct | 1 ms | 276 KB | Output is correct |
12 | Correct | 1 ms | 212 KB | Output is correct |
13 | Correct | 1 ms | 296 KB | Output is correct |
14 | Correct | 1 ms | 212 KB | Output is correct |
15 | Correct | 1 ms | 212 KB | Output is correct |
16 | Correct | 16 ms | 1108 KB | Output is correct |
17 | Correct | 14 ms | 1108 KB | Output is correct |
18 | Correct | 13 ms | 1080 KB | Output is correct |
19 | Correct | 14 ms | 1076 KB | Output is correct |
20 | Correct | 14 ms | 1072 KB | Output is correct |
21 | Correct | 48 ms | 3952 KB | Output is correct |
22 | Correct | 47 ms | 4000 KB | Output is correct |
23 | Correct | 49 ms | 3908 KB | Output is correct |
24 | Correct | 56 ms | 3912 KB | Output is correct |
25 | Correct | 55 ms | 3916 KB | Output is correct |