PTA | 程序设计类实验辅助教学平台 (pintia.cn)
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
vector<int> arr;
int getPos(int st, int l, int target) {
int a = 0, b = 0, posa = 0, posb = 0;
for (int i = st; i < st + l / 2; i++) {
a = max(a, arr[i]);
if (arr[i] == 0) posa = i;
}
for (int i = st + l / 2; i < st + l; i++) {
b = max(b, arr[i]);
if (arr[i] == 0) posb = i;
}
if (min(a, b) > target) return -1;
return a < b ? posa : posb;
}
int main() {
int n, max_ele = 0;
cin >> n;
arr.resize(1 << n, 0);
for (int i = 1; i < 1 << n; i += 2) {
cin >> arr[i];
max_ele = max(max_ele, arr[i]);
}
for (int i = 2; i <= n; i++) {
int len = 1 << i;
for (int j = 0, target; j < 1 << n; j += len) {
cin >> target;
int pos = getPos(j, len, target);
max_ele = max(max_ele, target);
if (pos == -1) {
cout << "No Solution";
return 0;
}
arr[pos] = target;
}
}
cin >> n;
if (n < max_ele) {
cout << "No Solution";
return 0;
}
int pos = 0;
while (arr[pos]) pos += 2;
arr[pos] = n;
int f = 0;
for (int x : arr) {
if (f) cout << " ";
cout << x;
f = 1;
}
return 0;
}