#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef std::pair<int, int> pii;
const int MXN = 100005;
int n;
struct P {
bool l, r;
int val;
P() {
l = false;
r = false;
val = 0;
}
};
vector<P> LL, RR, LR, RL;
P first, last, br;
bool state;
bool check() {
if (first.r && last.l && LR.size() != RL.size()) return false;
if (first.r && !last.l && LR.size() != RL.size() - 1) return false;
if (!first.r && last.l && LR.size() != RL.size() + 1) return false;
if (!first.r && !last.l && LR.size() != RL.size()) return false;
return true;
}
void OUT(vector<P> &v) {
cout << v.back().val;
state = v.back().r;
v.pop_back();
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int type;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> type >> br.val;
br.l = false, br.r = false;
if (type == 3 || type == 4 || type == 8) br.l = true;
if (type == 1 || type == 3 || type == 5) br.r = true;
// cout << br.l << br.r << endl;
if (type == 5 || type == 6) {
first = br;
}
else if (type == 7 || type == 8) {
last = br;
} else (br.l ? (br.r ? RR : RL) : (br.r ? LR : LL)).push_back(br);
}
// cout << LL.size() << ' ' << LR.size() << ' ' << RL.size() << ' ' << RR.size() << endl;
sort(LL.begin(), LL.end(), [](P a, P b) {
return a.val < b.val;
});
sort(LR.begin(), LR.end(), [](P a, P b) {
return a.val < b.val;
});
sort(RL.begin(), RL.end(), [](P a, P b) {
return a.val < b.val;
});
sort(RR.begin(), RR.end(), [](P a, P b) {
return a.val < b.val;
});
if (!check()) {
cout << -1 << endl;
return 0;
}
state = first.r;
cout << first.val;
for (int i = 1; i < n - 1; i++) {
// cout << state << endl;
cout << ' ';
if (state) { // R
if (RL.empty()) OUT(RR);
else if (RR.empty()) OUT(RL);
else if (LL.empty() && LR.empty()) OUT(RR);
else OUT((RL.back().val < RR.back().val ? RR : RL));
} else { // L
if (LL.empty()) OUT(LR);
else if (LR.empty()) OUT(LL);
else if (RL.empty() && RR.empty()) OUT(LL);
else OUT((LL.back().val < LR.back().val ? LR : LL));
}
}
cout << ' ' << last.val << endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
25 ms |
2728 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
26 ms |
2592 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
2020 KB |
Output is correct |
2 |
Incorrect |
27 ms |
2728 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
1908 KB |
Output is correct |
2 |
Incorrect |
26 ms |
2668 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
26 ms |
4232 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
25 ms |
2580 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
2008 KB |
Output is correct |
2 |
Incorrect |
33 ms |
2656 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
30 ms |
3212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
24 ms |
2636 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
29 ms |
2980 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
25 ms |
2484 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
25 ms |
2488 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |