This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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) 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) break;
if(x<y){
pans.pb(x);
rbts[1].pop_back();
}else{
pans.pb(y);
rbts[0].pop_back();
now^=1;
}
}
}
assert(!sz(rbts[0]) and !sz(rbts[2]));
pans.pb(rbts[close][0]);
for(auto x : pans){
cout<<x<<" ";
}
//
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |