#include <bits/stdc++.h>
#define fi first
#define se second
using namespace std;
typedef pair<int,int> pii;
pii change[9];
int n;
vector<int> type[9];
void makeChange() {
change[1] = pii(1,1);
change[2] = pii(1,2);
change[3] = pii(2,1);
change[4] = pii(2,2);
change[5] = pii(0,1);
change[6] = pii(0,2);
change[7] = pii(1,0);
change[8] = pii(2,0);
}
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> n;
makeChange();
for (int i = 1; i <= n; i++) {
int t,id;
cin >> t >> id;
type[t].push_back(id);
}
for (int i = 1; i <= 4; i++) sort(type[i].begin(),type[i].end(),greater<int> {});
int st = (type[5].empty()) ? 6: 5;
int fin = (type[7].empty()) ? 8: 7;
vector<pii> res;
res.push_back(pii(st,type[st].back()));
bool found = 1;
int pre = change[st].second;
while (found) {
int id;
if (pre == 1) {
if (type[3].empty() && type[4].empty()) found = 0;
else if (type[3].empty() || (!type[4].empty() && type[4].back() < type[3].back())) {
res.push_back(pii(4,type[4].back()));
type[4].pop_back();
id = 4;
} else {
res.push_back(pii(3,type[3].back()));
type[3].pop_back();
id = 3;
}
} else {
if (type[1].empty() && type[2].empty()) found = 0;
else if (type[1].empty() || (!type[2].empty() && type[2].back() < type[1].back())) {
res.push_back(pii(2,type[2].back()));
type[2].pop_back();
id = 2;
} else {
res.push_back(pii(1,type[1].back()));
type[1].pop_back();
id = 1;
}
}
if (found) pre = change[id].second;
}
// assert(!res.empty());
if (change[res.back().first].second == change[fin].first) {
cout << -1;
return 0;
}
if (!type[1].empty()) {
cout << -1;
return 0;
}
if (!type[4].empty()) {
cout << -1;
return 0;
}
int id;
if (type[2].empty() && type[3].empty()) {
res.push_back(pii(fin,type[fin].back()));
for (pii i: res) cout << i.second << ' ';
return 0;
} else id = (type[2].empty()) ? 3: 2;
int pos = -1;
for (int i = res.size() - 1; i >= 0; i--) {
if (change[res[i].first].second != change[id].first) {
pos = i;
break;
}
}
assert(pos != -1);
for (int i = 0; i <= pos; i++) cout << res[i].second << ' ';
for (int i = type[id].size() - 1; i >= 0; i--) cout << type[id][i] << ' ';
res.push_back(pii(fin,type[fin].back()));
for (int i = pos+1; i < (int) res.size(); i++) cout << res[i].second << ' ';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
26 ms |
2408 KB |
Output is correct |
2 |
Correct |
23 ms |
1860 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
33 ms |
2204 KB |
Output is correct |
2 |
Correct |
19 ms |
1904 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
22 ms |
1288 KB |
Output is correct |
2 |
Correct |
25 ms |
2240 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
1224 KB |
Output is correct |
2 |
Correct |
25 ms |
2184 KB |
Output is correct |
3 |
Correct |
30 ms |
2300 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
27 ms |
1996 KB |
Output is correct |
2 |
Correct |
19 ms |
1232 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
25 ms |
2252 KB |
Output is correct |
2 |
Correct |
19 ms |
1232 KB |
Output is correct |
3 |
Correct |
28 ms |
2240 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
1916 KB |
Output is correct |
2 |
Correct |
32 ms |
2236 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
35 ms |
2624 KB |
Output is correct |
2 |
Correct |
18 ms |
1740 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
25 ms |
2284 KB |
Output is correct |
2 |
Correct |
19 ms |
1832 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
30 ms |
2656 KB |
Output is correct |
2 |
Correct |
21 ms |
2960 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
24 ms |
2352 KB |
Output is correct |
2 |
Correct |
22 ms |
2880 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
26 ms |
2436 KB |
Output is correct |
2 |
Correct |
19 ms |
1868 KB |
Output is correct |