CF 425B Sereja and Table
来源:互联网 发布:人工智能处理器 编辑:程序博客网 时间:2024/06/09 21:52
题意:给出一个n×m的01矩阵,现在可以进行最多k次操作,每次可以选任意一个元素变成0或者变成1,问将矩阵每个连通块都变成一个矩阵的最小操作次数。
思路:感觉又一次被智商碾压,智商亟待提高……首先考虑n>k的情况,那么最多能修改k行,那么肯定有至少1行不会被修改,枚举每一行,注意到如果某一行确定了,那么相邻行要么和下面的元素全相等,要么不等,计算一下需要操作的最小代价。至于n<=k的情况,由于k比较小,状态压缩枚举一下某一列的状态就行了。
代码:
#include<iostream>#include<cstdio>#include<cstring>#include<string>#include<algorithm>#include<map>#include<queue>#include<stack>#include<cmath>#include<vector>#define inf 0x3f3f3f3f#define Inf 0x3FFFFFFFFFFFFFFFLL#define eps 1e-9#define pi acos(-1.0)using namespace std;typedef long long ll;const int maxn=100+10;int mat[maxn][maxn];int main(){ //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); int n,m,k; scanf("%d%d%d",&n,&m,&k); for(int i=0;i<n;++i) for(int j=0;j<m;++j) scanf("%d",&mat[i][j]); int ans=inf,sum,tmp; if(n>k) { for(int x=0;x<n;++x) { sum=0; for(int i=0;i<n;++i) { tmp=0; for(int j=0;j<m;++j) tmp+=mat[i][j]^mat[x][j]; sum+=min(tmp,m-tmp); } ans=min(ans,sum); } } else { for(int x=0;x<(1<<n);++x) { sum=0; for(int j=0;j<m;++j) { tmp=0; for(int i=0;i<n;++i) tmp+=mat[i][j]^((x>>i)&1); sum+=min(tmp,n-tmp); } ans=min(ans,sum); } } if(ans>k) ans=-1; printf("%d\n",ans); return 0;}
0 0
- CF 425B Sereja and Table
- CodeForces 425B Sereja and Table
- CodeForces 425B Sereja and Table(枚举)
- (CF)B. Sereja and Contests
- CF 314B: Sereja and Periods
- CF#215DIV2:B. Sereja and Suffixes
- CF 401B Sereja and Contests
- CF#215 DIV2: B. Sereja and Suffixes
- CF-Sereja and Algorithm
- CF 315B(Sereja and Array-峰顶距离统计)
- CF 315B - Sereja and Array(树状数组)
- CF:Problem 426B - Sereja and Mirroring 二分或者分治
- Codeforces Round #243 (Div. 1) B Sereja and Table
- CF Sereja and Periods1 【暴力】
- B. Sereja and Array
- B. Sereja and Suffixes
- B. Sereja and Stairs
- B. Sereja and Contests
- 在Oracle的FORM中高亮显示鼠标点击或光标所在的行
- Cocos2dx 3.0 过渡篇(十六)用ClippingNode做游戏的新手引导
- 写一个自己的Dictionary
- Python urllib模块与urlopen()函数解析
- 棋牌运营的微信营销你为什么做不成功
- CF 425B Sereja and Table
- 利用vertical-align:middle实现垂直居中对齐
- Memcached常用命令及使用说明
- mybatis There is no getter for property named 'xx' in 'class java.lang.String
- 聊点小小的面试
- List remove时出错"ConcurrentModificationException"
- Hibernate完成CRUD
- SSH整合初步---设计及各层的创建顺序
- android设计模式之控制反转(IOC)原则