题解:P15269 「UTOI 1D」Flowerfell
发现形如 x x ... x 1 2 1 2 x x ... x 的构造方案很优(x 可以为任意数字),设左边有 x,右边有 x,那么
但是如果
发现
当 3 4 3 3 3 ... 3 1 2 1 2 5 5 5 ... 5,注意第二个数是
那么当
但是这个构造必须满足开头的数为 3 4 3,即
代码如下:
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int d, t;
cin >> d >> t;
while (t--) {
int n;
cin >> n;
int mn = 1e9, x = 0, y = 0;
for (int i = 1; i <= n; i++) {
for (int j = i; 1ll * i * j <= n; j++) {
int k = n - i * j;
if (i + j + k < mn) mn = i + j + k, x = i, y = j;
}
}
for (int i = 1; i <= n; i++) {
int j = (n - 1) / i + 1;
if (i + j < mn && i >= 3 && j >= 3) mn = i + j, x = i, y = j;
}
if (x < y) swap(x, y);
vector<int> ans;
if (x * y <= n) {
for (int i = 1; i < x; i++) ans.emplace_back(3);
ans.emplace_back(1), ans.emplace_back(2), ans.emplace_back(1), ans.emplace_back(2);
for (int i = 1; i < y; i++) ans.emplace_back(4);
if (x * y ^ n) ans.emplace_back(1);
for (int i = 1; i < n - x * y; i++) ans.emplace_back(5);
}
else {
int z = x * y - n;
ans.emplace_back(3), ans.emplace_back(4);
for (int i = 1; i < x - 2; i++) ans.emplace_back(3);
ans.emplace_back(1), ans.emplace_back(2), ans.emplace_back(1), ans.emplace_back(2);
for (int i = 1; i < y - z; i++) ans.emplace_back(5);
for (int i = 1; i <= z; i++) ans.emplace_back(4);
}
cout << ans.size() << "\n";
for (auto i : ans) cout << i << " ";
cout << "\n";
}
return 0;
}