# |
제출 시각 |
아이디 |
문제 |
언어 |
결과 |
실행 시간 |
메모리 |
372232 |
2021-02-27T09:56:58 Z |
8e7 |
Swap (BOI16_swap) |
C++14 |
|
1000 ms |
202732 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;
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;
}
}
}
unordered_map<int, vector<int> > ans[maxn];
vector<int> solve(int v, int x) {
if (ans[v][x].size()) {
return ans[v][x];
}
vector<int> ret;
ret.push_back(x);
bool lef = 2 * v <= n, rig = 2 * v + 1 <= n;
if (!lef && !rig) {
return ret;
} else if (!rig) {
ret.push_back(a[2 * v]);
if (a[2 * v] < x) swap(ret[0], ret[1]);
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;
}
}
}
ans[v][x] = ret;
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:17:34: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
17 | for (int i = 0;(1<<(i + 1)) - 1 <= v2.size();i++) {
| ~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
swap.cpp:20:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
20 | if ((1<<(i + 2)) - 1 > v2.size()) {
| ~~~~~~~~~~~~~~~~~^~~~~~~~~~~
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 < v1.size();j++) ret.push_back(v1[j]);
| ~~^~~~~~~~~~~
swap.cpp:22:36: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
22 | 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:53:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
53 | for (int i = 0;i < v1.size();i++) {
| ~~^~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
11244 KB |
Output is correct |
2 |
Correct |
8 ms |
11244 KB |
Output is correct |
3 |
Correct |
8 ms |
11244 KB |
Output is correct |
4 |
Correct |
9 ms |
11404 KB |
Output is correct |
5 |
Correct |
8 ms |
11244 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
11244 KB |
Output is correct |
2 |
Correct |
8 ms |
11244 KB |
Output is correct |
3 |
Correct |
8 ms |
11244 KB |
Output is correct |
4 |
Correct |
9 ms |
11404 KB |
Output is correct |
5 |
Correct |
8 ms |
11244 KB |
Output is correct |
6 |
Correct |
8 ms |
11244 KB |
Output is correct |
7 |
Correct |
7 ms |
11244 KB |
Output is correct |
8 |
Correct |
8 ms |
11244 KB |
Output is correct |
9 |
Correct |
10 ms |
11372 KB |
Output is correct |
10 |
Correct |
10 ms |
11244 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
11244 KB |
Output is correct |
2 |
Correct |
8 ms |
11244 KB |
Output is correct |
3 |
Correct |
8 ms |
11244 KB |
Output is correct |
4 |
Correct |
9 ms |
11404 KB |
Output is correct |
5 |
Correct |
8 ms |
11244 KB |
Output is correct |
6 |
Correct |
8 ms |
11244 KB |
Output is correct |
7 |
Correct |
7 ms |
11244 KB |
Output is correct |
8 |
Correct |
8 ms |
11244 KB |
Output is correct |
9 |
Correct |
10 ms |
11372 KB |
Output is correct |
10 |
Correct |
10 ms |
11244 KB |
Output is correct |
11 |
Correct |
10 ms |
11500 KB |
Output is correct |
12 |
Correct |
9 ms |
11628 KB |
Output is correct |
13 |
Correct |
9 ms |
11500 KB |
Output is correct |
14 |
Correct |
14 ms |
12012 KB |
Output is correct |
15 |
Correct |
16 ms |
12012 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
11244 KB |
Output is correct |
2 |
Correct |
8 ms |
11244 KB |
Output is correct |
3 |
Correct |
8 ms |
11244 KB |
Output is correct |
4 |
Correct |
9 ms |
11404 KB |
Output is correct |
5 |
Correct |
8 ms |
11244 KB |
Output is correct |
6 |
Correct |
8 ms |
11244 KB |
Output is correct |
7 |
Correct |
7 ms |
11244 KB |
Output is correct |
8 |
Correct |
8 ms |
11244 KB |
Output is correct |
9 |
Correct |
10 ms |
11372 KB |
Output is correct |
10 |
Correct |
10 ms |
11244 KB |
Output is correct |
11 |
Correct |
10 ms |
11500 KB |
Output is correct |
12 |
Correct |
9 ms |
11628 KB |
Output is correct |
13 |
Correct |
9 ms |
11500 KB |
Output is correct |
14 |
Correct |
14 ms |
12012 KB |
Output is correct |
15 |
Correct |
16 ms |
12012 KB |
Output is correct |
16 |
Correct |
94 ms |
26332 KB |
Output is correct |
17 |
Correct |
107 ms |
26080 KB |
Output is correct |
18 |
Correct |
93 ms |
26660 KB |
Output is correct |
19 |
Correct |
582 ms |
82520 KB |
Output is correct |
20 |
Correct |
612 ms |
81924 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
11244 KB |
Output is correct |
2 |
Correct |
8 ms |
11244 KB |
Output is correct |
3 |
Correct |
8 ms |
11244 KB |
Output is correct |
4 |
Correct |
9 ms |
11404 KB |
Output is correct |
5 |
Correct |
8 ms |
11244 KB |
Output is correct |
6 |
Correct |
8 ms |
11244 KB |
Output is correct |
7 |
Correct |
7 ms |
11244 KB |
Output is correct |
8 |
Correct |
8 ms |
11244 KB |
Output is correct |
9 |
Correct |
10 ms |
11372 KB |
Output is correct |
10 |
Correct |
10 ms |
11244 KB |
Output is correct |
11 |
Correct |
10 ms |
11500 KB |
Output is correct |
12 |
Correct |
9 ms |
11628 KB |
Output is correct |
13 |
Correct |
9 ms |
11500 KB |
Output is correct |
14 |
Correct |
14 ms |
12012 KB |
Output is correct |
15 |
Correct |
16 ms |
12012 KB |
Output is correct |
16 |
Correct |
94 ms |
26332 KB |
Output is correct |
17 |
Correct |
107 ms |
26080 KB |
Output is correct |
18 |
Correct |
93 ms |
26660 KB |
Output is correct |
19 |
Correct |
582 ms |
82520 KB |
Output is correct |
20 |
Correct |
612 ms |
81924 KB |
Output is correct |
21 |
Correct |
383 ms |
75904 KB |
Output is correct |
22 |
Correct |
399 ms |
76308 KB |
Output is correct |
23 |
Correct |
379 ms |
76888 KB |
Output is correct |
24 |
Execution timed out |
1102 ms |
202732 KB |
Time limit exceeded |
25 |
Halted |
0 ms |
0 KB |
- |