G. Inverse of Rows and Columns
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
You are given a binary matrix aa of size n×mn×m. A binary matrix is a matrix where each element is either 00 or 11.
You may perform some (possibly zero) operations with this matrix. During each operation you can inverse the row of this matrix or a column of this matrix. Formally, inverting a row is changing all values in this row to the opposite (00 to 11, 11 to 00). Inverting a column is changing all values in this column to the opposite.
Your task is to sort the initial matrix by some sequence of such operations. The matrix is considered sorted if the array [a1,1,a1,2,…,a1,m,a2,1,a2,2,…,a2,m,…,an,m−1,an,m][a1,1,a1,2,…,a1,m,a2,1,a2,2,…,a2,m,…,an,m−1,an,m] is sorted in non-descending order.
Input
The first line of the input contains two integers nn and mm (1≤n,m≤2001≤n,m≤200) — the number of rows and the number of columns in the matrix.
The next nn lines contain mm integers each. The jj-th element in the ii-th line is ai,jai,j (0≤ai,j≤10≤ai,j≤1) — the element of aa at position (i,j)(i,j).
Output
If it is impossible to obtain a sorted matrix, print "NO" in the first line.
Otherwise print "YES" in the first line. In the second line print a string rr of length nn. The ii-th character riri of this string should be '1' if the ii-th row of the matrix is inverted and '0' otherwise. In the third line print a string cc of length mm. The jj-th character cjcj of this string should be '1' if the jj-th column of the matrix is inverted and '0' otherwise. If there are multiple answers, you can print any.
Examples
input
Copy
2 2 1 1 0 1
output
Copy
YES 00 10
input
Copy
3 4 0 0 0 1 0 0 0 0 1 1 1 1
output
Copy
YES 010 0000
input
Copy
3 3 0 0 0 1 0 1 1 1 0
output
Copy
NO
分析:如果这个矩阵是单调的,那么第一行也是单调的,情况只有M+1种,在第一行确定后,c[i]的值也就确定了。
再考虑r[1],r[1]的取值是无所谓的,因为只需要将所有的c[i]和除r[1]之外的r[i]翻转一次,得到的结果是相同的,所以r[1]默认为0。
如果第一行的末尾是1的话,那么接下来所有的也是1,这样r[i]也确定了,n^2check一下就可以。
如果末尾是0的话,我们只需要保证尽可能的让1的出现靠后,也就是说如果1还没有出现,默认当前一行的首个元素为0,这样就可以得到r[i]的值。
总复杂度
然后因为cf的数据有丶水,直接1e9暴力也可以过。。。
#include "bits/stdc++.h"
using namespace std;
int n, m;
int a[204][204];
int r[204], c[204];
bool che(int y) {
int b[204] = {0};
for (int i = 1; i <= m; ++i) {
if (i <= y)continue;
else b[i] = 1;
}
for (int i = 1; i <= m; ++i) {
c[i] = a[1][i] ^ b[i];
}
if (b[m] == 1) {
for (int i = 2; i <= n; ++i) {
r[i] = 1 ^ a[i][1] ^ c[1];
}
for (int i = 2; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
if (!(a[i][j] ^ r[i] ^ c[j]))return 0;
}
}
return 1;
} else {
bool ok = 0;
r[1] = 0;
for (int i = 2; i <= n; ++i) {
r[i] = a[i][1] ^ c[1];
if (ok)r[i] ^= 1;
for (int j = 1; j <= m; ++j) {
if (!(a[i][j] ^ r[i] ^ c[j]) && ok)return 0;
else if ((a[i][j] ^ r[i] ^ c[j]))ok = 1;
}
}
}
return 1;
}
int main() {
cin >> n >> m;
int T = clock();
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
scanf("%d", &a[i][j]);
}
}
for (int j = 0; j <= m; ++j) {
if (che(j)) {
puts("YES");
for (int k = 1; k <= n; ++k) {
printf("%d", r[k]);
}
puts("");
for (int k = 1; k <= m; ++k) {
printf("%d", c[k]);
}
puts("");
return 0;
}
}
puts("NO");
}