#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) 1e5;
int arr[3 * MAXN + 1];
inline void check(int val1, int val2, int pos1, int pos2, vector <int> &sol) {
bool ok1 = 0, ok2 = 0;
while(1) {
int p1, p2;
if(arr[2 * pos1] < arr[2 * pos1 + 1]) {
p1 = 2 * pos1;
}
else {
p1 = 2 * pos1 + 1;
}
if(arr[2 * pos2] < arr[2 * pos2 + 1]) {
p2 = 2 * pos2;
}
else {
p2 = 2 * pos2 + 1;
}
if(arr[p1] > val1 && arr[p2] > val2) {
if(ok1 == 0 && ok2 == 0) {
if(pos1 < pos2) {
sol.push_back(val1);
sol.push_back(val2);
}
else {
sol.push_back(val2);
sol.push_back(val1);
}
}
else if(ok1) {
sol.push_back(val2);
}
else if(ok2) {
sol.push_back(val1);
}
ok1 = ok2 = 1;
break;
}
if(arr[p1] > val1 && arr[p2] < val2) {
if(ok1 == 1) {
sol.push_back(arr[p2]);
pos2 = p2;
}
else {
if(pos1 < pos2) {
sol.push_back(val1);
ok1 = 1;
}
else {
sol.push_back(arr[p2]);
pos2 = p2;
}
}
}
else if(arr[p1] < val1 && arr[p2] > val2) {
if(ok2 == 1) {
sol.push_back(arr[p1]);
pos1 = p1;
}
else {
if(pos2 < pos1) {
sol.push_back(val2);
ok2 = 1;
}
else {
sol.push_back(arr[p1]);
pos1 = p1;
}
}
}
else if(arr[p1] < val1 && arr[p2] < val2) {
if(pos1 < pos2) {
sol.push_back(arr[p1]);
pos1 = p1;
}
else {
sol.push_back(arr[p2]);
pos2 = p2;
}
}
}
}
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";
vector <int> sol1, sol2;
check(b, c, i, i + 1, sol1);
check(c, b, i, i + 1, sol2);
//b c a
if(sol1 < sol2) {
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 {
vector <int> sol1, sol2;
check(b, c, i, i + 1, sol1);
check(c, b, i, i + 1, sol2);
//c b a
if(sol1 < sol2) {
swap(arr[pos], arr[i + 1]);
}
else {
swap(arr[pos], arr[i]);
swap(arr[pos], arr[i + 1]);
}
}
}
/*for(int j = 1; j <= n; j++) {
cerr << arr[j] << " ";
}
cerr << "\n";*/
}
for(i = 1; i <= n; i++) {
cout << arr[i] << " ";
}
//cin.close();
//cout.close();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Incorrect |
2 ms |
380 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Incorrect |
2 ms |
380 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Incorrect |
2 ms |
380 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Incorrect |
2 ms |
380 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Incorrect |
2 ms |
380 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |