#include"iostream"
#include"vector"
// #include"queue"
// #include"deque"
#include"set"
// #include"map"
#include"algorithm"
// #include"iomanip"
// #include"cstring"
// #include"cmath"
// #include"bitset"
#define int long long
using namespace std;
const int INF=1e9;
const int maxn=1e5;
int sbmp=0,ebmp=0;
set<int> st[7];
bool check(int a,int b,int c,int d){
if(a<0 || b<0 || c<0 || d<0){return 0;}
if(a==0 && b==0 && c==0 && d==0){return 1;}
// cout << a << " " << b << " " << c <<" " << d << "\n";
if(sbmp==1 && ebmp==0){
// cout << "asd?";
if(a==d){
if(a==0 && b!=0){
return 0;
}
else{
return 1;
}
}
return 0;
}
if(sbmp==0 && ebmp==1){
// cout << a << " " << b << " " << c <<" " << d << "\n";
if(a==d){
if(a==0 && c!=0){
return 0;
}
else{
return 1;
}
}
return 0;
}
if(sbmp==1 && ebmp==1){
if(a==d-1){
return 1;
}
return 0;
}
if(sbmp==0 && ebmp==0){
// cout << a << " " << b << " " << c <<" " << d << "\n";
if(d==a-1){
return 1;
}
return 0;
}
// cout << "wha?";
return 0;
}
void solve(){
int n,a,b;
cin >> n;
pair<int,int> start,end;
for(int i=0;i<n;i++){
cin >> a >> b;
if(a<=4)
st[a].insert(b);
else if(a<=6)
start={a,b};
else if(a<=8)
end={a,b};
}
// for(int i=0;i<5;i++){
// cout << st[i].size() << "\n";
// }
if(start.first==5){
sbmp=1;
}
if(end.first==7){
ebmp=1;
}
vector<int> ans;
// sbmp ve ebmp ve check fonksiyonunda öncelik karışması var
// o anda olduğun yerden itibaren kontrol edeceksin
for(int i=1;i<n-1;i++){
int mini=INF;
int mininum=0;
if(sbmp==0){
sbmp=1;
if(check(st[1].size()-1,st[2].size(),st[3].size(),st[4].size())){
// cout << ".1\n";
if(mini>*st[1].begin()){
mininum=1;
}
mini = min(mini,*(st[1].begin()));
}
sbmp=0;
}
if(sbmp==0){
sbmp=0;
if(check(st[1].size(),st[2].size()-1,st[3].size(),st[4].size())){
// cout << ".2\n";
if(mini>*st[2].begin()){
mininum=2;
}
mini = min(mini,*(st[2].begin()));
}
sbmp=0;
}
if(sbmp==1){
sbmp=1;
if(check(st[1].size(),st[2].size(),st[3].size()-1,st[4].size())){
// cout << ".3\n";
if(mini>*st[3].begin()){
mininum=3;
}
mini = min(mini,*(st[3].begin()));
}
sbmp=1;
}
if(sbmp==1){
sbmp=0;
// cout << st[4].size() << "--\n";
if(check(st[1].size(),st[2].size(),st[3].size(),st[4].size()-1)){
// cout << ".4\n";
if(mini>*st[4].begin()){
mininum=4;
}
mini = min(mini,*(st[4].begin()));
}
sbmp=1;
}
// cout << mininum << "<->" << mini << "\n\n";
ans.push_back(mini);
if(mini==INF){cout << "-1";return;}
if(mininum==1 || mininum==3){sbmp=1;}
if(mininum==2 || mininum==4){sbmp=0;}
st[mininum].erase(*(st[mininum].begin()));
// cout << "\n";
}
cout << start.second << " ";
for(auto it : ans){
cout << it << " ";
}
cout << end.second << " ";
// for(int i=0;i<5;i++){
// cout << st[i].size() << "\n";
// }
}
signed main(){
ios::sync_with_stdio(false); cin.tie(0);
// freopen("","r",stdin);freopen("","w",stdout);
int t=1;
// cin >> t;
for(int i=1;i<=t;i++){
// cout << "Case " << i << ":\n";
solve();
// cout <<"\n\n";
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
49 ms |
6008 KB |
Output is correct |
2 |
Correct |
36 ms |
4676 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
46 ms |
5868 KB |
Output is correct |
2 |
Correct |
33 ms |
4388 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
50 ms |
5076 KB |
Output is correct |
2 |
Correct |
53 ms |
6116 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
35 ms |
4192 KB |
Output is correct |
2 |
Correct |
43 ms |
5728 KB |
Output is correct |
3 |
Correct |
54 ms |
6772 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
46 ms |
5948 KB |
Output is correct |
2 |
Correct |
40 ms |
4628 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
47 ms |
5880 KB |
Output is correct |
2 |
Correct |
38 ms |
4676 KB |
Output is correct |
3 |
Correct |
51 ms |
6440 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
33 ms |
4312 KB |
Output is correct |
2 |
Correct |
44 ms |
5820 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
63 ms |
6732 KB |
Output is correct |
2 |
Correct |
34 ms |
5168 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
44 ms |
5668 KB |
Output is correct |
2 |
Correct |
34 ms |
5344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
62 ms |
6756 KB |
Output is correct |
2 |
Correct |
39 ms |
5820 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
43 ms |
5620 KB |
Output is correct |
2 |
Correct |
39 ms |
5412 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
59 ms |
5708 KB |
Output is correct |
2 |
Correct |
40 ms |
5624 KB |
Output is correct |