#include <bits/stdc++.h>
using namespace std;
const int N = (int) (1e6) + 100;
int ptr[N], from[N], to[N];
vector<int> g[N];
vector<int> euler;
bool was[N];
void dfs(int v) {
while (ptr[v] < (int) g[v].size()) {
int e = g[v][ptr[v]];
if (was[e]) {
ptr[v] += 1;
continue;
}
int u = (from[e] ^ to[e] ^ v);
was[e] = true;
ptr[v] += 1;
dfs(u);
}
euler.push_back(v);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, s, t;
cin >> n >> s >> t;
vector<vector<int>> a(n, vector<int>(s));
vector<vector<int>> pos(t);
for (int i = 0; i < n; i++) {
for (int j = 0; j < s; j++) {
cin >> a[i][j];
--a[i][j];
pos[a[i][j]].push_back(i);
}
}
vector<int> f(t);
iota(f.begin(), f.end(), 0);
for (int i = 0; i < t; i++) {
if ((int) pos[i].size() > s) {
t += 1;
pos.push_back({});
f.push_back(i);
for (int j = s; j < (int) pos[i].size(); j++) {
int v = pos[i].back();
if ((int) pos.back().size() < s) {
pos.back().push_back(v);
} else {
t += 1;
pos.push_back({});
f.push_back(i);
pos.back().push_back(v);
}
pos[i].pop_back();
}
}
}
vector<vector<int>> res(n, vector<int>(s, -1));
function<void(int, int, vector<vector<int>>)> Solve = [&](int l, int r, vector<vector<int>> a) {
if (l == r) {
for (int i = 0; i < n; i++) {
res[i][l] = a[i][0];
}
return;
}
int m = r - l + 1;
/* vector<vector<int>> pos(t);
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
pos[a[i][j]].push_back(i);
}
} */
for (int i = 0; i < n + t; i++) {
g[i].clear();
ptr[i] = 0;
}
int p = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
from[p] = i;
to[p] = n + a[i][j];
g[i].push_back(p);
g[n + a[i][j]].push_back(p);
p += 1;
}
}
for (int i = 0; i < p; i++) {
was[i] = false;
}
euler.clear();
for (int i = 0; i < n; i++) {
if (ptr[i] < (int) g[i].size()) {
dfs(i);
}
}
vector<vector<int>> L(n);
vector<vector<int>> R(n);
int sz = (int) euler.size();
for (int i = 0; i < sz; i += 2) {
if (i + 1 < sz) {
L[euler[i]].push_back(euler[i + 1] - n);
}
if (i > 0) {
R[euler[i]].push_back(euler[i - 1] - n);
}
}
int mid = (l + r) >> 1;
Solve(l, mid, L);
Solve(mid + 1, r, R);
};
Solve(0, s - 1, a);
for (int i = 0; i < n; i++) {
for (int j = 0; j < s; j++) {
cout << f[res[i][j]] + 1 << " ";
}
cout << '\n';
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
23 ms |
58972 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
23 ms |
58968 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
115 ms |
94888 KB |
Correct |
2 |
Correct |
104 ms |
93096 KB |
Correct |
3 |
Correct |
102 ms |
84172 KB |
Correct |
4 |
Correct |
75 ms |
87432 KB |
Correct |
5 |
Correct |
102 ms |
96512 KB |
Correct |
6 |
Correct |
114 ms |
95276 KB |
Correct |
7 |
Runtime error |
94 ms |
141768 KB |
Execution killed with signal 11 |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
115 ms |
94888 KB |
Correct |
2 |
Correct |
104 ms |
93096 KB |
Correct |
3 |
Correct |
102 ms |
84172 KB |
Correct |
4 |
Correct |
75 ms |
87432 KB |
Correct |
5 |
Correct |
102 ms |
96512 KB |
Correct |
6 |
Correct |
114 ms |
95276 KB |
Correct |
7 |
Runtime error |
94 ms |
141768 KB |
Execution killed with signal 11 |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
23 ms |
58968 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
29020 KB |
Correct |
2 |
Runtime error |
30 ms |
61132 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
29020 KB |
Correct |
2 |
Runtime error |
30 ms |
61132 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
29020 KB |
Correct |
2 |
Runtime error |
30 ms |
61132 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
115 ms |
94888 KB |
Correct |
2 |
Correct |
104 ms |
93096 KB |
Correct |
3 |
Correct |
102 ms |
84172 KB |
Correct |
4 |
Correct |
75 ms |
87432 KB |
Correct |
5 |
Correct |
102 ms |
96512 KB |
Correct |
6 |
Correct |
114 ms |
95276 KB |
Correct |
7 |
Runtime error |
94 ms |
141768 KB |
Execution killed with signal 11 |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
115 ms |
94888 KB |
Correct |
2 |
Correct |
104 ms |
93096 KB |
Correct |
3 |
Correct |
102 ms |
84172 KB |
Correct |
4 |
Correct |
75 ms |
87432 KB |
Correct |
5 |
Correct |
102 ms |
96512 KB |
Correct |
6 |
Correct |
114 ms |
95276 KB |
Correct |
7 |
Runtime error |
94 ms |
141768 KB |
Execution killed with signal 11 |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
23 ms |
58972 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |