WOJ1032-Find the Max NORM

As you know, matrix theory is widely used in the engineering and other fields. But do you know boolean matrix? It's a most useful matrix
in the computer science. In graph theory, IC design and compiler theory, boolean matrix plays an important role.
One day Little Dragonfly has to do his homework. But one Hard problem puzzles him. Now he wants you to help him to resolve this Hard
problem. The description of the Hard problem:
Note that Boolean matrix is the matrix that each entry in it is 0 or 1. We define the NORM of the boolean matrix as follow:
NORM = the sum of all the entry in the matrix.

And define two manipulations of the matrix:
A(n) : reverse every entry of the nth row;
B(n) : reverse every entry of the nth column;
Given a boolean matrix, you can get some matrices after a series of the two manipulations. Now you have to find the Max NORM of these
matrices.

Examples:
(1) Give a 22 matrix:
0 1
1 0
The Max NORM is 4.
Because we can get a matrix
1 1
1 1
after A(1),B(2). Then the NORM is the largest.
(2) Give another 2
2 matrix
0 1
1 1
The Max NORM is 3.

输入格式

The input contains several cases. In the first line of each case there are two integers:mmm and nnn (1≤m≤101\le m \le 101m10,1≤n≤30001\le n \le 30001n3000). And in the following m lines there are the entries of the m×nm\times nm×n boolean matrix. Each entry is 0 or 1.

Input will be terminated by EOF.

输出格式

For each case, you should print the Max NORM in one line.

样例输入

2 2
0 1
1 0
2 2
0 1
1 1

样例输出

4
3
暴力小的一维的取法 对于每种取法在大的一维取最优值


#include<stdio.h>
int a[11][3001];
int ans,m,n;
int count(){
    int i,j,tp,sum=0;
    for(i=0;i<n;i++){
        tp=0;
        for(j=0;j<m;j++)
        if(a[j][i])
        tp++;
        if(2*tp<m)
        tp=m-tp;
        sum+=tp;
    }
    return sum;
}
void solve(int t){
    int j,tp;
    if(t<m)
    solve(t+1);
    else{
    	tp=count();
    	if(tp>ans)
    	ans=tp;
    	return;
	}
	if(ans==m*n)
	return; 
    for(j=0;j<n;j++)
    a[t][j]=!a[t][j];
    if(t<m)
    solve(t+1);
    else
    return;
    if(ans==m*n)
	return;
    for(j=0;j<n;j++)
    a[t][j]=!a[t][j];
    return;
}
int main(){
	int i,j;
    while(scanf("%d %d",&m,&n)!=EOF){
        for(i=0;i<m;i++)
        for(j=0;j<n;j++)
        scanf("%d",&a[i][j]);            
        ans=0;
        solve(1);
        printf("%d\n",ans);
    }
    return 0;
}                

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值