#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;
dfs(u);
ptr[v] += 1;
}
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<pair<int, 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]].emplace_back(i, j);
}
}
vector<int> f(t);
iota(f.begin(), f.end(), 0);
for (int i = 0; i < t; i++) {
int c = (int) pos[i].size() / s;
if (c > 1) {
for (int it = 0; it < c - 1; it++) {
t += 1;
f.push_back(i);
pos.push_back({});
for (int j = 0; j < s; j++) {
pair<int, int> v = pos[i].back();
pos.back().push_back(v);
a[v.first][v.second] = t - 1;
pos[i].pop_back();
}
}
}
}
for (int i = 0; i < t; i++) {
if ((int) pos[i].size() != s && (int) pos[i].size() != 0) {
while (true) {
}
}
}
/* for (int i = 0; i < n; i++) {
for (int j = 0; j < s; j++) {
cout << a[i][j] << " ";
}
cout << endl;
}
cout << "t = " << t << endl; */
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;
}
vector<vector<int>> L(n);
vector<vector<int>> R(n);
for (int i = 0; i < n; i++) {
if (ptr[i] < (int) g[i].size()) {
euler.clear();
dfs(i);
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);
}
}
}
}
/* if (l == 0 && r == 1) {
cout << "euler = ";
for (int i : euler) {
cout << i << " ";
}
cout << endl;
}
if (l == 0 && r == 1) {
cout << "L" << endl;
for (int i = 0; i < n; i++) {
for (int v : L[i]) {
cout << v << " ";
}
cout << endl;
}
cout << "R" << endl;
for (int i = 0; i < n; i++) {
for (int v : L[i]) {
cout << v << " ";
}
cout << endl;
}
} */
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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2076 ms |
24924 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2062 ms |
24924 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
128 ms |
96676 KB |
Correct |
2 |
Correct |
111 ms |
89160 KB |
Correct |
3 |
Correct |
101 ms |
79832 KB |
Correct |
4 |
Correct |
76 ms |
82852 KB |
Correct |
5 |
Correct |
103 ms |
98292 KB |
Correct |
6 |
Correct |
117 ms |
93760 KB |
Correct |
7 |
Correct |
110 ms |
80212 KB |
Correct |
8 |
Correct |
87 ms |
94780 KB |
Correct |
9 |
Correct |
88 ms |
94664 KB |
Correct |
10 |
Correct |
71 ms |
94920 KB |
Correct |
11 |
Correct |
71 ms |
94568 KB |
Correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
128 ms |
96676 KB |
Correct |
2 |
Correct |
111 ms |
89160 KB |
Correct |
3 |
Correct |
101 ms |
79832 KB |
Correct |
4 |
Correct |
76 ms |
82852 KB |
Correct |
5 |
Correct |
103 ms |
98292 KB |
Correct |
6 |
Correct |
117 ms |
93760 KB |
Correct |
7 |
Correct |
110 ms |
80212 KB |
Correct |
8 |
Correct |
87 ms |
94780 KB |
Correct |
9 |
Correct |
88 ms |
94664 KB |
Correct |
10 |
Correct |
71 ms |
94920 KB |
Correct |
11 |
Correct |
71 ms |
94568 KB |
Correct |
12 |
Correct |
114 ms |
97444 KB |
Correct |
13 |
Correct |
108 ms |
90800 KB |
Correct |
14 |
Correct |
120 ms |
81792 KB |
Correct |
15 |
Correct |
86 ms |
83384 KB |
Correct |
16 |
Correct |
107 ms |
99232 KB |
Correct |
17 |
Correct |
128 ms |
93104 KB |
Correct |
18 |
Correct |
98 ms |
81440 KB |
Correct |
19 |
Correct |
88 ms |
94924 KB |
Correct |
20 |
Correct |
87 ms |
94664 KB |
Correct |
21 |
Correct |
70 ms |
94924 KB |
Correct |
22 |
Correct |
71 ms |
94660 KB |
Correct |
23 |
Execution timed out |
2051 ms |
40396 KB |
Time limit exceeded |
24 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2062 ms |
24924 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
29016 KB |
Correct |
2 |
Correct |
8 ms |
30300 KB |
Correct |
3 |
Correct |
7 ms |
30044 KB |
Correct |
4 |
Correct |
8 ms |
31324 KB |
Correct |
5 |
Correct |
6 ms |
31460 KB |
Correct |
6 |
Correct |
8 ms |
32092 KB |
Correct |
7 |
Correct |
7 ms |
31280 KB |
Correct |
8 |
Correct |
8 ms |
31064 KB |
Correct |
9 |
Correct |
7 ms |
30552 KB |
Correct |
10 |
Correct |
8 ms |
31324 KB |
Correct |
11 |
Correct |
7 ms |
30988 KB |
Correct |
12 |
Correct |
7 ms |
32092 KB |
Correct |
13 |
Correct |
7 ms |
32092 KB |
Correct |
14 |
Correct |
7 ms |
32088 KB |
Correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
29016 KB |
Correct |
2 |
Correct |
8 ms |
30300 KB |
Correct |
3 |
Correct |
7 ms |
30044 KB |
Correct |
4 |
Correct |
8 ms |
31324 KB |
Correct |
5 |
Correct |
6 ms |
31460 KB |
Correct |
6 |
Correct |
8 ms |
32092 KB |
Correct |
7 |
Correct |
7 ms |
31280 KB |
Correct |
8 |
Correct |
8 ms |
31064 KB |
Correct |
9 |
Correct |
7 ms |
30552 KB |
Correct |
10 |
Correct |
8 ms |
31324 KB |
Correct |
11 |
Correct |
7 ms |
30988 KB |
Correct |
12 |
Correct |
7 ms |
32092 KB |
Correct |
13 |
Correct |
7 ms |
32092 KB |
Correct |
14 |
Correct |
7 ms |
32088 KB |
Correct |
15 |
Correct |
4 ms |
29020 KB |
Correct |
16 |
Correct |
8 ms |
30300 KB |
Correct |
17 |
Correct |
8 ms |
30044 KB |
Correct |
18 |
Correct |
7 ms |
31344 KB |
Correct |
19 |
Correct |
7 ms |
31552 KB |
Correct |
20 |
Correct |
7 ms |
32092 KB |
Correct |
21 |
Correct |
7 ms |
31324 KB |
Correct |
22 |
Correct |
9 ms |
31068 KB |
Correct |
23 |
Correct |
9 ms |
30740 KB |
Correct |
24 |
Correct |
8 ms |
31320 KB |
Correct |
25 |
Correct |
7 ms |
31068 KB |
Correct |
26 |
Correct |
7 ms |
32160 KB |
Correct |
27 |
Correct |
7 ms |
32092 KB |
Correct |
28 |
Correct |
7 ms |
32016 KB |
Correct |
29 |
Execution timed out |
2040 ms |
25176 KB |
Time limit exceeded |
30 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
29016 KB |
Correct |
2 |
Correct |
8 ms |
30300 KB |
Correct |
3 |
Correct |
7 ms |
30044 KB |
Correct |
4 |
Correct |
8 ms |
31324 KB |
Correct |
5 |
Correct |
6 ms |
31460 KB |
Correct |
6 |
Correct |
8 ms |
32092 KB |
Correct |
7 |
Correct |
7 ms |
31280 KB |
Correct |
8 |
Correct |
8 ms |
31064 KB |
Correct |
9 |
Correct |
7 ms |
30552 KB |
Correct |
10 |
Correct |
8 ms |
31324 KB |
Correct |
11 |
Correct |
7 ms |
30988 KB |
Correct |
12 |
Correct |
7 ms |
32092 KB |
Correct |
13 |
Correct |
7 ms |
32092 KB |
Correct |
14 |
Correct |
7 ms |
32088 KB |
Correct |
15 |
Correct |
4 ms |
29020 KB |
Correct |
16 |
Correct |
8 ms |
30300 KB |
Correct |
17 |
Correct |
8 ms |
30044 KB |
Correct |
18 |
Correct |
7 ms |
31344 KB |
Correct |
19 |
Correct |
7 ms |
31552 KB |
Correct |
20 |
Correct |
7 ms |
32092 KB |
Correct |
21 |
Correct |
7 ms |
31324 KB |
Correct |
22 |
Correct |
9 ms |
31068 KB |
Correct |
23 |
Correct |
9 ms |
30740 KB |
Correct |
24 |
Correct |
8 ms |
31320 KB |
Correct |
25 |
Correct |
7 ms |
31068 KB |
Correct |
26 |
Correct |
7 ms |
32160 KB |
Correct |
27 |
Correct |
7 ms |
32092 KB |
Correct |
28 |
Correct |
7 ms |
32016 KB |
Correct |
29 |
Execution timed out |
2040 ms |
25176 KB |
Time limit exceeded |
30 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
128 ms |
96676 KB |
Correct |
2 |
Correct |
111 ms |
89160 KB |
Correct |
3 |
Correct |
101 ms |
79832 KB |
Correct |
4 |
Correct |
76 ms |
82852 KB |
Correct |
5 |
Correct |
103 ms |
98292 KB |
Correct |
6 |
Correct |
117 ms |
93760 KB |
Correct |
7 |
Correct |
110 ms |
80212 KB |
Correct |
8 |
Correct |
87 ms |
94780 KB |
Correct |
9 |
Correct |
88 ms |
94664 KB |
Correct |
10 |
Correct |
71 ms |
94920 KB |
Correct |
11 |
Correct |
71 ms |
94568 KB |
Correct |
12 |
Correct |
4 ms |
29016 KB |
Correct |
13 |
Correct |
8 ms |
30300 KB |
Correct |
14 |
Correct |
7 ms |
30044 KB |
Correct |
15 |
Correct |
8 ms |
31324 KB |
Correct |
16 |
Correct |
6 ms |
31460 KB |
Correct |
17 |
Correct |
8 ms |
32092 KB |
Correct |
18 |
Correct |
7 ms |
31280 KB |
Correct |
19 |
Correct |
8 ms |
31064 KB |
Correct |
20 |
Correct |
7 ms |
30552 KB |
Correct |
21 |
Correct |
8 ms |
31324 KB |
Correct |
22 |
Correct |
7 ms |
30988 KB |
Correct |
23 |
Correct |
7 ms |
32092 KB |
Correct |
24 |
Correct |
7 ms |
32092 KB |
Correct |
25 |
Correct |
7 ms |
32088 KB |
Correct |
26 |
Correct |
121 ms |
96676 KB |
Correct |
27 |
Correct |
111 ms |
91252 KB |
Correct |
28 |
Correct |
102 ms |
81516 KB |
Correct |
29 |
Correct |
85 ms |
82108 KB |
Correct |
30 |
Correct |
115 ms |
98784 KB |
Correct |
31 |
Correct |
114 ms |
94132 KB |
Correct |
32 |
Correct |
94 ms |
81448 KB |
Correct |
33 |
Correct |
92 ms |
94972 KB |
Correct |
34 |
Correct |
85 ms |
94708 KB |
Correct |
35 |
Correct |
76 ms |
94920 KB |
Correct |
36 |
Correct |
70 ms |
94576 KB |
Correct |
37 |
Correct |
3 ms |
29020 KB |
Correct |
38 |
Correct |
9 ms |
30520 KB |
Correct |
39 |
Correct |
10 ms |
30044 KB |
Correct |
40 |
Correct |
7 ms |
31164 KB |
Correct |
41 |
Correct |
6 ms |
31552 KB |
Correct |
42 |
Correct |
8 ms |
32092 KB |
Correct |
43 |
Correct |
8 ms |
31420 KB |
Correct |
44 |
Correct |
8 ms |
31100 KB |
Correct |
45 |
Correct |
7 ms |
30556 KB |
Correct |
46 |
Correct |
8 ms |
31320 KB |
Correct |
47 |
Correct |
9 ms |
31068 KB |
Correct |
48 |
Correct |
8 ms |
32192 KB |
Correct |
49 |
Correct |
7 ms |
32012 KB |
Correct |
50 |
Correct |
7 ms |
31984 KB |
Correct |
51 |
Correct |
350 ms |
131464 KB |
Correct |
52 |
Correct |
303 ms |
92092 KB |
Correct |
53 |
Correct |
53 ms |
42208 KB |
Correct |
54 |
Correct |
103 ms |
58692 KB |
Correct |
55 |
Correct |
257 ms |
84928 KB |
Correct |
56 |
Correct |
316 ms |
118524 KB |
Correct |
57 |
Correct |
352 ms |
126448 KB |
Correct |
58 |
Correct |
301 ms |
89184 KB |
Correct |
59 |
Correct |
231 ms |
79072 KB |
Correct |
60 |
Correct |
281 ms |
111884 KB |
Correct |
61 |
Correct |
328 ms |
122144 KB |
Correct |
62 |
Correct |
175 ms |
115016 KB |
Correct |
63 |
Correct |
153 ms |
114856 KB |
Correct |
64 |
Correct |
164 ms |
114736 KB |
Correct |
65 |
Correct |
169 ms |
114712 KB |
Correct |
66 |
Correct |
164 ms |
116676 KB |
Correct |
67 |
Correct |
163 ms |
127396 KB |
Correct |
68 |
Correct |
167 ms |
127428 KB |
Correct |
69 |
Correct |
164 ms |
122972 KB |
Correct |
70 |
Correct |
188 ms |
99776 KB |
Correct |
71 |
Correct |
190 ms |
107340 KB |
Correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
128 ms |
96676 KB |
Correct |
2 |
Correct |
111 ms |
89160 KB |
Correct |
3 |
Correct |
101 ms |
79832 KB |
Correct |
4 |
Correct |
76 ms |
82852 KB |
Correct |
5 |
Correct |
103 ms |
98292 KB |
Correct |
6 |
Correct |
117 ms |
93760 KB |
Correct |
7 |
Correct |
110 ms |
80212 KB |
Correct |
8 |
Correct |
87 ms |
94780 KB |
Correct |
9 |
Correct |
88 ms |
94664 KB |
Correct |
10 |
Correct |
71 ms |
94920 KB |
Correct |
11 |
Correct |
71 ms |
94568 KB |
Correct |
12 |
Correct |
114 ms |
97444 KB |
Correct |
13 |
Correct |
108 ms |
90800 KB |
Correct |
14 |
Correct |
120 ms |
81792 KB |
Correct |
15 |
Correct |
86 ms |
83384 KB |
Correct |
16 |
Correct |
107 ms |
99232 KB |
Correct |
17 |
Correct |
128 ms |
93104 KB |
Correct |
18 |
Correct |
98 ms |
81440 KB |
Correct |
19 |
Correct |
88 ms |
94924 KB |
Correct |
20 |
Correct |
87 ms |
94664 KB |
Correct |
21 |
Correct |
70 ms |
94924 KB |
Correct |
22 |
Correct |
71 ms |
94660 KB |
Correct |
23 |
Execution timed out |
2051 ms |
40396 KB |
Time limit exceeded |
24 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2076 ms |
24924 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |