#include <bits/stdc++.h>
#define lsb(x) (x & (-x))
#define ll long long
#define ull unsigned long long
#define ld long double
// 217
// 44
using namespace std;
const int INF = 1e9;
const int MAXN = (int) 2e5;
int arr[3 * MAXN + 1];
map < pair <int, int>, int > mp;
int solve(int pos, int val) {
if(mp.count({pos, val})) {
return mp[{pos, val}];
}
if(val < min(arr[2 * pos], arr[2 * pos + 1])) {
return pos;
}
if(arr[2 * pos] < arr[2 * pos + 1]) {
return mp[{pos, val}] = solve(2 * pos, val);
}
int mn = min(arr[2 * pos], val);
// b c a
if(solve(2 * pos, mn) < solve(2 * pos + 1, mn)) {
if(mn == val) return mp[{pos, val}] = solve(2 * pos, val);
return mp[{pos, val}] = solve(2 * pos + 1, val);
}
else { // c b a
if(mn == val) return mp[{pos, val}] = solve(2 * pos + 1, val);
return mp[{pos, val}] = solve(2 * pos, val);
}
}
int main() {
//ifstream cin("A.in");
//ofstream cout("A.out");
int i, n;
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> n;
for(i = 1; i <= n; i++) {
cin >> arr[i];
}
for(i = n + 1; i <= 2 * n + 5; i++) {
arr[i] = INF;
}
for(i = 2; i <= n; i += 2) {
int pos = i / 2;
int a = min(arr[pos], min(arr[i], arr[i + 1]));
int c = max(arr[pos], max(arr[i], arr[i + 1]));
int b = arr[pos] + arr[i] + arr[i + 1] - a - c;
if(arr[pos] == a) {
}
else if(arr[pos] == b) {
if(arr[i] == a) {
swap(arr[pos], arr[i]);
}
else {
//cerr << min(arr[2 * i], arr[2 * i + 1]) << " " << b << " " << min(arr[2 * i + 2], arr[2 * i + 3]) << "\n";
//b c a
if(solve(i, b) < solve(i + 1, b)) {
swap(arr[pos], arr[i]);
swap(arr[pos], arr[i + 1]);
}
else {
swap(arr[pos], arr[i + 1]);
}
}
}
else if(arr[pos] == c) {
if(arr[i] == a) {
swap(arr[pos], arr[i]);
}
else {
//c b a
if(solve(i, b) < solve(i + 1, b)) {
swap(arr[pos], arr[i + 1]);
}
else {
swap(arr[pos], arr[i]);
swap(arr[pos], arr[i + 1]);
}
}
}
}
for(i = 1; i <= n; i++) {
cout << arr[i] << " ";
}
//cin.close();
//cout.close();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
16 ms |
1912 KB |
Output is correct |
17 |
Correct |
16 ms |
1920 KB |
Output is correct |
18 |
Correct |
16 ms |
1904 KB |
Output is correct |
19 |
Correct |
37 ms |
4088 KB |
Output is correct |
20 |
Correct |
40 ms |
4088 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
16 ms |
1912 KB |
Output is correct |
17 |
Correct |
16 ms |
1920 KB |
Output is correct |
18 |
Correct |
16 ms |
1904 KB |
Output is correct |
19 |
Correct |
37 ms |
4088 KB |
Output is correct |
20 |
Correct |
40 ms |
4088 KB |
Output is correct |
21 |
Correct |
63 ms |
7000 KB |
Output is correct |
22 |
Correct |
63 ms |
8184 KB |
Output is correct |
23 |
Correct |
63 ms |
8216 KB |
Output is correct |
24 |
Correct |
192 ms |
17020 KB |
Output is correct |
25 |
Correct |
222 ms |
16972 KB |
Output is correct |