# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
258599 | IOrtroiii | Swap (BOI16_swap) | C++14 | 62 ms | 5628 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>
using namespace std;
const int MX = 400400;
int N;
int A[MX];
int state[MX];
int ans[MX];
int query(int i) {
int ans = MX;
for (; i > 0; i /= 2) {
if (state[i] != 1) ans = min(ans, A[i]);
if (state[i] == 0) return ans;
if (i % 2 && state[i - 1] != 0) {
ans = min(ans, A[i - 1]);
if (state[i - 1] == 1) return ans;
}
}
}
void modify(int i, int val) {
for (; i > 0; i /= 2) {
if (A[i] == val) {
state[i] = 0;
return;
}
if (i % 2) {
if (A[i - 1] == val) {
state[i] = state[i - 1] = 1;
return;
} else state[i - 1] = 0;
}
state[i] = 1;
}
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(nullptr);
cin >> N;
for (int i = 1; i <= N; ++i) cin >> A[i];
for (int i = N + 1; i <= 2 * N + 1; ++i) A[i] = MX;
state[1] = 0; for (int i = 2; i <= 2 * N + 1; ++i) state[i] = -1;
for (int i = 1; i <= N; ++i) {
int val = query(i);
ans[i] = min({val, A[i * 2], A[i * 2 + 1]});
if (ans[i] == val) {
modify(i, val);
state[i * 2] = 0;
state[i * 2 + 1] = 0;
} else if (ans[i] == A[i * 2]) {
state[i * 2] = 1;
state[i * 2 + 1] = 0;
} else {
assert(ans[i] == A[i * 2 + 1]);
state[i * 2 + 1] = 1;
}
}
for (int i = 1; i <= N; ++i) cout << ans[i] << ' '; cout << '\n';
return 0;
}
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... |