#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define sz(a) (int)a.size()
#define all(a) a.begin(),a.end()
#define rep(i,n) for(int i=0;i<n;i++)
#define crep(i,x,n) for(int i=x;i<n;i++)
#define drep(i,n) for(int i=n-1;i>=0;i--)
#define vec(...) vector<__VA_ARGS__>
#define _32bB6rn ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define nare {cout<<"-1\n"; exit(0);}
using namespace std;
typedef long long ll;
typedef long double ld;
using pii=pair<int,int>;
using vi=vector<int>;
int main(){
_32bB6rn;
int n;
cin>>n;
vec(vi) rbts(8);
rep(i,n){
int x,y;
cin>>x>>y;
x--;
rbts[x].pb(y);
}
rep(i,8){
sort(rbts[i].rbegin(),rbts[i].rend());
}
if(sz(rbts[4])>1
or sz(rbts[5])>1
or (sz(rbts[4]) and sz(rbts[5]))){
nare;
}
if(sz(rbts[6])>1
or sz(rbts[7])>1
or (sz(rbts[6]) and sz(rbts[7]))){
nare;
}
int open=sz(rbts[4])?4:5;
int close=sz(rbts[6])?6:7;
if(open==4){
if(close==6){
if(sz(rbts[3])!=sz(rbts[0])+1) nare;
}else{
if(sz(rbts[3])!=sz(rbts[0])) nare;
}
}else{
if(close==6){
if(sz(rbts[3])!=sz(rbts[0])) nare;
}else{
if(sz(rbts[3])+1!=sz(rbts[0])) nare;
}
}
vi pans;
pans.pb(rbts[open][0]);
int now=(open==4?1:0);
rep(i,n-2){
int x=1e9+1,y=1e9+1; // fix here
if(now){
if(sz(rbts[2])){
x=rbts[2].back();
}
if(sz(rbts[3])){
y=rbts[3].back();
}
if(min(x,y)==1e9+1){
int v=pans.back();
pans.pop_back();
while(sz(rbts[1])){
pans.pb(rbts[1].back());
rbts[1].pop_back();
}
pans.pb(v);
break;
}
if(x<y){
pans.pb(x);
rbts[2].pop_back();
}else{
pans.pb(y);
rbts[3].pop_back();
now^=1;
}
}else{
if(sz(rbts[1])){
x=rbts[1].back();
}
if(sz(rbts[0])){
y=rbts[0].back();
}
if(min(x,y)==1e9+1){
int v=pans.back();
pans.pop_back();
while(sz(rbts[2])){
pans.pb(rbts[2].back());
rbts[2].pop_back();
}
pans.pb(v);
break;
}
if(x<y){
pans.pb(x);
rbts[1].pop_back();
}else{
pans.pb(y);
rbts[0].pop_back();
now^=1;
}
}
}
pans.pb(rbts[close][0]);
for(auto x : pans){
cout<<x<<" ";
}
//
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
31 ms |
3004 KB |
Output is correct |
2 |
Correct |
21 ms |
1880 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
26 ms |
2824 KB |
Output is correct |
2 |
Correct |
21 ms |
1860 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
2044 KB |
Output is correct |
2 |
Incorrect |
28 ms |
2964 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
1736 KB |
Output is correct |
2 |
Correct |
27 ms |
2736 KB |
Output is correct |
3 |
Incorrect |
33 ms |
3276 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
30 ms |
2948 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
33 ms |
2796 KB |
Output is correct |
2 |
Correct |
21 ms |
1800 KB |
Output is correct |
3 |
Incorrect |
31 ms |
3176 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
22 ms |
1896 KB |
Output is correct |
2 |
Incorrect |
29 ms |
2876 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
36 ms |
3344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
26 ms |
2904 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
32 ms |
3452 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
29 ms |
2992 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
31 ms |
3024 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |