@UPC 7625@North American Invitational Programming Contest 2018 H: Recovery(贪心)

版权声明:岂曰无衣,与子同袍 https://blog.csdn.net/sizaif/article/details/82083360

时间限制: 1 Sec  内存限制: 128 MB
提交: 203  解决: 35
[提交] [状态] [讨论版] [命题人:admin]

题目描述

Consider an n × m matrix of ones and zeros. For example, this 4 × 4:
1 1 1 1
0 1 1 1
0 1 1 1
0 1 1 0
We can compute even parity for each row, and each column. In this case, the row parities are [0, 1, 1, 0] and the column parities are [1, 0, 0, 1] (the parity is 1 if there is an odd number of 1s in the row or column, 0 if the number of 1s is even). Note that the top row is row 1, the bottom row is row n, the leftmost column is column 1, and the rightmost column is column m.
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 1’s as possible. Secondly, of all possible recovered matrices with the most 1’s, use the one which has the smallest binary value when you start with row 1, concatenate row 2 to the end of row 1, then append row 3, row 4, and so on.

输入

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 ≤ |R| ≤ 50), consisting only of the characters 0 and 1. These are the row parities, in order.
The second line will contain a string C (1 ≤ |C| ≤ 50), consisting only of the characters 0 and 1. 
These are the column parities, in order.

输出

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

样例输入

0110
1001

样例输出

1111
0111
1110
1111

[题意]

根据奇偶校验和 构造01矩阵,使得满足题意得情况下,保证1得个数尽量多得情况下,每一行串起来得二进制尽量小.

也就是说0尽量往第一行填.

0代表 1得个数为偶数,1 代表1得个数为奇数.

先输入行,在输入列.

[思路]

贪心,, 行 与  m 有关,  列与 n有关.  所以只需要修改 , 不匹配得 行和列就可以了.

例如  行 为n  列为m ,  m为奇数, 那么  就需要修改行中 为0得 ,  

[代码]

#include <bits/stdc++.h>
#include <stdio.h>
#define rep(i,a,n) for(int i=a;i<=n;i++)
typedef long long ll;
const int maxn = 1e5+10;

using namespace std;

int mp[100][100];
int a[100],b[100];
int main(int argc, char const *argv[])
{
	char s1[100],s2[100];
	scanf("%s%s",s1+1,s2+1);
	int n = strlen(s1+1);
	int m = strlen(s2+1);
	rep(i,1,n)
	{
		rep(j,1,m)
			mp[i][j] =1;
	}
	int c1=0,c2=0;
	int y =(m%2);
	int x =(n%2);
	rep(i,1,n)
	{
		int te = s1[i]-'0';
		 if(te!=y)
		 	a[++c1] = i;
	}
	rep(i,1,m)
	{
		int te = s2[i]-'0';
		 if(te!=x)
		 	b[++c2] = i;
	}
	if( (c1+c2 )&1) // 当为奇数时,时构造不出来的
	{
		printf("-1\n");
		return 0;
	}
	while(c1<c2)
		a[++c1] = 1;
	while(c2<c1)
		b[++c2] = 1;
	sort(a+1,a+c1+1);
	sort(b+1,b+c2+1);
	rep(i,1,max(c1,c2))
	{
		//cout<<a[i]<<" "<<b[i]<<endl;
		mp[a[i]][b[i]] = 0;
	}
	rep(i,1,n)
	{
		rep(j,1,m)
		{
			printf("%d", mp[i][j]);
		}
		printf("\n");
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/sizaif/article/details/82083360
今日推荐