# |
제출 시각 |
아이디 |
문제 |
언어 |
결과 |
실행 시간 |
메모리 |
372227 |
2021-02-27T09:47:27 Z |
8e7 |
Swap (BOI16_swap) |
C++14 |
|
1000 ms |
1644 KB |
//Challenge: Accepted
#include <iostream>
#include <algorithm>
#include <utility>
#include <vector>
#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;
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> 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) {
//cout << v << " " << x << endl;
//cout << ret[0] << "\n";
return ret;
} else if (!rig) {
ret.push_back(a[2 * v]);
if (a[2 * v] < x) swap(ret[0], ret[1]);
//cout << v << " " << x << endl;
//cout << ret[0] << " " << ret[1] << "\n";
return ret;
} else {
int l = a[2 * v], r = a[2 * v + 1];
if (x < l && x < r) {
merge(solve(v * 2, l), solve(v * 2 + 1, r), ret);
} else if (l < x && l < r) {
ret[0] = l;
merge(solve(v * 2, x), solve(v * 2 + 1, r), ret);
} else {
ret[0] = r;
vector<int> v1, v2;
merge(solve(v * 2, l), solve(v * 2 + 1, x), v1);
merge(solve(v * 2, x), solve(v * 2 + 1, 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;
}
}
}
//cout << v << " " << x << endl;
//for (int i:ret) cout << i << " ";
//cout << "\n";
return ret;
}
}
int main() {
io
cin >> n;
for (int i = 1;i <= n;i++) cin >> a[i];
vector<int> ans = solve(1, a[1]);
for (int i:ans) 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:16:34: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
16 | for (int i = 0;(1<<(i + 1)) - 1 <= v2.size();i++) {
| ~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
swap.cpp:19:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
19 | if ((1<<(i + 2)) - 1 > v2.size()) {
| ~~~~~~~~~~~~~~~~~^~~~~~~~~~~
swap.cpp:20:36: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
20 | for (int j = (1<<(i + 1)) - 1;j < v1.size();j++) ret.push_back(v1[j]);
| ~~^~~~~~~~~~~
swap.cpp:21:36: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
21 | for (int j = (1<<(i + 1)) - 1;j < v2.size();j++) ret.push_back(v2[j]);
| ~~^~~~~~~~~~~
swap.cpp: In function 'std::vector<int> solve(int, int)':
swap.cpp:52:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
52 | for (int i = 0;i < v1.size();i++) {
| ~~^~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
2 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
2 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
2 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
396 KB |
Output is correct |
11 |
Correct |
2 ms |
364 KB |
Output is correct |
12 |
Correct |
5 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
28 ms |
492 KB |
Output is correct |
15 |
Correct |
27 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
2 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
396 KB |
Output is correct |
11 |
Correct |
2 ms |
364 KB |
Output is correct |
12 |
Correct |
5 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
28 ms |
492 KB |
Output is correct |
15 |
Correct |
27 ms |
364 KB |
Output is correct |
16 |
Correct |
390 ms |
1612 KB |
Output is correct |
17 |
Correct |
299 ms |
1644 KB |
Output is correct |
18 |
Correct |
425 ms |
1628 KB |
Output is correct |
19 |
Execution timed out |
1081 ms |
1132 KB |
Time limit exceeded |
20 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
2 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
396 KB |
Output is correct |
11 |
Correct |
2 ms |
364 KB |
Output is correct |
12 |
Correct |
5 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
28 ms |
492 KB |
Output is correct |
15 |
Correct |
27 ms |
364 KB |
Output is correct |
16 |
Correct |
390 ms |
1612 KB |
Output is correct |
17 |
Correct |
299 ms |
1644 KB |
Output is correct |
18 |
Correct |
425 ms |
1628 KB |
Output is correct |
19 |
Execution timed out |
1081 ms |
1132 KB |
Time limit exceeded |
20 |
Halted |
0 ms |
0 KB |
- |