WOJ1032-Find the Max NORM

来源:互联网 发布:数据标准化是什么意思 编辑:程序博客网 时间:2024/05/24 07:43

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 20 11 02 20 11 1

样例输出

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


#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;}                

原创粉丝点击