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>
using namespace std;
#define ll long long
int const N=2e5+5;
int const inf=1e9+7;
deque<int> pc[10];
void solve57(){
if(pc[4].size()!=1+pc[1].size()){
cout<<-1<<endl;
return;
}
bool b=0;
pc[1].push_back(inf);
vector<int> ans;
ans.push_back(pc[5][0]);
while(true){
if(b==0){
while(pc[3].empty()==0 && (pc[3][0]<pc[4][0] || pc[4].size()==1)){
ans.push_back(pc[3][0]);
pc[3].pop_front();
}
ans.push_back(pc[4][0]);
pc[4].pop_front();
b=1;
}
else{
while(pc[2].empty()==0 && pc[2][0]<pc[1][0]){
ans.push_back(pc[2][0]);
pc[2].pop_front();
}
if(pc[1].size()==1)
break;
ans.push_back(pc[1][0]);
pc[1].pop_front();
b=0;
}
}
ans.push_back(pc[7][0]);
for(int i:ans)
cout<<i<<' ';
cout<<endl;
}
void solve58(){
if(pc[4].size()!=pc[1].size()){
cout<<-1<<endl;
return;
}
bool b=0;
pc[4].push_back(inf);
vector<int> ans;
ans.push_back(pc[5][0]);
while(true){
if(b==0){
while(pc[3].empty()==0 && pc[3][0]<pc[4][0]){
ans.push_back(pc[3][0]);
pc[3].pop_front();
}
if(pc[4].size()==1)
break;
ans.push_back(pc[4][0]);
pc[4].pop_front();
b=1;
}
else{
while(pc[2].empty()==0 && (pc[2][0]<pc[1][0] || pc[1].size()==1)){
ans.push_back(pc[2][0]);
pc[2].pop_front();
}
ans.push_back(pc[1][0]);
pc[1].pop_front();
b=0;
}
}
ans.push_back(pc[8][0]);
for(int i:ans)
cout<<i<<' ';
cout<<endl;
}
void solve67(){
if(pc[4].size()!=pc[1].size()){
cout<<-1<<endl;
return;
}
bool b=1;
pc[1].push_back(inf);
vector<int> ans;
ans.push_back(pc[6][0]);
while(true){
if(b==0){
while(pc[3].empty()==0 && (pc[3][0]<pc[4][0] || pc[4].size()==1)){
ans.push_back(pc[3][0]);
pc[3].pop_front();
}
ans.push_back(pc[4][0]);
pc[4].pop_front();
b=1;
}
else{
while(pc[2].empty()==0 && pc[2][0]<pc[1][0]){
ans.push_back(pc[2][0]);
pc[2].pop_front();
}
if(pc[1].size()==1)
break;
ans.push_back(pc[1][0]);
pc[1].pop_front();
b=0;
}
}
ans.push_back(pc[7][0]);
for(int i:ans)
cout<<i<<' ';
cout<<endl;
}
void solve68(){
// cout<<"YES"<<endl;
if(pc[4].size()+1!=pc[1].size()){
cout<<-1<<endl;
return;
}
bool b=1;
pc[4].push_back(inf);
vector<int> ans;
ans.push_back(pc[6][0]);
while(true){
if(b==0){
while(pc[3].empty()==0 && pc[3][0]<pc[4][0]){
ans.push_back(pc[3][0]);
pc[3].pop_front();
}
if(pc[4].size()==1)
break;
ans.push_back(pc[4][0]);
pc[4].pop_front();
b=1;
}
else{
while(pc[2].empty()==0 && (pc[2][0]<pc[1][0] || pc[1].size()==1)){
ans.push_back(pc[2][0]);
pc[2].pop_front();
}
ans.push_back(pc[1][0]);
pc[1].pop_front();
b=0;
}
}
ans.push_back(pc[8][0]);
for(int i:ans)
cout<<i<<' ';
cout<<endl;
}
int main(){
int n;
cin>>n;
for (int i = 0; i < n; ++i)
{
int x,a;
cin>>x>>a;
pc[x].push_back(a);
}
for (int i = 0; i < 10; ++i)
sort(pc[i].begin(), pc[i].end());
if(pc[5].size()>0 && pc[7].size()>0)
solve57();
else if(pc[5].size()>0 && pc[8].size()>0)
solve58();
else if(pc[7].size()>0)
solve67();
else
solve68();
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... |