# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
544729 | rainboy | Swap (BOI16_swap) | C11 | 56 ms | 4000 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |