http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=50785
翻译:http://blog.csdn.net/acm_10000h/article/details/40988419
1 #include <iostream> 2 #include <cstdio> 3 #include <cmath> 4 #include <vector> 5 #include <cstring> 6 #include <string> 7 #include <algorithm> 8 #include <string> 9 #include <set> 10 #include <functional> 11 #include <numeric> 12 #include <sstream> 13 #include <stack> 14 #include <map> 15 #include <queue> 16 17 #define CL(arr, val) memset(arr, val, sizeof(arr)) 18 19 #define ll long long 20 #define inf 0x7f7f7f7f 21 #define lc l,m,rt<<1 22 #define rc m + 1,r,rt<<1|1 23 #define pi acos(-1.0) 24 25 #define L(x) (x) << 1 26 #define R(x) (x) << 1 | 1 27 #define MID(l, r) (l + r) >> 1 28 #define Min(x, y) (x) < (y) ? (x) : (y) 29 #define Max(x, y) (x) < (y) ? (y) : (x) 30 #define E(x) (1 << (x)) 31 #define iabs(x) (x) < 0 ? -(x) : (x) 32 #define OUT(x) printf("%I64d ", x) 33 #define lowbit(x) (x)&(-x) 34 #define Read() freopen("a.txt", "r", stdin) 35 #define Write() freopen("b.txt", "w", stdout); 36 #define maxn 1000000000 37 #define N 1010 38 using namespace std; 39 int a[15][10010]; 40 bool vis[15]; 41 int n,m,ans; 42 43 void dfs(int k) 44 { 45 if(k==n) 46 { 47 int top=0; 48 for(int j=0;j<m;j++) 49 { 50 int cnt=0; 51 for(int i=0;i<n;i++) 52 { 53 if(!vis[i]&&a[i][j]||vis[i]&&!a[i][j]) //注意这里,前面和后面只会有一种满足条件,,所以其实统计的只有一种情况。 54 cnt++; 55 } 56 //printf("%d ",cnt); 57 top+=max(cnt,n-cnt); 58 } 59 ans=max(top,ans); 60 //printf("%d %d ",top,ans); 61 return; 62 } 63 vis[k]=!vis[k]; 64 dfs(k+1); 65 vis[k]=!vis[k]; 66 dfs(k+1); 67 } 68 int main() 69 { 70 //Read(); 71 //Write() 72 while(~scanf("%d%d",&n,&m)&&(n+m)) 73 { 74 for(int i=0;i<n;i++) 75 for(int j=0;j<m;j++) scanf("%d",&a[i][j]); 76 ans=0; 77 CL(vis,false); 78 dfs(0); 79 printf("%d ",ans); 80 } 81 return 0; 82 }