# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
46828 | Bruteforceman | Swap (BOI16_swap) | C++11 | 3 ms | 2076 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 a[200010 << 1];
int p[200010];
int n;
const int inf = 1e9;
int func(int x) {
int ans = inf;
ans = min(ans, a[x]);
if((x << 1) <= n) ans = min(ans, a[x << 1]);
if(((x << 1) | 1) <= n) ans = min(ans, a[(x << 1) | 1]);
return ans;
}
bool reach(int x, int y) {
while(x) {
int l = x << 1;
int r = l + 1;
if(l == y || r == y) return true;
if(x == y) return true;
x >>= 1;
}
return false;
}
int main(int argc, char const *argv[])
{
memset(a, -1, sizeof a);
scanf("%d", &n);
for(int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
p[i] = a[i];
}
vector <int> v;
queue <int> Q;
Q.push(1);
while(!Q.empty()) {
int x = Q.front();
Q.pop();
int f = func(x);
int l = x << 1;
int r = l + 1;
/* cout << "v: ";
for(auto i : v) {
cout << i << " ";
}
cout << endl;
*/
if(a[x] == inf) {
int opt = inf;
int idx = -1;
for(int i = 0; i < v.size(); i++) {
if(reach(x, v[i]) && opt > p[v[i]]) {
opt = p[v[i]];
idx = v[i];
}
}
if(opt < f) {
a[x] = opt;
v.erase(find(v.begin(), v.end(), idx));
// cout << "delete " << idx << endl;
} else if (a[r] == f) {
swap(a[x], a[r]);
v.push_back(l);
a[l] = inf;
} else {
swap(a[x], a[l]);
// swap(p[x], p[l]);
}
} else {
if(a[r] == f) {
v.push_back(l);
v.push_back(r);
swap(p[x], p[r]);
swap(a[x], a[r]);
a[l] = a[r] = inf;
} else if (a[l] == f) {
swap(a[x], a[l]);
swap(p[x], p[l]);
}
}
if(l <= n) Q.push(l);
if(r <= n) Q.push(r);
}
for(int i = 1; i <= n; i++) {
printf("%d ", a[i]);
}
printf("\n");
return 0;
}
// 9 1 3 11 6 4 5 2 8 15 14 16 7 17 13 12 18 10 20 19
// 9 1 3 11 6 4 5 2 8 15 14 16 7 10 13 12 17 18 20 19
// 9 1 3 11 6 4 5 1 8 15 14 16 7 17 13 12 2 10 18 19
/*
20
20 9 17 1 15 4 3 18 11 6 14 16 7 5 13 12 2 10 8 19
50
50 41 37 26 44 48 4 15 3 5 16 12 33 8 40 34 30 35 43 17 46 13 11 39 24 10 19 42 27 45 20 36 28 9 7 18 1 38 47 32 21 6 22 31 49 29 23 14 25 2
*/
// 37 26 4 3 5 12 8 15 35 17 11 24 10 27 20 28 7 1 38 21 6 13 16 14 2 33 19 41 42 40 45 34 36 9 30 18 50 43 47 32 44 46 22 31 49 29 23 39 25 48
// 37 26 4 3 5 12 8 15 35 17 11 24 10 27 20 28 7 1 38 21 6 13 16 14 2 33 19 41 42 40 45 34 36 9 30 18 50 43 47 32 44 46 22 31 49 29 23 39 25 48
// 37 26 4 3 5 12 8 15 26 17 11 12 10 27 20 28 7 1 38 21 6 13 16 14 2 33 19 42 50 40 45 34 36 9 30 18 35 43 47 32 44 46 22 31 49 29 23 39 25 24
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... |