#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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
33 ms |
2092 KB |
Output is correct |
2 |
Correct |
31 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
35 ms |
2000 KB |
Output is correct |
2 |
Correct |
36 ms |
684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
38 ms |
604 KB |
Output is correct |
2 |
Correct |
40 ms |
2000 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
30 ms |
736 KB |
Output is correct |
2 |
Correct |
32 ms |
1860 KB |
Output is correct |
3 |
Correct |
40 ms |
2260 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
34 ms |
2000 KB |
Output is correct |
2 |
Correct |
32 ms |
1872 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
39 ms |
1996 KB |
Output is correct |
2 |
Correct |
33 ms |
684 KB |
Output is correct |
3 |
Correct |
39 ms |
2156 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
31 ms |
788 KB |
Output is correct |
2 |
Correct |
45 ms |
2000 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
55 ms |
2252 KB |
Output is correct |
2 |
Correct |
31 ms |
1616 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
36 ms |
1932 KB |
Output is correct |
2 |
Correct |
35 ms |
1624 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
41 ms |
2224 KB |
Output is correct |
2 |
Correct |
34 ms |
600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
31 ms |
2012 KB |
Output is correct |
2 |
Correct |
30 ms |
600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
36 ms |
2000 KB |
Output is correct |
2 |
Correct |
34 ms |
1880 KB |
Output is correct |