# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
27104 | tlwpdus | Swap (BOI16_swap) | C++11 | 79 ms | 6708 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;
int n, arr[400100], brr[400100], used[400100], INF = 987654321;
int getv(int idx) {
int res = INF;
while(idx>0) {
if (used[idx]!=1) res = min(res,arr[idx]);
if (!used[idx]) break;
if ((idx&1)&&used[idx^1]) {
res = min(res,arr[idx^1]);
if (used[idx^1]==1) break;
}
idx>>=1;
}
return res;
}
void era(int idx, int val) {
while(idx>0) {
if (arr[idx]==val) {
used[idx] = 0;
break;
}
if (idx&1) {
if (arr[idx^1]==val) {
used[idx^1] = used[idx] = 1;
break;
}
else used[idx^1] = 0;
}
used[idx] = 1;
idx>>=1;
}
}
int main() {
int i;
scanf("%d",&n);
for (i=1;i<=n;i++) scanf("%d",&arr[i]);
for (i=1;i<=n;i++) used[i] = 2;
used[1] = 0;
for (i=1;i<=n;i++) {
int res = getv(i), l = ((i*2<=n)?arr[i*2]:INF), r = ((i*2+1<=n)?arr[i*2+1]:INF);
int v = min({res,l,r});
if (res==v) {
used[i*2] = used[i*2+1] = 0;
era(i,res);
}
else if (l==v) {used[i*2+1] = 0; used[i*2] = 1;}
else used[i*2+1] = 1;
brr[i] = v;
}
for (i=1;i<=n;i++) printf("%d ",brr[i]);
printf("\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... |