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>
using namespace std;
#define arr array
#define pii pair<int, int>
#define fir first
#define sec second
const int R = 1e5 + 5, C = 2 + 5, K = 1e5 + 5, INF = 1e9;
int r, c, k;
arr<arr<int, C>, R> vl;
arr<int, K> blnc;
void prcmp() {
for (int i = 1; i <= r; i++)
blnc[vl[i][1]]++, blnc[vl[i][2]]--;
}
void swp(int i) {
blnc[vl[i][1]] -= 2, blnc[vl[i][2]] += 2;
swap(vl[i][1], vl[i][2]);
}
void cmp() {
while (true) {
pii extrm = {-1, -1};
for (int i = 1; i <= k; i++)
extrm = max(extrm, {abs(blnc[i]), i});
if (extrm.fir <= 1) break;
int x = extrm.sec;
if (blnc[x] > 0) {
pii extrm2 = {INF, INF};
for (int i = 1; i <= r; i++)
if (vl[i][1] == x) extrm2 = min(extrm2, {blnc[vl[i][2]], vl[i][2]});
for (int i = 1; i <= r; i++)
if (vl[i][2] == extrm2.sec) { swp(i); break; }
} else {
pii extrm2 = {-1, -1};
for (int i = 1; i <= r; i++)
if (vl[i][2] == x) extrm2 = max(extrm2, {blnc[vl[i][1]], vl[i][1]});
for (int i = 1; i <= r; i++)
if (vl[i][1] == extrm2.sec) { swp(i); break; }
}
}
}
int main() {
// freopen("bl.in", "r", stdin);
cin >> r >> c >> k; assert(c == 2);
for (int i = 1; i <= r; i++)
for (int j = 1; j <= c; j++)
cin >> vl[i][j];
prcmp();
cmp();
for (int i = 1; i <= r; i++) {
for (int j = 1; j <= c; j++) {
cout << vl[i][j] << " ";
}
cout << endl;
}
}
# | 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... |