【思路要点】
- 记给定序列可以分成的最少的值域连续的上升子序列的个数 CntCntCnt ,则有
Ans=⌈Log2Cnt⌉Ans=\lceil Log_2Cnt\rceilAns=⌈Log2Cnt⌉- 不难发现这也是答案的下界,考虑如何构造。
- 显然每次操作时,每一个上升子序列种的所有数或是同被操作,或是同不被操作。
- 因此方案的构造与 N,N−1,N−2,…,1N,N-1,N-2,\dots,1N,N−1,N−2,…,1 的方案等价。
- 对于第 iii 次操作,选定 j&2i−1=0j\&2^{i-1}=0j&2i−1=0 的 jjj 即可。
- 时间复杂度 O(N2)O(N^2)O(N2) 或 O(NLogN)O(NLogN)O(NLogN) 。
【代码】
#include<bits/stdc++.h> using namespace std; const int MAXN = 3005; const int MAXLOG = 15; typedef long long ll; typedef long double ld; typedef unsigned long long ull; template <typename T> void chkmax(T &x, T y) {x = max(x, y); } template <typename T> void chkmin(T &x, T y) {x = min(x, y); } template <typename T> void read(T &x) { x = 0; int f = 1; char c = getchar(); for (; !isdigit(c); c = getchar()) if (c == '-') f = -f; for (; isdigit(c); c = getchar()) x = x * 10 + c - '0'; x *= f; } template <typename T> void write(T x) { if (x < 0) x = -x, putchar('-'); if (x > 9) write(x / 10); putchar(x % 10 + '0'); } template <typename T> void writeln(T x) { write(x); puts(""); } bool sel[MAXLOG][MAXN]; int n, cnt, cmptype, a[MAXN], val[MAXN], bit[MAXLOG]; bool cmp(int x, int y) { if (sel[cmptype][x] == sel[cmptype][y]) return false; else return sel[cmptype][y]; } int main() { freopen("sort.in", "r", stdin); freopen("sort.out", "w", stdout); read(n); for (int i = 1; i <= n; i++) read(a[i]); int now = 1; while (now <= n) { for (int i = 1; i <= n; i++) if (a[i] == now) { val[now] = cnt; now++; } cnt++; } int ans = 0; while ((1 << ans) < cnt) ans++; printf("%d\n", ans); for (int i = 1; i <= ans; i++) { bit[i] = 1 << (i - 1); for (int j = 1; j <= n; j++) sel[i][j] = (val[j] & bit[i]) != 0; } for (int i = 1; i <= n; i++) printf("%d ", a[i]); printf("\n"); for (int i = 1; i <= ans; i++) { cmptype = i; stable_sort(a + 1, a + n + 1, cmp); for (int j = 1; j <= n; j++) printf("%d ", a[j]); printf("\n"); } return 0; }