The North American Invitational Programming Contest 2018 H. Recovery

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

\displaystyle \begin{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\n");
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\n",s[i]);
55    }
56    return  0;
57 }
复制代码

 

posted on   cltt  阅读(334)  评论(0编辑  收藏  举报

编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示