题解:P12386 [蓝桥杯 2023 省 Python B] 混乱的数组
对着想了半天,感觉还是打表找规律题。
首先考虑如果要求
现在去掉
我们尝试对
可以再尝试几种情况,比如
然后前面其实是
#include <iostream>
using namespace std;
int main () {
cin.tie(0)->sync_with_stdio(0);
int x, n = 2;
cin >> x;
for (; n * (n - 1) / 2 < x; ++n);
cout << n << '\n';
int v = 1 + x - (n - 1) * (n - 2) / 2;
const static int kN = 2e5 + 5;
static int a[kN];
a[1] = v;
if (v <= n / 2) {
int i = n;
for (int j = 1; j < v; i -= 2, ++j) a[i] = a[i - 1] = j;
a[i--] = v;
for (int j = v + 1; i > 1; --i, ++j) a[i] = j;
}
else {
for (int i = n, j = 1; i > 1; ++j) {
if (i - 1 != v - j) a[i] = j, --i;
a[i] = j, --i;
}
}
for (int i = 1; i <= n; ++i) cout << a[i] << ' ';
cout << '\n';
return 0;
}