#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
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++){
| ~~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
3 |
Correct |
0 ms |
256 KB |
Output is correct |
4 |
Correct |
0 ms |
256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
256 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
36 ms |
5288 KB |
Output is correct |
2 |
Correct |
28 ms |
4644 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
35 ms |
5044 KB |
Output is correct |
2 |
Correct |
26 ms |
4348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
3444 KB |
Output is correct |
2 |
Correct |
38 ms |
5500 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
2940 KB |
Output is correct |
2 |
Correct |
35 ms |
4976 KB |
Output is correct |
3 |
Correct |
41 ms |
5916 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
35 ms |
5236 KB |
Output is correct |
2 |
Correct |
24 ms |
3188 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
35 ms |
5104 KB |
Output is correct |
2 |
Correct |
24 ms |
3188 KB |
Output is correct |
3 |
Correct |
39 ms |
5616 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
4352 KB |
Output is correct |
2 |
Correct |
37 ms |
5240 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
42 ms |
5964 KB |
Output is correct |
2 |
Correct |
27 ms |
4352 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
35 ms |
4984 KB |
Output is correct |
2 |
Correct |
27 ms |
4352 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
42 ms |
5836 KB |
Output is correct |
2 |
Correct |
31 ms |
4864 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
35 ms |
5008 KB |
Output is correct |
2 |
Correct |
27 ms |
4472 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
38 ms |
5128 KB |
Output is correct |
2 |
Correct |
31 ms |
4640 KB |
Output is correct |