记录编号 |
602282 |
评测结果 |
AAAAAAAAAA |
题目名称 |
4166.遵循指令之意 |
最终得分 |
100 |
用户昵称 |
LikableP |
是否通过 |
通过 |
代码语言 |
C++ |
运行时间 |
1.677 s |
提交时间 |
2025-07-03 14:39:23 |
内存使用 |
8.32 MiB |
显示代码纯文本
#include <algorithm>
#include <cstdio>
#include <cstring>
template <typename T> T read();
template <typename T> void write(T, char);
void work(int[], int, int &);
const int MAXN = 1e6 + 10;
int n;
int arr[MAXN], a[MAXN >> 1], b[MAXN >> 1], acnt, bcnt, cnt;
int ansarr, ansa, ansb;
int main() {
freopen("sort.in", "r", stdin);
freopen("sort.out", "w", stdout);
n = read<int>();
for (int i = 1; i <= n; ++i) {
arr[i] = read<int>();
if (i & 1) a[++acnt] = arr[i];
if (!(i & 1)) b[++bcnt] = arr[i];
}
work(arr, n, ansarr);
work(a, acnt, ansa);
work(b, bcnt, ansb);
::std::sort(a + 1, a + acnt + 1);
::std::sort(b + 1, b + bcnt + 1);
for (int i = 1; i <= bcnt; ++i) {
arr[++cnt] = a[i];
arr[++cnt] = b[i];
}
if (acnt > bcnt) arr[++cnt] = a[acnt];
// printf("%d %d %d\n", ansarr, ansa, ansb);
if ((ansa & 1) != (ansb & 1)) ::std::swap(arr[cnt], arr[cnt - 2]);
for (int i = 1; i <= cnt; ++i) {
write(arr[i], " \n"[i == cnt]);
}
return 0;
}
int tree[MAXN];
void add(int x, int y) {
for (; x <= MAXN - 10; x += (x & -x)) tree[x] += y;
}
int ask(int x) {
int res = 0;
for (; x; x -= (x & -x)) res += tree[x];
return res;
}
void work(int arr[], int len, int &ans) {
memset(tree, 0, sizeof tree);
ans = 0;
for (int i = len; i >= 1; --i) {
ans += ask(arr[i] - 1);
add(arr[i], 1);
}
}
#define isdigit(ch) (ch >= '0' && ch <= '9')
template <typename T> T read() {
T res = 0, f = 1;
char ch = getchar();
for (; !isdigit(ch); ch = getchar())
if (ch == '-') f = -1;
for (; isdigit(ch); ch = getchar()) res = (res << 3) + (res << 1) + (ch ^ 48);
return res * f;
}
template <typename T> void write(T x, char ed) {
if (x < 0) x = -x, putchar('-');
static int sta[sizeof(T) << 2], top = 0;
do {
sta[++top] = x % 10;
x /= 10;
} while (x);
while (top) {
putchar(sta[top--] ^ 48);
}
putchar(ed);
}