# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
372271 |
2021-02-27T12:48:00 Z |
8e7 |
Swap (BOI16_swap) |
C++14 |
|
19 ms |
19584 KB |
//Challenge: Accepted
#include <iostream>
#include <algorithm>
#include <utility>
#include <vector>
#include <unordered_map>
#define ll long long
#define maxn 200005
#define pii pair<int, int>
#define ff first
#define ss second
#define io ios_base::sync_with_stdio(0);cin.tie(0);
using namespace std;
int a[maxn], n;
vector<int> ord;
void getorder(int cur) {
if (cur * 2 + 1 <= n) getorder(cur * 2 + 1);
if (cur * 2 <= n) getorder(cur * 2);
ord.push_back(cur);
}
void merge(vector<int> &v1, vector<int> &v2, vector<int> & ret) {
ret.resize(ret.size() + v1.size() + v2.size());
int cur = ret.size();
for (int i = 0;(1<<(i + 1)) - 1 <= v2.size();i++) {
for (int j = (1<<i);j < (1<<(i + 1));j++) ret[cur++] = v1[j - 1];
for (int j = (1<<i);j < (1<<(i + 1));j++) ret[cur++] = v2[j - 1];
if ((1<<(i + 2)) - 1 > v2.size()) {
for (int j = (1<<(i + 1)) - 1;j < v1.size();j++) ret[cur++] = v1[j];
for (int j = (1<<(i + 1)) - 1;j < v2.size();j++) ret[cur++] = v2[j];
return;
}
}
}
vector<int> ind[maxn];
vector< vector<int> > ans[maxn];
inline int search(int v, int x) {
return find(ind[v].begin(), ind[v].end(), x) - ind[v].begin();
}
void getind(int v, int x) {
if (search(v, x) != ind[v].size()) return;
bool lef = 2 * v <= n, rig = 2 * v + 1 <= n;
if (lef && !rig) {
getind(v * 2, x);
getind(v * 2, a[v * 2]);
} else if (lef && rig) {
int l = a[2 * v], r = a[2 * v + 1];
if (x < l && x < r) {
getind(v * 2, l), getind(v * 2 + 1, r);
} else if (l < x && l < r) {
getind(v * 2, x), getind(v * 2 + 1, r);
} else {
getind(v * 2, l), getind(v * 2 + 1, x);
getind(v * 2, x), getind(v * 2 + 1, l);
}
}
ind[v].push_back(x);
}
void solve(int v, int x, vector<int> &ret) {
ret.push_back(x);
bool lef = 2 * v <= n, rig = 2 * v + 1 <= n;
if (lef && !rig) {
ret.push_back(a[2 * v]);
if (a[2 * v] < x) swap(ret[0], ret[1]);
} else if (lef && rig) {
int l = a[2 * v], r = a[2 * v + 1], lind = v * 2, rind = v * 2 + 1;
if (x < l && x < r) {
merge(ans[lind][search(lind, l)], ans[rind][search(rind, r)], ret);
} else if (l < x && l < r) {
ret[0] = l;
merge(ans[lind][search(lind, x)], ans[rind][search(rind, r)], ret);
} else {
ret[0] = r;
vector<int> v1, v2;
merge(ans[lind][search(lind, l)], ans[rind][search(rind, x)], v1);
merge(ans[lind][search(lind, x)], ans[rind][search(rind, l)], v2);
for (int i = 0;i < v1.size();i++) {
if (v1[i] < v2[i]) {
ret.insert(ret.end(), v1.begin(), v1.end());
break;
} else if (v1[i] > v2[i]) {
ret.insert(ret.end(), v2.begin(), v2.end());
break;
}
}
}
}
}
int main() {
io
cin >> n;
for (int i = 1;i <= n;i++) cin >> a[i];
getorder(1);
getind(1, a[1]);
for (int cnt = 0;cnt < n;cnt++) {
int i = ord[cnt];
ans[i].resize(ind[i].size());
for (int x = 0;x < ind[i].size();x++) {
solve(i, ind[i][x], ans[i][x]);
}
if (i * 2 <= n) ans[i * 2].clear();
if (i * 2 + 1 <= n) ans[i * 2 + 1].clear();
}
for (int i:ans[1][0]) cout << i << " ";
cout << "\n";
}
/*
5
3 4 2 5 1
*/
Compilation message
swap.cpp: In function 'void merge(std::vector<int>&, std::vector<int>&, std::vector<int>&)':
swap.cpp:25:34: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
25 | for (int i = 0;(1<<(i + 1)) - 1 <= v2.size();i++) {
| ~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
swap.cpp:28:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
28 | if ((1<<(i + 2)) - 1 > v2.size()) {
| ~~~~~~~~~~~~~~~~~^~~~~~~~~~~
swap.cpp:29:36: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
29 | for (int j = (1<<(i + 1)) - 1;j < v1.size();j++) ret[cur++] = v1[j];
| ~~^~~~~~~~~~~
swap.cpp:30:36: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
30 | for (int j = (1<<(i + 1)) - 1;j < v2.size();j++) ret[cur++] = v2[j];
| ~~^~~~~~~~~~~
swap.cpp: In function 'void getind(int, int)':
swap.cpp:43:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
43 | if (search(v, x) != ind[v].size()) return;
| ~~~~~~~~~~~~~^~~~~~~~~~~~~~~~
swap.cpp: In function 'void solve(int, int, std::vector<int>&)':
swap.cpp:79:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
79 | for (int i = 0;i < v1.size();i++) {
| ~~^~~~~~~~~~~
swap.cpp: In function 'int main()':
swap.cpp:100:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
100 | for (int x = 0;x < ind[i].size();x++) {
| ~~^~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
19 ms |
19584 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
19 ms |
19584 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
19 ms |
19584 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
19 ms |
19584 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
19 ms |
19584 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |