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 pii pair<int,int>
#define fs first
#define sc second
vector<int> v[9];
pii dif[] = {{0,0},{1,1},{1,-1},{-1,1},{-1,-1},{0,1},{0,-1},{1,0},{-1,0}};
int main(){
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int n;
cin>>n;
int total = 0;
int cnt = 0;
for(int i = 0;i<n;i++){
int a,b;
cin>>a>>b;
total += dif[a].fs+dif[a].sc;
v[a].push_back(b);
}
if(total != 0||v[5].size()+v[6].size() != 1||v[7].size()+v[8].size() != 1){
cout<<-1;
return 0;
}
for(auto &i:v)sort(i.rbegin(),i.rend());
vector<int> ans;
int now = 0;
if(v[5].empty()){
ans.push_back(v[6][0]);
now = dif[6].sc;
}
else{
ans.push_back(v[5].back());
now = dif[5].sc;
}
pii lcnt = {0,0};
for(int i = 1;i<=4;i++){
lcnt.fs += v[i].size()*(dif[i].fs>0);
lcnt.sc += v[i].size()*(dif[i].fs<0);
}
for(int i = 1;i<n-1;i++){
pii tmp = {1e9+10,1e9+10};
for(int j = 1;j<=4;j++){
if(i != n-2&&dif[j].sc == -1)if(lcnt.fs-(dif[j].fs == 1)== 0)continue;
// cout<<j<<endl;
if(i != n-2&&dif[j].sc == 1)if(lcnt.sc -(dif[j].fs == -1)== 0)continue;
if(dif[j].fs+now == 0&&!v[j].empty())tmp = min(tmp,make_pair(v[j].back(),j));
}
// cout<<i<<":"<<tmp.fs<<' '<<tmp.sc<<' '<<lcnt.fs<<' '<<lcnt.sc<<endl;
if(tmp.fs > 1e9){
cout<<-1;
return 0;
}
if(dif[tmp.sc].fs>0)lcnt.fs--;
else lcnt.sc--;
ans.push_back(tmp.fs);
now = dif[tmp.sc].sc;
v[tmp.sc].pop_back();
}
for(auto &i:ans)cout<<i<<' ';
for(int i = 7;i<=8;i++){
if(!v[i].empty())cout<<v[i].back();
}
}
Compilation message (stderr)
slagalica.cpp: In function 'int main()':
slagalica.cpp:14:9: warning: unused variable 'cnt' [-Wunused-variable]
14 | int cnt = 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... |