This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
#define TASK ""
#define bit(x) (1LL << (x))
#define getbit(x, i) (((x) >> (i)) & 1)
#define ALL(x) (x).begin(), (x).end()
using namespace std;
template <typename T1, typename T2> bool mini(T1 &a, T2 b) {
if (a > b) {a = b; return true;} return false;
}
template <typename T1, typename T2> bool maxi(T1 &a, T2 b) {
if (a < b) {a = b; return true;} return false;
}
mt19937_64 rd(chrono::steady_clock::now().time_since_epoch().count());
int rand(int l, int r) {
return l + rd() % (r - l + 1);
}
const int N = 4e5 + 5;
const int oo = 1e9;
const long long ooo = 1e18;
const int mod = 1e9 + 7; // 998244353;
const long double pi = acos(-1);
int dp[1005][1005];
int a[N];
int n;
int solve(int v, int pos) {
if (pos > n)
return pos;
// cerr << v << " " << pos << "\n";
int &res = dp[v][pos];
if (res != -1)
return res;
int mn = min({v, a[pos << 1], a[pos << 1 | 1]});
if (v == mn)
return res = pos;
if (a[pos << 1] == mn)
return res = solve(v, pos << 1);
return res = min(solve(v, pos << 1), solve(v, pos << 1 | 1));
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (int i = 1; i <= n; i++)
cin >> a[i];
for (int i = n + 1; i <= 2 * n + 1; i++)
a[i] = oo;
memset(dp, -1, sizeof(dp));
for (int i = 1; i <= n; i++) {
int mn = min({a[i], a[i << 1], a[i << 1 | 1]});
if (a[i] == mn)
continue;
if (a[i << 1] == mn) {
swap(a[i], a[i << 1]);
continue;
}
swap(a[i], a[i << 1 | 1]);
int l = i << 1;
int r = i << 1 | 1;
if (a[l] > a[r])
swap(a[l], a[r]);
if (solve(a[l], l) > solve(a[l], r))
swap(a[l], a[r]);
}
for (int i = 1; i <= n; i++)
cout << a[i] << " ";
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |