#include <bits/stdc++.h>
using namespace std;
vector<vector<int>> A;
int B[100005]; // B[val] = id
int C[100005];
int D[100005]; // D[id] = sum
int E[100005]; // current
int N, S;
vector<vector<array<int, 4>>> adj2;
vector<int> path;
void Euler(int c) {
while(adj2[c].size() > 0) {
if(adj2[c].back()[1] == 0) {
adj2[c].pop_back();
continue;
}
int sz = adj2[c].size();
adj2[c][sz-1][1]--;
int a = adj2[c][sz-1][2], b = adj2[c][sz-1][3];
if(adj2[a].size()>b) {
adj2[a][b][1]--;
}
Euler(adj2[c][sz-1][0]);
}
path.push_back(c);
}
void addEdge(int u, int v) {
//cout << "ADD : " << u << ' ' << v << '\n';
int s1 = adj2[u].size(), s2 = adj2[v].size();
adj2[u].push_back({v, 1, v, s2});
adj2[v].push_back({u, 1, u, s1});
}
vector<int> DnC2(int s, int e, int a, int b) {
int i, j;
if(s>=e) return {};
if(s+1==e) {
vector<int> v;
for(i=a;i<b;i++) v.push_back(A[s][i]);
v.erase(unique(v.begin(),v.end()),v.end());
return v;
}
int mid = (s+e)/2;
vector<int> v1 = DnC2(s, mid, a, b);
vector<int> v2 = DnC2(mid, e, a, b);
vector<int> v;
v.resize(v1.size()+v2.size());
merge(v1.begin(),v1.end(),v2.begin(),v2.end(),v.begin());
v.erase(unique(v.begin(),v.end()),v.end());
return v;
}
vector<vector<array<int, 4>>> V;
void DnC(int s, int e) {
if(e==s+1) return;
int i, j;
vector<int> S = DnC2(0, N, s, e);
int T = S.size();
for(i=0;i<T;i++) B[S[i]] = i;
int mid = (s+e)/2;
for(i=0;i<T;i++) {
D[i] = 0;
}
for(i=0;i<N;i++) {
for(j=s;j<e;j++) {
D[B[A[i][j]]]++;
}
}
adj2.clear();
adj2.resize(T+N);
vector<int> v;
for(i=0;i<T;i++) {
if(D[i] % 2 == 0) D[i] = 0;
else v.push_back(i);
}
assert(v.size()%2==0);
int sz = v.size();
for(i=0;i<sz;i+=2) addEdge(N+v[i], N+v[i+1]);
v.clear();
V.clear();
V.resize(N);
for(i=0;i<N;i++) {
for(j=s;j<e;) {
int pt = j;
while(pt+1<e&&A[i][j]==A[i][pt+1]) pt++;
//[j ~ pt]
V[i].push_back({j, pt, A[i][j], -1});
j = pt + 1;
}
for(j=0;j<V[i].size();j++) {
int v = V[i][j][1] - V[i][j][0] + 1;
if(v % 2 == 0) V[i][j][3] = 0;
if(v % 2 == 1) {
addEdge(i, N+B[V[i][j][2]]);
}
}
}
//cout << "Adding Edge done\n";
//Euler(0);
vector<vector<array<int, 2>>> adj;
//cout << s << ' ' << e << " Euler Done\n";
adj.resize(N);
for(j=N;j<T+N;j++) {
path.clear();
Euler(j);
bool isrev = false;
for(i=0;i+1<path.size();i++) {
if(path[i+1]==j&&path[i]>=N) isrev = true;
}
if(isrev) reverse(path.begin(),path.end());
int pt = -1;
for(i=0;i+1<path.size();i++) {
if(path[i]==j&&path[i+1]>=N) pt = i;
}
if(pt != -1) {
vector<int> path2;
for(i=pt;i<path.size();i++) path2.push_back(path[i]);
for(i=1;i<pt;i++) path2.push_back(path[i]);
path = path2;
}
for(i=0;i+1<path.size();i++) {
int x = path[i], y = path[i+1];
if(x>y)swap(x, y);
if(x < N && y >= N) {
adj[x].push_back({y-N, 2*(i % 2)-1});
}
}
}
for(i=0;i<N;i++) {
/*cout << i << " : ";
for(auto n : adj[i]) cout << "("<<n[0]<<", " << n[1] << ") ";
cout << '\n';*/
for(auto n : adj[i]) C[n[0]] = n[1];
int l = s, r = mid;
for(auto n : V[i]) {
int a = -1, b = -1;
int v = n[1] - n[0] + 1;
if(v%2==0) a = v/2, b=v/2;
else {
if(C[B[n[2]]]==1) a = v/2+1, b = v/2;
else a = v/2, b = v/2+1;
}
//cout << n[2] << " : " << a << ' ' << b <<'\n';
assert(l+a-1<mid&&r+b-1<e);
for(j=l;j<=l+a-1;j++) A[i][j] = n[2];
l += a;
for(j=r;j<=r+b-1;j++) A[i][j] = n[2];
r += b;
}
assert(l == mid && r == e);
}
adj.clear();
DnC(s, mid);
DnC(mid, e);
}
signed main() {
//cin.sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int T;
cin >> N >> S >> T;
A.resize(N);
int i, j;
for(i=0;i<N;i++) A[i].resize(S);
for(i=0;i<N;i++) {
for(j=0;j<S;j++) cin >> A[i][j];
}
for(i=0;i<N;i++) sort(A[i].begin(),A[i].end());
DnC(0, S);
for(i=0;i<N;i++) {
for(j=0;j<S;j++)cout << A[i][j] << ' ';
cout << '\n';
}
}
Compilation message
balance.cpp: In function 'void Euler(int)':
balance.cpp:20:26: warning: comparison of integer expressions of different signedness: 'std::vector<std::array<int, 4> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
20 | if(adj2[a].size()>b) {
| ~~~~~~~~~~~~~~^~
balance.cpp: In function 'std::vector<int> DnC2(int, int, int, int)':
balance.cpp:34:12: warning: unused variable 'j' [-Wunused-variable]
34 | int i, j;
| ^
balance.cpp: In function 'void DnC(int, int)':
balance.cpp:88:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::array<int, 4> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
88 | for(j=0;j<V[i].size();j++) {
| ~^~~~~~~~~~~~
balance.cpp:105:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
105 | for(i=0;i+1<path.size();i++) {
| ~~~^~~~~~~~~~~~
balance.cpp:110:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
110 | for(i=0;i+1<path.size();i++) {
| ~~~^~~~~~~~~~~~
balance.cpp:115:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
115 | for(i=pt;i<path.size();i++) path2.push_back(path[i]);
| ~^~~~~~~~~~~~
balance.cpp:119:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
119 | for(i=0;i+1<path.size();i++) {
| ~~~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Correct |
2 |
Correct |
0 ms |
348 KB |
Correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Correct |
2 |
Runtime error |
0 ms |
604 KB |
Execution killed with signal 6 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
125 ms |
39500 KB |
Correct |
2 |
Correct |
132 ms |
38724 KB |
Correct |
3 |
Correct |
114 ms |
33788 KB |
Correct |
4 |
Correct |
68 ms |
33320 KB |
Correct |
5 |
Correct |
120 ms |
37988 KB |
Correct |
6 |
Correct |
130 ms |
40672 KB |
Correct |
7 |
Correct |
119 ms |
34036 KB |
Correct |
8 |
Correct |
111 ms |
42020 KB |
Correct |
9 |
Correct |
104 ms |
41640 KB |
Correct |
10 |
Correct |
83 ms |
41032 KB |
Correct |
11 |
Correct |
83 ms |
40760 KB |
Correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
125 ms |
39500 KB |
Correct |
2 |
Correct |
132 ms |
38724 KB |
Correct |
3 |
Correct |
114 ms |
33788 KB |
Correct |
4 |
Correct |
68 ms |
33320 KB |
Correct |
5 |
Correct |
120 ms |
37988 KB |
Correct |
6 |
Correct |
130 ms |
40672 KB |
Correct |
7 |
Correct |
119 ms |
34036 KB |
Correct |
8 |
Correct |
111 ms |
42020 KB |
Correct |
9 |
Correct |
104 ms |
41640 KB |
Correct |
10 |
Correct |
83 ms |
41032 KB |
Correct |
11 |
Correct |
83 ms |
40760 KB |
Correct |
12 |
Correct |
142 ms |
39512 KB |
Correct |
13 |
Correct |
125 ms |
38876 KB |
Correct |
14 |
Correct |
101 ms |
33796 KB |
Correct |
15 |
Correct |
71 ms |
33320 KB |
Correct |
16 |
Correct |
108 ms |
38196 KB |
Correct |
17 |
Correct |
139 ms |
40684 KB |
Correct |
18 |
Correct |
129 ms |
34032 KB |
Correct |
19 |
Correct |
107 ms |
41960 KB |
Correct |
20 |
Correct |
143 ms |
41640 KB |
Correct |
21 |
Correct |
91 ms |
41080 KB |
Correct |
22 |
Correct |
95 ms |
40824 KB |
Correct |
23 |
Correct |
154 ms |
42660 KB |
Correct |
24 |
Runtime error |
93 ms |
71264 KB |
Execution killed with signal 6 |
25 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Correct |
2 |
Runtime error |
0 ms |
604 KB |
Execution killed with signal 6 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Correct |
2 |
Correct |
9 ms |
1116 KB |
Correct |
3 |
Correct |
5 ms |
344 KB |
Correct |
4 |
Correct |
7 ms |
1600 KB |
Correct |
5 |
Correct |
5 ms |
1628 KB |
Correct |
6 |
Correct |
5 ms |
2396 KB |
Correct |
7 |
Correct |
5 ms |
1372 KB |
Correct |
8 |
Correct |
8 ms |
1624 KB |
Correct |
9 |
Correct |
8 ms |
1508 KB |
Correct |
10 |
Correct |
7 ms |
1884 KB |
Correct |
11 |
Correct |
4 ms |
1620 KB |
Correct |
12 |
Correct |
5 ms |
2396 KB |
Correct |
13 |
Correct |
4 ms |
2396 KB |
Correct |
14 |
Correct |
4 ms |
2396 KB |
Correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Correct |
2 |
Correct |
9 ms |
1116 KB |
Correct |
3 |
Correct |
5 ms |
344 KB |
Correct |
4 |
Correct |
7 ms |
1600 KB |
Correct |
5 |
Correct |
5 ms |
1628 KB |
Correct |
6 |
Correct |
5 ms |
2396 KB |
Correct |
7 |
Correct |
5 ms |
1372 KB |
Correct |
8 |
Correct |
8 ms |
1624 KB |
Correct |
9 |
Correct |
8 ms |
1508 KB |
Correct |
10 |
Correct |
7 ms |
1884 KB |
Correct |
11 |
Correct |
4 ms |
1620 KB |
Correct |
12 |
Correct |
5 ms |
2396 KB |
Correct |
13 |
Correct |
4 ms |
2396 KB |
Correct |
14 |
Correct |
4 ms |
2396 KB |
Correct |
15 |
Correct |
0 ms |
344 KB |
Correct |
16 |
Correct |
9 ms |
1188 KB |
Correct |
17 |
Correct |
4 ms |
504 KB |
Correct |
18 |
Correct |
6 ms |
1624 KB |
Correct |
19 |
Correct |
3 ms |
1628 KB |
Correct |
20 |
Correct |
5 ms |
2396 KB |
Correct |
21 |
Correct |
5 ms |
1368 KB |
Correct |
22 |
Correct |
8 ms |
1628 KB |
Correct |
23 |
Correct |
8 ms |
1500 KB |
Correct |
24 |
Correct |
7 ms |
1884 KB |
Correct |
25 |
Correct |
4 ms |
1628 KB |
Correct |
26 |
Correct |
5 ms |
2396 KB |
Correct |
27 |
Correct |
4 ms |
2396 KB |
Correct |
28 |
Correct |
5 ms |
2396 KB |
Correct |
29 |
Runtime error |
4 ms |
1628 KB |
Execution killed with signal 6 |
30 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Correct |
2 |
Correct |
9 ms |
1116 KB |
Correct |
3 |
Correct |
5 ms |
344 KB |
Correct |
4 |
Correct |
7 ms |
1600 KB |
Correct |
5 |
Correct |
5 ms |
1628 KB |
Correct |
6 |
Correct |
5 ms |
2396 KB |
Correct |
7 |
Correct |
5 ms |
1372 KB |
Correct |
8 |
Correct |
8 ms |
1624 KB |
Correct |
9 |
Correct |
8 ms |
1508 KB |
Correct |
10 |
Correct |
7 ms |
1884 KB |
Correct |
11 |
Correct |
4 ms |
1620 KB |
Correct |
12 |
Correct |
5 ms |
2396 KB |
Correct |
13 |
Correct |
4 ms |
2396 KB |
Correct |
14 |
Correct |
4 ms |
2396 KB |
Correct |
15 |
Correct |
0 ms |
344 KB |
Correct |
16 |
Correct |
9 ms |
1188 KB |
Correct |
17 |
Correct |
4 ms |
504 KB |
Correct |
18 |
Correct |
6 ms |
1624 KB |
Correct |
19 |
Correct |
3 ms |
1628 KB |
Correct |
20 |
Correct |
5 ms |
2396 KB |
Correct |
21 |
Correct |
5 ms |
1368 KB |
Correct |
22 |
Correct |
8 ms |
1628 KB |
Correct |
23 |
Correct |
8 ms |
1500 KB |
Correct |
24 |
Correct |
7 ms |
1884 KB |
Correct |
25 |
Correct |
4 ms |
1628 KB |
Correct |
26 |
Correct |
5 ms |
2396 KB |
Correct |
27 |
Correct |
4 ms |
2396 KB |
Correct |
28 |
Correct |
5 ms |
2396 KB |
Correct |
29 |
Runtime error |
4 ms |
1628 KB |
Execution killed with signal 6 |
30 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
125 ms |
39500 KB |
Correct |
2 |
Correct |
132 ms |
38724 KB |
Correct |
3 |
Correct |
114 ms |
33788 KB |
Correct |
4 |
Correct |
68 ms |
33320 KB |
Correct |
5 |
Correct |
120 ms |
37988 KB |
Correct |
6 |
Correct |
130 ms |
40672 KB |
Correct |
7 |
Correct |
119 ms |
34036 KB |
Correct |
8 |
Correct |
111 ms |
42020 KB |
Correct |
9 |
Correct |
104 ms |
41640 KB |
Correct |
10 |
Correct |
83 ms |
41032 KB |
Correct |
11 |
Correct |
83 ms |
40760 KB |
Correct |
12 |
Correct |
1 ms |
348 KB |
Correct |
13 |
Correct |
9 ms |
1116 KB |
Correct |
14 |
Correct |
5 ms |
344 KB |
Correct |
15 |
Correct |
7 ms |
1600 KB |
Correct |
16 |
Correct |
5 ms |
1628 KB |
Correct |
17 |
Correct |
5 ms |
2396 KB |
Correct |
18 |
Correct |
5 ms |
1372 KB |
Correct |
19 |
Correct |
8 ms |
1624 KB |
Correct |
20 |
Correct |
8 ms |
1508 KB |
Correct |
21 |
Correct |
7 ms |
1884 KB |
Correct |
22 |
Correct |
4 ms |
1620 KB |
Correct |
23 |
Correct |
5 ms |
2396 KB |
Correct |
24 |
Correct |
4 ms |
2396 KB |
Correct |
25 |
Correct |
4 ms |
2396 KB |
Correct |
26 |
Correct |
129 ms |
39512 KB |
Correct |
27 |
Correct |
129 ms |
38824 KB |
Correct |
28 |
Correct |
101 ms |
33784 KB |
Correct |
29 |
Correct |
67 ms |
33420 KB |
Correct |
30 |
Correct |
105 ms |
37988 KB |
Correct |
31 |
Correct |
126 ms |
40680 KB |
Correct |
32 |
Correct |
127 ms |
34084 KB |
Correct |
33 |
Correct |
111 ms |
41992 KB |
Correct |
34 |
Correct |
105 ms |
41644 KB |
Correct |
35 |
Correct |
82 ms |
41084 KB |
Correct |
36 |
Correct |
83 ms |
40824 KB |
Correct |
37 |
Correct |
0 ms |
348 KB |
Correct |
38 |
Correct |
9 ms |
1180 KB |
Correct |
39 |
Correct |
5 ms |
348 KB |
Correct |
40 |
Correct |
6 ms |
1628 KB |
Correct |
41 |
Correct |
4 ms |
1628 KB |
Correct |
42 |
Correct |
5 ms |
2396 KB |
Correct |
43 |
Correct |
5 ms |
1544 KB |
Correct |
44 |
Correct |
8 ms |
1624 KB |
Correct |
45 |
Correct |
8 ms |
1372 KB |
Correct |
46 |
Correct |
7 ms |
1884 KB |
Correct |
47 |
Correct |
4 ms |
1628 KB |
Correct |
48 |
Correct |
5 ms |
2396 KB |
Correct |
49 |
Correct |
5 ms |
2396 KB |
Correct |
50 |
Correct |
5 ms |
2396 KB |
Correct |
51 |
Correct |
615 ms |
60360 KB |
Correct |
52 |
Correct |
561 ms |
13896 KB |
Correct |
53 |
Correct |
45 ms |
1112 KB |
Correct |
54 |
Correct |
159 ms |
12476 KB |
Correct |
55 |
Correct |
410 ms |
11256 KB |
Correct |
56 |
Correct |
525 ms |
49588 KB |
Correct |
57 |
Correct |
640 ms |
62916 KB |
Correct |
58 |
Correct |
277 ms |
4440 KB |
Correct |
59 |
Correct |
208 ms |
3156 KB |
Correct |
60 |
Correct |
465 ms |
43308 KB |
Correct |
61 |
Correct |
572 ms |
46784 KB |
Correct |
62 |
Correct |
198 ms |
44268 KB |
Correct |
63 |
Correct |
204 ms |
44316 KB |
Correct |
64 |
Correct |
197 ms |
44292 KB |
Correct |
65 |
Correct |
192 ms |
44148 KB |
Correct |
66 |
Correct |
221 ms |
44232 KB |
Correct |
67 |
Correct |
211 ms |
44724 KB |
Correct |
68 |
Correct |
208 ms |
44244 KB |
Correct |
69 |
Correct |
200 ms |
44452 KB |
Correct |
70 |
Correct |
237 ms |
29656 KB |
Correct |
71 |
Correct |
302 ms |
29140 KB |
Correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
125 ms |
39500 KB |
Correct |
2 |
Correct |
132 ms |
38724 KB |
Correct |
3 |
Correct |
114 ms |
33788 KB |
Correct |
4 |
Correct |
68 ms |
33320 KB |
Correct |
5 |
Correct |
120 ms |
37988 KB |
Correct |
6 |
Correct |
130 ms |
40672 KB |
Correct |
7 |
Correct |
119 ms |
34036 KB |
Correct |
8 |
Correct |
111 ms |
42020 KB |
Correct |
9 |
Correct |
104 ms |
41640 KB |
Correct |
10 |
Correct |
83 ms |
41032 KB |
Correct |
11 |
Correct |
83 ms |
40760 KB |
Correct |
12 |
Correct |
142 ms |
39512 KB |
Correct |
13 |
Correct |
125 ms |
38876 KB |
Correct |
14 |
Correct |
101 ms |
33796 KB |
Correct |
15 |
Correct |
71 ms |
33320 KB |
Correct |
16 |
Correct |
108 ms |
38196 KB |
Correct |
17 |
Correct |
139 ms |
40684 KB |
Correct |
18 |
Correct |
129 ms |
34032 KB |
Correct |
19 |
Correct |
107 ms |
41960 KB |
Correct |
20 |
Correct |
143 ms |
41640 KB |
Correct |
21 |
Correct |
91 ms |
41080 KB |
Correct |
22 |
Correct |
95 ms |
40824 KB |
Correct |
23 |
Correct |
154 ms |
42660 KB |
Correct |
24 |
Runtime error |
93 ms |
71264 KB |
Execution killed with signal 6 |
25 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Correct |
2 |
Correct |
0 ms |
348 KB |
Correct |
3 |
Correct |
0 ms |
344 KB |
Correct |
4 |
Runtime error |
0 ms |
604 KB |
Execution killed with signal 6 |
5 |
Halted |
0 ms |
0 KB |
- |