During the break, we decided to relax and play dominoes. Our box with Domino was empty, so we decided to borrow the teacher's dominoes.
The teacher responded instantly at our request. He put nm dominoes on the table as an n × 2m rectangle so that each of the n rows contained m dominoes arranged horizontally. Each half of each domino contained number (0 or 1).
We were taken aback, and the teacher smiled and said: "Consider some arrangement of dominoes in an n × 2m matrix. Let's count for each column of the matrix the sum of numbers in this column. Then among all such sums find the maximum one. Can you rearrange the dominoes in the matrix in such a way that the maximum sum will be minimum possible? Note that it is prohibited to change the orientation of the dominoes, they all need to stay horizontal, nevertheless dominoes are allowed to rotate by 180 degrees. As a reward I will give you all my dominoes".
We got even more taken aback. And while we are wondering what was going on, help us make an optimal matrix of dominoes.
The first line contains integers n, m (1 ≤ n, m ≤ 103).
In the next lines there is a description of the teachers' matrix. Each of next n lines contains m dominoes. The description of one domino is two integers (0 or 1), written without a space — the digits on the left and right half of the domino.
Print the resulting matrix of dominoes in the format: n lines, each of them contains m space-separated dominoes.
If there are multiple optimal solutions, print any of them.
2 3 01 11 00 00 01 11
11 11 10 00 00 01
4 1 11 10 01 00
11 10 01 00
Consider the answer for the first sample. There, the maximum sum among all columns equals 1 (the number of columns is 6, and not 3). Obviously, this maximum can't be less than 1, then such matrix is optimal.
Note that the dominoes can be rotated by 180 degrees.
优先放1多的,从左到右,从右到左
#include <string.h>
#include <stdio.h>
#include <algorithm>
#include <vector>
using namespace std;
struct node{
char s[3];
int a;
};
bool cmp(node i,node j){
return i.a>j.a;
}
int n,m;
char s[1001][1001][3];
vector<node>in;
int main(){
while(~scanf("%d%d",&n,&m)){
in.clear();
int sum0=0,sum1=0,sum2=0,sum=0;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
node v;
scanf("%s",s[i][j]);
strcpy(v.s,s[i][j]);
int a=0;
if(v.s[0]=='1') a++;
if(v.s[1]=='1') a++;
sum+=a;
if(a==0) sum0++;
else if(a==1) sum1++;
else sum2++;
v.a=a;
in.push_back(v);
}
}
sort(in.begin(),in.end(),cmp);
int x=0;
int p=0;
for(int i=0;i<n;i++){
if(p==0){
for(int j=0;j<m;j++){
node v=in[x++];
// printf("%s %d\n",v.s,v.a);
if(v.a==2){
s[i][j][0]=s[i][j][1]='1';
}
else if(v.a==1){
if(s[i-1][j][0]=='0'){
s[i][j][0]='1',s[i][j][1]='0';
}
else s[i][j][0]='0',s[i][j][1]='1';
}
else{
s[i][j][0]=s[i][j][1]='0';
}
}
p=1;
}
else{
for(int j=m-1;j>=0;j--){
node v=in[x++];
//printf("%s %d\n",v.s,v.a);
if(v.a==2){
s[i][j][0]=s[i][j][1]='1';
}
else if(v.a==1){
if(s[i-1][j][0]=='0'){
s[i][j][0]='1',s[i][j][1]='0';
}
else s[i][j][0]='0',s[i][j][1]='1';
}
else{
s[i][j][0]=s[i][j][1]='0';
}
}
p=0;
}
}
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
printf("%s",s[i][j]);
if(j!=m-1){printf(" ");}
else printf("\n");
}
}
}
}