# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
240310 | karma | Swap (BOI16_swap) | C++14 | 62 ms | 7288 KiB |
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 pb emplace_back
#define ll long long
#define fi first
#define se second
#define mp make_pair
//#define int int64_t
using namespace std;
const int N = int(5e5) + 7;
const int logN = 18;
typedef pair<int, int> pii;
int n, a[N], b[N], can[N];
int get(int x) {
for(int res = 1e9; ; x >>= 1) {
if(can[x] != 1) res = min(res, a[x]);
if(can[x] == 0) return res;
if((x & 1) && can[x ^ 1]) {
res = min(res, a[x ^ 1]);
if(can[x ^ 1] == 1) return res;
}
}
return 1e9;
}
void change(int x, int v) {
for(;; x >>= 1) {
if(a[x] == v) {
can[x] = 0;
return;
}
if(x & 1) {
if(a[x ^ 1] == v) {
can[x] = can[x ^ 1] = 1;
return;
} else can[x ^ 1] = 0;
}
can[x] = 1;
}
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
#define Task "test"
if(fopen(Task".inp", "r")) {
freopen(Task".inp", "r", stdin);
freopen(Task".out", "w", stdout);
}
cin >> n; fill_n(a, N, 1e9);
for(int i = 1; i <= n; ++i) cin >> a[i], can[i] = i == 1? 0: 2;
for(int x, i = 1; i <= n; ++i) {
b[i] = min({x = get(i), a[i << 1], a[i << 1 | 1]});
if(b[i] == x) {
change(i, x);
can[i << 1] = can[i << 1 | 1] = 0;
} else if(b[i] == a[i << 1]) can[i << 1] = 1, can[i << 1 | 1] = 0;
else can[i << 1 | 1] = 1;
}
for(int i = 1; i <= n; ++i) cout << b[i] << ' ';
}
Compilation message (stderr)
# | 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... |