题目链接:https://www.lanqiao.cn/problems/3550/learning/
个人评价:难度 3 星(满星:5)
前置知识:递归,找规律,组合数
整体思路
- 首先写出暴力打表代码找出前 37 37 37 个逆序数对应的答案数组,尝试找规律(打表代码见下文),尽量取 x x x 较大的进行观察:
x = 28: 8 7 6 5 4 3 2 1
x = 29: 2 7 6 5 4 3 2 1 1
x = 30: 3 6 5 4 3 2 2 1 1
x = 31: 4 5 4 3 3 2 2 1 1
x = 32: 5 4 4 3 3 2 2 1 1
x = 33: 6 5 4 3 3 2 2 1 1
x = 34: 7 6 5 4 3 2 2 1 1
x = 35: 8 7 6 5 4 3 2 1 1
x = 36: 9 8 7 6 5 4 3 2 1
- 可以发现,当 x x x 正好等于 C n 2 C_n^2 Cn2 时,恰好是一个长度为 n n n 的全逆序排列,且此时逆序数最大,其它情况需要满足 x ≥ C n 2 x\geq C_n^2 x≥Cn2,即 x ≥ n ( n − 1 ) 2 x\geq\frac{n(n-1)}{2} x≥2n(n−1),解得 n ≥ 1 + 1 + 8 x 2 n\geq\frac{1+\sqrt{1+8x}}{2} n≥21+1+8x,由于 n n n 为整数,所以 n = ⌈ 1 + 1 + 8 x 2 ⌉ n=\lceil\frac{1+\sqrt{1+8x}}{2}\rceil n=⌈21+1+8x⌉;
- 观察中间的情况,从 x = 29 x=29 x=29 到 x = 35 x=35 x=35 第一个数字依次递增,等于 x − C n − 1 2 + 1 x - C_{n-1}^2 + 1 x−Cn−12+1( n n n 为序列长度);
- 从第二个数字开始依次递减,在某个位置变为两个数字两个数字地递减,我们需要找到这个递减的中间位置的规律,其它数字都好计算;
- 两两递减的序列以 x = 32 x=32 x=32 为分界, x ∈ [ 29 , 32 ] x\in[29, 32] x∈[29,32] 两两递减序列逐渐变长, x ∈ [ 33 , 35 ] x\in[33,35] x∈[33,35] 两两递减序列逐渐变短;
- 当 x − C n − 1 2 ≤ n − 1 2 x-C_{n-1}^2\leq \frac{n-1}{2} x−Cn−12≤2n−1 时(即 x ∈ [ 29 , 32 ] x\in[29,32] x∈[29,32],这里取 n − 1 n-1 n−1 除以 2 2 2 而不是 n n n 除以 2 2 2 是为了与 n n n 为偶数时的规律统一),后面 2 ( x − C n − 1 2 ) 2(x-C_{n-1}^2) 2(x−Cn−12) 个数字从 1 1 1 开始倒着往前两两递增,其余数字依次递增;
- 当 x − C n − 1 2 > n − 1 2 x-C_{n-1}^2>\frac{n-1}{2} x−Cn−12>2n−1 时(即 x ∈ [ 33 , 35 ] x\in[33,35] x∈[33,35]),后面 2 ( C n 2 − x ) 2(C_n^2-x) 2(Cn2−x) 个数字两两往前倒着递增,其余数字依次递增。
过题代码
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = 100000 + 100;
LL n, x, mid;
LL ans[maxn];
int main() {
#ifdef ExRoc
freopen("test.txt", "r", stdin);
#endif // ExRoc
cin >> x;
// 计算数组长度 n
n = ceil((1 + sqrt(1 + 8 * x)) / 2);
cout << n << endl;
// 计算 C_n^2 与 C_{n-1}^2
LL Cn2 = n * (n - 1) / 2;
LL Cnn2 = (n - 1) * (n - 2) / 2;
if (x - Cnn2 <= (n - 1) / 2) {
// x \in [29, 32] 的情况
mid = 2 * (x - Cnn2);
} else {
// x \in [33, 35] 的情况
mid = 2 * (Cn2 - x);
}
// 两两递增的情况
for (int i = 1; i <= mid; ++i) {
ans[i] = (i + 1) / 2;
}
// 依次递增的情况
for (int i = mid + 1; i < n; ++i) {
ans[i] = mid / 2 + i - mid;
}
// 第一位总是递增的
ans[n] = x - Cnn2 + 1;
// 以上为代码逻辑方便,逆序放入,这里统一反转数组
reverse(ans + 1, ans + 1 + n);
// 输出答案
for (int i = 1; i <= n; ++i) {
cout << ans[i] << " ";
}
cout << endl;
return 0;
}
打表代码
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = 100 + 100;
bool flag;
int num[maxn];
// 判断生成的长度为 n 的数组是否恰好有 n 个逆序对
bool judge(int x, int n) {
int cnt = 0;
for (int i = 1; i <= n; ++i) {
for (int j = i + 1; j <= n; ++j) {
if (num[i] > num[j]) {
++cnt;
}
}
}
return cnt == x;
}
// 暴力递归枚举所有可能的数组
void dfs(int depth, int x, int n) {
if (depth == n + 1) {
// 找到的第一组就是字典序最小的数组
flag = judge(x, n);
if (flag) {
for (int i = 1; i <= n; ++i) {
cout << num[i] << " ";
}
cout << endl;
}
return ;
}
for (int i = 1; i <= n; ++i) {
num[depth] = i;
dfs(depth + 1, x, n);
// 只要找到第一组,后续就不用再继续搜索了
if (flag) {
return ;
}
}
}
bool solve(int x, int n) {
flag = false;
dfs(1, x, n);
return flag;
}
void solve(int x) {
for (int n = 2; n <= 10; ++n) {
if (solve(x, n)) {
return ;
}
}
}
int main() {
#ifdef ExRoc
freopen("test.txt", "r", stdin);
#endif // ExRoc
for (int i = 1; i <= 37; ++i) {
cout << "x = " << i << ": ";
solve(i);
}
return 0;
}