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 FOR(ii,aa,bb) for(int ii=aa;ii<bb;ii++)
#define for0(ii,bb) FOR(ii,0,bb)
#define for1(ii,bb) FOR(ii,1,bb+1)
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define st first
#define nd second
#define pii pair<int,int>
#define piii pair<int,pii>
#define pdi pair<double,int>
#define vi vector<int>
#define sp " "
#define nl "\n"
#define all(x) x.begin(),x.end()
#define fastio() ios_base::sync_with_stdio(0);cin.tie(0);
#define ll long long
#define int ll
using namespace std;
const int N = 1e5+5;
const int INF = 1e9+5;
const int mod = 1e9+7;
int n,cnt[9],tmp[9];
vector<multiset<int>> s(9);
signed main(){
fastio()
cin >> n;
for(int i=1;i<=n;i++){
int t,x;
cin >> t >> x;
s[t].insert(x);
cnt[t]++;
}
vector<pii> ans;
int cur;
if(s[5].empty()){
ans.pb({*s[6].begin(),6});
cur=6;
}
else{
ans.pb({*s[5].begin(),5});
cur=5;
}
for(int i=2;i<n;i++){
if(cur==1||cur==3||cur==5){
if(s[3].empty()&&s[4].empty()){
break;
}
if(s[3].empty()){
ans.pb({*s[4].begin(),4});
s[4].erase(s[4].begin());
cur=4;
}
else if(s[4].empty()){
ans.pb({*s[3].begin(),3});
s[3].erase(s[3].begin());
cur=3;
}
else{
if(*s[3].begin()<=*s[4].begin()){
ans.pb({*s[3].begin(),3});
s[3].erase(s[3].begin());
cur=3;
}
else{
ans.pb({*s[4].begin(),4});
s[4].erase(s[4].begin());
cur=4;
}
}
}
else{
if(s[1].empty()&&s[2].empty()){
break;
}
if(s[1].empty()){
ans.pb({*s[2].begin(),2});
s[2].erase(s[2].begin());
cur=2;
}
else if(s[2].empty()){
ans.pb({*s[1].begin(),1});
s[1].erase(s[1].begin());
cur=1;
}
else{
if(*s[1].begin()<=*s[2].begin()){
ans.pb({*s[1].begin(),1});
s[1].erase(s[1].begin());
cur=1;
}
else{
ans.pb({*s[2].begin(),2});
s[2].erase(s[2].begin());
cur=2;
}
}
}
}
if(s[1].size()||s[4].size()){
cout << -1;
return 0;
}
if(s[8].empty()){
if(cur==2||cur==4||cur==6)
ans.pb({*s[7].begin(),7});
else{
cout << -1;
return 0;
}
}
else{
if(cur==1||cur==3||cur==5)
ans.pb({*s[8].begin(),8});
else{
cout << -1;
return 0;
}
}
for(auto e:ans){
tmp[e.nd]++;
if(s[2].size()&&tmp[1]==cnt[1]){
while(s[2].size()){
cout << *s[2].begin() << sp;
s[2].erase(s[2].begin());
}
}
if(s[3].size()&&tmp[4]==cnt[4]){
while(s[3].size()){
cout << *s[3].begin() << sp;
s[3].erase(s[3].begin());
}
}
cout << e.st << sp;
}
}
# | 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... |