#include <bits/stdc++.h>
using namespace std;
int main(){
int n;
cin >> n;
vector<int> vec[10], g[10];
for (int i = 0; i < n; i ++){
int x, y;
cin >> x >> y;
vec[x].push_back(y);
}
for (int i = 1; i <= 8; i ++)
sort(vec[i].rbegin(), vec[i].rend());
g[5] = {3, 4};
g[6] = {1, 2};
g[1] = {3, 4};
g[2] = {1, 2};
g[3] = {3, 4};
g[4] = {1, 2};
int f = 5;
if (vec[6].size()) f = 6;
int l = 7;
if (vec[8].size()) l = 8;
int c1 = vec[1].size();
int c2 = vec[2].size();
int c3 = vec[3].size();
int c4 = vec[4].size();
if (abs(c1 - c4) >= 2){
cout << -1 << endl;
return 0;
}
if (c1 > c4 and l == 7){
cout << -1 << endl;
return 0;
}
if (c1 < c4 and l == 8){
cout << -1 << endl;
return 0;
}
if (c1 == c4 and ((f==6 and l==8) or (f==5 and l==7))){
cout << -1 << endl;
return 0;
}
vector<int> answer, types;
for (int x : vec[5]){
answer.push_back(x);
types.push_back(5);
}
for (int x : vec[6]){
answer.push_back(x);
types.push_back(6);
}
while (1){
int last = types.back();
int min_t = -1;
for (int t : g[last]){
if (vec[t].size() == 0) continue;
if (t == 3 and vec[1].size() == 0){
min_t = 3;
break;
}
if (t == 2 and vec[4].size() == 0){
min_t = 2;
break;
}
if (vec[t].size() and (min_t == -1 or vec[min_t].back() > vec[t].back()))
min_t = t;
}
bool all = 1;
for (int i = 1; i <= 4; i ++)
all &= (vec[i].size() == 0);
if (all) break;
if (!all and min_t == -1){
cout << -1 << endl;
return 0;
}
types.push_back(min_t);
answer.push_back(vec[min_t].back());
vec[min_t].pop_back();
}
int last = types.back();
int have = 7;
if (vec[8].size()) have = 8;
if ((have == 7 and (last == 2 or last == 4)) or (have == 8 and (last == 1 or last == 3)))
answer.push_back(vec[have][0]);
else{
cout << -1 << endl;
return 0;
}
for (int x : answer)
cout << x << " ";
cout << endl;
}
Compilation message
slagalica.cpp: In function 'int main()':
slagalica.cpp:30:9: warning: unused variable 'c2' [-Wunused-variable]
30 | int c2 = vec[2].size();
| ^~
slagalica.cpp:31:9: warning: unused variable 'c3' [-Wunused-variable]
31 | int c3 = vec[3].size();
| ^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
73 ms |
3188 KB |
Output is correct |
2 |
Correct |
35 ms |
1536 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
37 ms |
3120 KB |
Output is correct |
2 |
Correct |
40 ms |
2360 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
38 ms |
1744 KB |
Output is correct |
2 |
Correct |
40 ms |
3056 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
33 ms |
2088 KB |
Output is correct |
2 |
Correct |
69 ms |
3480 KB |
Output is correct |
3 |
Correct |
44 ms |
3460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
45 ms |
3056 KB |
Output is correct |
2 |
Correct |
39 ms |
1672 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
44 ms |
3396 KB |
Output is correct |
2 |
Correct |
46 ms |
2160 KB |
Output is correct |
3 |
Correct |
49 ms |
3436 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
38 ms |
1740 KB |
Output is correct |
2 |
Correct |
40 ms |
3260 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
66 ms |
3700 KB |
Output is correct |
2 |
Correct |
34 ms |
1620 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
44 ms |
3140 KB |
Output is correct |
2 |
Correct |
40 ms |
2380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
53 ms |
3600 KB |
Output is correct |
2 |
Correct |
41 ms |
2160 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
38 ms |
3116 KB |
Output is correct |
2 |
Correct |
46 ms |
1736 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
51 ms |
3140 KB |
Output is correct |
2 |
Correct |
34 ms |
1760 KB |
Output is correct |