The North American Invitational Programming Contest 2018 H. Recovery

Consider an n imes mn×m matrix of ones and zeros. For example, this 4 imes 44×4:

displaystyle egin{matrix} 1 & 1 & 1 & 1 \ 0 & 1 & 1 & 1 \ 0 & 1 & 1 & 1 \ 0 & 1 & 1 & 0 end{matrix}1000111111111110

We can compute even parity for each row, and each column. In this case, the row parities are [0, 1, 1, 0][0,1,1,0] and the column parities are [1, 0, 0, 1][1,0,0,1] (the parity is 11 if there is an odd number of 11s in the row or column, 00 if the number of 11s is even). Note that the top row is row 11, the bottom row is row nn, the leftmost column is column 11, and the rightmost column is column mm.

Suppose we lost the original matrix, and only have the row and column parities. Can we recover the original matrix? Unfortunately, we cannot uniquely recover the original matrix, but with some constraints, we can uniquely recover a matrix that fits the bill. Firstly, the recovered matrix must contain as many 11’s as possible. Secondly, of all possible recovered matrices with the most 11’s, use the one which has the smallest binary value when you start with row 11, concatenate row 22 to the end of row 11, then append row 33, row 44, and so on.

Input Format

Each input will consist of a single test case.

Note that your program may be run multiple times on different inputs.

Each test case will consist of exactly two lines.

The first line will contain a string R (1 le |R| le 50)R(1R50), consisting only of the characters 00 and 11. These are the row parities, in order.

The second line will contain a string C (1 le |C| le 50)C(1C50), consisting only of the characters 00 and 11. These are the column parities, in order.

Output Format

If it is possible to recover the original matrix with the given constraints, then output the matrix as |R|R∣ lines of exactly |C|C∣ characters, consisting only of 00’s and 11’s. If it is not possible to recover the original matrix, output -11.

样例输入1

0110
1001

样例输出1

1111
0111
1110
1111

样例输入2

0
1

样例输出2

-1

样例输入3

11
0110

样例输出3

1011
1101

题目来源

The North American Invitational Programming Contest 2018

 

 

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 #include <cstdlib>
 5 #include <cstring>
 6 #include <string>
 7 #include <deque>
 8 using namespace std;
 9 #define  ll long long 
10 #define  N 60
11 #define  gep(i,a,b)  for(int  i=a;i<=b;i++)
12 #define  gepp(i,a,b) for(int  i=a;i>=b;i--)
13 #define  gep1(i,a,b)  for(ll i=a;i<=b;i++)
14 #define  gepp1(i,a,b) for(ll i=a;i>=b;i--)    
15 #define  mem(a,b)  memset(a,b,sizeof(a))
16 char s1[N],s2[N];
17 int a[N],b[N];
18 char s[N][N];
19 int main()
20 {
21    scanf("%s%s",s1,s2);
22    int l=strlen(s1);
23    int r=strlen(s2);
24    int x=l%2,y=r%2;
25    int cnt1=0,cnt2=0;
26    gep(i,0,l-1){
27        int ii=s1[i]-'0';
28        if(ii%2!=y){
29            a[cnt1++]=i;
30        }
31    }
32    gep(i,0,r-1){
33        int ii=s2[i]-'0';
34        if(ii%2!=x){
35            b[cnt2++]=i;
36        }
37    }
38    if((cnt1+cnt2)&1){//必须为偶数
39        printf("-1
");
40        return 0;
41    }
42    while(cnt1<cnt2)  a[cnt1++]=0;
43    while(cnt2<cnt1)  b[cnt2++]=0;
44    sort(a,a+cnt1);sort(b,b+cnt2);//贪心
45    gep(i,0,l-1){
46        gep(j,0,r-1){
47            s[i][j]='1';
48        }
49    }
50    gep(i,0,cnt1-1){
51        s[a[i]][b[i]]='0';//这些点必须为0
52    }
53    gep(i,0,l-1){
54        printf("%s
",s[i]);
55    }
56    return  0;
57 }
原文地址:https://www.cnblogs.com/tingtin/p/9487642.html