#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, ff, fl;
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) {
if (ff) {
cout << -1 << endl;
return 0;
}
first = br;
ff = true;
}
else if (type == 7 || type == 8) {
if (fl) {
cout << -1 << endl;
return 0;
}
last = br;
fl = true;
} else (br.l ? (br.r ? RR : RL) : (br.r ? LR : LL)).push_back(br);
}
if (!ff || !fl) {
cout << -1 << endl;
return 0;
}
// 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 |
26 ms |
2720 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
25 ms |
2684 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
2112 KB |
Output is correct |
2 |
Incorrect |
32 ms |
2664 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
1972 KB |
Output is correct |
2 |
Incorrect |
25 ms |
2524 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
31 ms |
4260 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
26 ms |
2612 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
1988 KB |
Output is correct |
2 |
Incorrect |
30 ms |
2672 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
34 ms |
3124 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
27 ms |
2616 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
29 ms |
3028 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 |
36 ms |
2528 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |