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 <bits/stdc++.h>
#define fi first
#define se second
using namespace std;
typedef pair<int,int> pii;
pii change[9];
int n, cnt[5];
vector<int> type[9];
void makeChange() {
change[1] = pii(1,3);
change[2] = pii(1,4);
change[3] = pii(2,3);
change[4] = pii(2,4);
change[5] = pii(0,3);
change[6] = pii(0,4);
change[7] = pii(1,0);
change[8] = pii(2,0);
}
bool checkChoose(int t) {
int cnt1 = cnt[1] ;
int cnt2 = cnt[2] ;
int cnt3 = cnt[3] + (change[t].se == 3);
int cnt4 = cnt[4] + (change[t].se == 4);
return (cnt1 == cnt4 && cnt2 == cnt3);
}
bool cmp(int A, int B) {return A > B;}
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);
if (t < 5) {
cnt[change[t].fi]++;
cnt[change[t].se]++;
}
}
for (int i = 1; i <= 4; i++) sort(type[i].begin(),type[i].end(),cmp);
int st = (type[5].empty()) ? 6: 5;
int fin = (type[7].empty()) ? 8: 7;
if (fin == 7) cnt[1]++;
else cnt[2]++;
if (st == 5) cnt[3]++;
else cnt[4]++;
vector<int> res;
if (cnt[1] != cnt[4] || cnt[2] != cnt[3]) {
cout << -1;
return 0;
}
if (st == 5) cnt[3]--;
else cnt[4]--;
res.push_back(type[st].back());
int pre = (st == 5) ? 3: 4;
while (res.size() < n-1) {
int t;
if (pre == 3) {
if (type[3].empty()) t = 4;
else if (type[4].empty()) t = 3;
else if (!checkChoose(3) && !checkChoose(4)) assert(0);
else if (!checkChoose(4)) t = 3;
else if (!checkChoose(3)) t = 4;
else t = (type[3].back() < type[4].back()) ? 3 : 4;
} else {
if (type[1].empty()) t = 2;
else if (type[2].empty()) t = 1;
else if (!checkChoose(1) && !checkChoose(2)) assert(0);
else if (!checkChoose(2)) t = 1;
else if (!checkChoose(1)) t = 2;
else t = (type[1].back() < type[2].back()) ? 1 : 2;
}
res.push_back(type[t].back());
type[t].pop_back();
cnt[change[t].fi]--;
cnt[change[t].se]--;
pre = change[t].se;
}
res.push_back(type[fin].back());
for (int i: res) cout << i << ' ';
return 0;
}
Compilation message (stderr)
slagalica.cpp: In function 'int main()':
slagalica.cpp:62:23: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
62 | while (res.size() < n-1) {
| ~~~~~~~~~~~^~~~~
# | 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... |