# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
372268 |
2021-02-27T12:34:15 Z |
8e7 |
Swap (BOI16_swap) |
C++14 |
|
1000 ms |
157416 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);
//cout << cur << endl;
ord.push_back(cur);
}
void merge(vector<int> &v1, vector<int> &v2, vector<int> & ret) {
for (int i = 0;(1<<(i + 1)) - 1 <= v2.size();i++) {
for (int j = (1<<i);j < (1<<(i + 1));j++) ret.push_back(v1[j - 1]);
for (int j = (1<<i);j < (1<<(i + 1));j++) ret.push_back(v2[j - 1]);
if ((1<<(i + 2)) - 1 > v2.size()) {
for (int j = (1<<(i + 1)) - 1;j < v1.size();j++) ret.push_back(v1[j]);
for (int j = (1<<(i + 1)) - 1;j < v2.size();j++) ret.push_back(v2[j]);
return;
}
}
}
vector<int> ind[maxn];
vector< vector<int> > ans[maxn];
inline int search(int v, int x) {
for (int i = 0;i < ind[v].size();i++) {
if (ind[v][i] == x) return i;
}
//cout << "not found" << endl;
return -1;
}
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);
for (int cnt = 0;cnt < n;cnt++) {
int i = ord[cnt];
int cop = i;
while (cop) {
ind[i].push_back(a[cop]);
if ((cop & 1) && (cop - 1 > 0)) ind[i].push_back(a[cop - 1]);
cop >>= 1;
}
//cout << i << endl;
ans[i].resize(ind[i].size());
for (int x = 0;x < ind[i].size();x++) {
//cout << ind[i][x] << ": ";
solve(i, ind[i][x], ans[i][x]);
//for (auto j:tmp) cout << j << " ";
//cout << endl;
//ans[i].push_back(tmp);
}
//cout << endl;
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:24:34: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
24 | for (int i = 0;(1<<(i + 1)) - 1 <= v2.size();i++) {
| ~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
swap.cpp:27:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
27 | if ((1<<(i + 2)) - 1 > v2.size()) {
| ~~~~~~~~~~~~~~~~~^~~~~~~~~~~
swap.cpp:28:36: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
28 | for (int j = (1<<(i + 1)) - 1;j < v1.size();j++) ret.push_back(v1[j]);
| ~~^~~~~~~~~~~
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 < v2.size();j++) ret.push_back(v2[j]);
| ~~^~~~~~~~~~~
swap.cpp: In function 'int search(int, int)':
swap.cpp:37:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
37 | for (int i = 0;i < ind[v].size();i++) {
| ~~^~~~~~~~~~~~~~~
swap.cpp: In function 'void solve(int, int, std::vector<int>&)':
swap.cpp:62:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
62 | for (int i = 0;i < v1.size();i++) {
| ~~^~~~~~~~~~~
swap.cpp: In function 'int main()':
swap.cpp:89:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
89 | for (int x = 0;x < ind[i].size();x++) {
| ~~^~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
9708 KB |
Output is correct |
2 |
Correct |
7 ms |
9708 KB |
Output is correct |
3 |
Correct |
7 ms |
9708 KB |
Output is correct |
4 |
Correct |
7 ms |
9708 KB |
Output is correct |
5 |
Correct |
7 ms |
9708 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
9708 KB |
Output is correct |
2 |
Correct |
7 ms |
9708 KB |
Output is correct |
3 |
Correct |
7 ms |
9708 KB |
Output is correct |
4 |
Correct |
7 ms |
9708 KB |
Output is correct |
5 |
Correct |
7 ms |
9708 KB |
Output is correct |
6 |
Correct |
7 ms |
9708 KB |
Output is correct |
7 |
Correct |
7 ms |
9708 KB |
Output is correct |
8 |
Correct |
7 ms |
9708 KB |
Output is correct |
9 |
Correct |
7 ms |
9708 KB |
Output is correct |
10 |
Correct |
7 ms |
9708 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
9708 KB |
Output is correct |
2 |
Correct |
7 ms |
9708 KB |
Output is correct |
3 |
Correct |
7 ms |
9708 KB |
Output is correct |
4 |
Correct |
7 ms |
9708 KB |
Output is correct |
5 |
Correct |
7 ms |
9708 KB |
Output is correct |
6 |
Correct |
7 ms |
9708 KB |
Output is correct |
7 |
Correct |
7 ms |
9708 KB |
Output is correct |
8 |
Correct |
7 ms |
9708 KB |
Output is correct |
9 |
Correct |
7 ms |
9708 KB |
Output is correct |
10 |
Correct |
7 ms |
9708 KB |
Output is correct |
11 |
Correct |
10 ms |
10220 KB |
Output is correct |
12 |
Correct |
11 ms |
10348 KB |
Output is correct |
13 |
Correct |
10 ms |
10220 KB |
Output is correct |
14 |
Correct |
10 ms |
10220 KB |
Output is correct |
15 |
Correct |
10 ms |
10220 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
9708 KB |
Output is correct |
2 |
Correct |
7 ms |
9708 KB |
Output is correct |
3 |
Correct |
7 ms |
9708 KB |
Output is correct |
4 |
Correct |
7 ms |
9708 KB |
Output is correct |
5 |
Correct |
7 ms |
9708 KB |
Output is correct |
6 |
Correct |
7 ms |
9708 KB |
Output is correct |
7 |
Correct |
7 ms |
9708 KB |
Output is correct |
8 |
Correct |
7 ms |
9708 KB |
Output is correct |
9 |
Correct |
7 ms |
9708 KB |
Output is correct |
10 |
Correct |
7 ms |
9708 KB |
Output is correct |
11 |
Correct |
10 ms |
10220 KB |
Output is correct |
12 |
Correct |
11 ms |
10348 KB |
Output is correct |
13 |
Correct |
10 ms |
10220 KB |
Output is correct |
14 |
Correct |
10 ms |
10220 KB |
Output is correct |
15 |
Correct |
10 ms |
10220 KB |
Output is correct |
16 |
Correct |
252 ms |
44472 KB |
Output is correct |
17 |
Correct |
261 ms |
44100 KB |
Output is correct |
18 |
Correct |
253 ms |
44528 KB |
Output is correct |
19 |
Correct |
325 ms |
44732 KB |
Output is correct |
20 |
Correct |
330 ms |
44588 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
9708 KB |
Output is correct |
2 |
Correct |
7 ms |
9708 KB |
Output is correct |
3 |
Correct |
7 ms |
9708 KB |
Output is correct |
4 |
Correct |
7 ms |
9708 KB |
Output is correct |
5 |
Correct |
7 ms |
9708 KB |
Output is correct |
6 |
Correct |
7 ms |
9708 KB |
Output is correct |
7 |
Correct |
7 ms |
9708 KB |
Output is correct |
8 |
Correct |
7 ms |
9708 KB |
Output is correct |
9 |
Correct |
7 ms |
9708 KB |
Output is correct |
10 |
Correct |
7 ms |
9708 KB |
Output is correct |
11 |
Correct |
10 ms |
10220 KB |
Output is correct |
12 |
Correct |
11 ms |
10348 KB |
Output is correct |
13 |
Correct |
10 ms |
10220 KB |
Output is correct |
14 |
Correct |
10 ms |
10220 KB |
Output is correct |
15 |
Correct |
10 ms |
10220 KB |
Output is correct |
16 |
Correct |
252 ms |
44472 KB |
Output is correct |
17 |
Correct |
261 ms |
44100 KB |
Output is correct |
18 |
Correct |
253 ms |
44528 KB |
Output is correct |
19 |
Correct |
325 ms |
44732 KB |
Output is correct |
20 |
Correct |
330 ms |
44588 KB |
Output is correct |
21 |
Execution timed out |
1099 ms |
157416 KB |
Time limit exceeded |
22 |
Halted |
0 ms |
0 KB |
- |