This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// Source: https://oj.uz/problem/view/BOI16_swap
//
#include "bits/stdc++.h"
using namespace std;
#define s second
#define f first
#define pb push_back
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<pii> vpii;
typedef vector<int> vi;
#define FOR(i, a, b) for (int i = (a); i<b; i++)
const int N = 1e5;
int a[N];
bool vis[N];
set<int> s[N];
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
FOR(i, 1, n+1) {
cin >> a[i];
s[i].insert(a[i]);
}
FOR(i, 1, n+1) {
while (vis[*s[i].begin()]) s[i].erase(s[i].begin());
// cout << i << endl;
if (2 * i + 1 <= n) assert(s[2*i].size() == 1 && s[2*i + 1].size() == 1);
int opt = *s[i].begin();
if (2*i <= n) opt = min(*s[2*i].begin(), opt);
if (2 * i + 1 <= n) opt = min(*s[2*i+1].begin(), opt);
if (2*i <= n && *s[2 * i].begin() == opt) swap(s[i], s[2*i]);
if (2 * i + 1 && *s[2*i + 1].begin() == opt) {
swap(s[i], s[2*i+1]);
s[2*i+1].insert(*s[2*i].begin());
s[2*i] = s[2*i + 1];
}
vis[*s[i].begin()]=true;
cout << (*s[i].begin()) << ' ';
}
cout << endl;
}
# | 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... |