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;
}                
全部评论

相关推荐

这算盘打的
程序员小白条:都这样的,都是潜规则,你自己说可以实习一年就行了,实习可以随便跑路的
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-10 11:45
你不要过来啊啊啊啊啊啊啊
码农索隆:对面:“今天你不面也得面”
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务