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 <cstdio>
#include <cctype>
#include <algorithm>
#include <vector>
#include <list>
using namespace std;
const int maxn = 1e5 + 100;
inline int read(){
int x = 0;char c = getchar();
while(!isdigit(c))c = getchar();
while(isdigit(c))x = x * 10 + c - '0',c = getchar();
return x;
}
vector<int> G[16],vec[16];
list<int> ans;
list<int>::iterator rem[2];
int n,pos[16],nxt[4],lst;
inline int get(int x){
return pos[x] == vec[x].size() ? 0x7fffffff : vec[x][pos[x]];
}
inline void msort(){
if(get(nxt[1]) < get(nxt[0]))swap(nxt[1],nxt[0]);
if(get(nxt[2]) < get(nxt[0]))swap(nxt[2],nxt[0]);
if(get(nxt[2]) < get(nxt[1]))swap(nxt[1],nxt[2]);
}
inline void add(int u,int v){G[u].push_back(v);}
inline void init(){
add(1,3);add(1,4);//add(1,8);
add(2,1);add(2,2);//add(2,7);
add(3,3);add(3,4);//add(3,8);
add(4,1);add(4,2);//add(4,7);
add(5,3);add(5,4);//add(5,8);
add(6,1);add(6,2);//add(6,7);
}
int main(){
init();
n = read();
for(int a,b,i = 1;i <= n;i++)
a = read(),b = read(),vec[a].push_back(b);
for(int i = 1;i <= 8;i++)
sort(vec[i].begin(),vec[i].end());
if(!vec[5].empty())lst = 5,ans.push_back(vec[5][0]),rem[1] = --ans.end();
else if(!vec[6].empty())lst = 6,ans.push_back(vec[6][0]),rem[0] = --ans.end();
while(true){
for(int i = 0;i < G[lst].size();i++)nxt[i] = G[lst][i];
msort();
if(pos[nxt[0]] == vec[nxt[0]].size())break;
ans.push_back(vec[nxt[0]][pos[nxt[0]]]);
rem[nxt[0] & 1] = --ans.end();
lst = nxt[0];
pos[nxt[0]]++;
}
if(((lst & 1) && !vec[7].empty()) || (!(lst & 1) && !vec[8].empty()))return puts("-1"),0;
else if(!vec[7].empty())ans.push_back(vec[7][0]);
else if(!vec[8].empty())ans.push_back(vec[8][0]);
if(pos[1] != vec[1].size() || pos[4] != vec[4].size())return puts("-1"),0;
for(int i = pos[2];i < vec[2].size();i++){
auto it = ans.insert(++rem[0],vec[2][i]);
rem[0] = it;
}
for(int i = pos[3];i < vec[3].size();i++){
auto it = ans.insert(++rem[1],vec[3][i]);
rem[1] = it;
}
for(auto it : ans)printf("%d ",it);
return 0;
}
Compilation message (stderr)
slagalica.cpp: In function 'int get(int)':
slagalica.cpp:19:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
19 | return pos[x] == vec[x].size() ? 0x7fffffff : vec[x][pos[x]];
| ~~~~~~~^~~~~~~~~~~~~~~~
slagalica.cpp: In function 'int main()':
slagalica.cpp:45:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
45 | for(int i = 0;i < G[lst].size();i++)nxt[i] = G[lst][i];
| ~~^~~~~~~~~~~~~~~
slagalica.cpp:47:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
47 | if(pos[nxt[0]] == vec[nxt[0]].size())break;
| ~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~
slagalica.cpp:56:12: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
56 | if(pos[1] != vec[1].size() || pos[4] != vec[4].size())return puts("-1"),0;
| ~~~~~~~^~~~~~~~~~~~~~~~
slagalica.cpp:56:39: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
56 | if(pos[1] != vec[1].size() || pos[4] != vec[4].size())return puts("-1"),0;
| ~~~~~~~^~~~~~~~~~~~~~~~
slagalica.cpp:57:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
57 | for(int i = pos[2];i < vec[2].size();i++){
| ~~^~~~~~~~~~~~~~~
slagalica.cpp:61:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
61 | for(int i = pos[3];i < vec[3].size();i++){
| ~~^~~~~~~~~~~~~~~
# | 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... |