CodeForces 425B Sereja and Table(枚举)
来源:互联网 发布:dhcp服务端口 编辑:程序博客网 时间:2024/06/07 08:37
满足条件的矩阵有一个性质,就是相邻的两行(列)要么完全相同,要么完全相反,所以就枚举每行做为模式串,去把其他行都改为和模式串相同或完全相反的。
注意最优的模式串可能是行的也可能是列的,所以要对行列都做。
而且还有一种数据 比如:
0 0 0 1
0 0 1 0
0 1 0 0
1 0 0 0
也就是说模式串可能既不是某行也不是某列,而是全0或者全1。所以最后再判断一下这两种。
代码:
#include <iostream>#include <cstdio>#include <cstring>using namespace std;#define LL long long#include <algorithm>int a[105][105];int main(){int N,M,K;scanf("%d%d%d",&N,&M,&K);for(int i=1;i<=N;i++){for(int j=1;j<=M;j++){scanf("%d",&a[i][j]);}}int tmp[105];int res=10000000;for(int i=1;i<=N;i++){for(int j=1;j<=M;j++){tmp[j]=a[i][j];}int num=0;for(int j=1;j<=N;j++){int cur=0;for(int k=1;k<=M;k++){if(a[j][k]!=tmp[k]) cur++;}num+=min(cur,M-cur);}res=min(res,num);}for(int i=1;i<=M;i++){for(int j=1;j<=N;j++){tmp[j]=a[j][i];}int num=0;for(int j=1;j<=M;j++){int cur=0;for(int k=1;k<=N;k++){if(a[k][j]!=tmp[k]) cur++;}num+=min(cur,N-cur);}res=min(res,num);}int num=0;for(int i=1;i<=N;i++){int cur=0;for(int j=1;j<=M;j++){if(a[i][j]) cur++;}num+=min(cur,M-cur);}res=min(num,res);num=0;for(int i=1;i<=N;i++){int cur=0;for(int j=1;j<=M;j++){if(!a[i][j]) cur++;}num+=min(cur,M-cur);}res=min(num,res);if(res>K) printf("-1");else printf("%d",res);return 0;}/*3 10 70 1 0 0 1 0 1 0 0 00 0 1 1 0 0 0 1 0 11 0 1 1 1 0 1 1 0 0*/
0 0
- CodeForces 425B Sereja and Table(枚举)
- CodeForces 425B Sereja and Table
- CF 425B Sereja and Table
- codeforces 380B Sereja and Tree 暴力枚举
- Codeforces 426 B Sereja and Mirroring【暴力枚举+模拟】水题
- Codeforces Round #243 (Div. 1) B Sereja and Table
- codeforces B. Sereja and Contests
- Codeforces 425A Sereja and Swaps(暴力枚举)
- Codeforces 426D Sereja and Table【思维+暴力枚举】好题!
- codeforces 315 B.Sereja and Array
- CodeForces 315B Sereja and Array
- codeforces 401B - Sereja and Contests
- codeforces 426B - Sereja and Mirroring
- CodeForces 368B Sereja and Suffixes
- Codeforces 401B Sereja and Contests(DP)
- CodeForces 401B Sereja and Contests
- codeforces 426 B. Sereja and Mirroring(递归)
- CodeForces 368B-Sereja and Suffixes【模拟】
- 多态
- com.google.gson.JsonSyntaxException:java.lang.IllegalStateException异常
- 单链表,建立,完成固定数组的排序
- c# 用户登录框 限制
- 【自考】管理经济学之宏观认识
- CodeForces 425B Sereja and Table(枚举)
- 【JSP开发】一个防盗链的WEB小例子
- iscroll5的demo,pull to refresh ,pull to load(下拉刷新,上拉加载)
- Android Toast源码分析
- POJ 1146 ID Codes (生成排列)
- android的自定义dialog对话框实现
- 玩转Bash脚本:循环结构之while循环
- Linux内核--网络栈实现分析(二)--数据包的传递过程(上)
- Android L Phone的目录结构与进程模型