点击打开题目链接 

题意:给定一个n×m的0,1矩阵,做多可以对矩阵做k次变换,每次变换只可以将矩阵的某一个元素由0变成1,或从1变成0。

求最小的变换次数使得得到的矩阵满足:每一个连通块都是一个“实心”矩形。

要想清楚的就是最终的矩阵一定是这样的: 

010...

1 0 1 ...

0 1 0 ... 这样的0,1交替的矩阵,这里0,1分别表示一个矩形。

如果 n > k, 那么肯定又一行是没有改变的(为什么?想一想),那么只需要枚举这没有改变的一行,其他行要么和他完全相同,要么完全不同(为什么?想一想)。并且任何两行之间没有任何的其他依赖关系,所以只需要取变换次数最少的一种。

如果 n< k,  那么可以枚举第一列最后的情况,也就是2^ n,这里n< k, 而且k<10。 然后和上面一样就可以了。

附上代码:

 /*************************************************************************
> File Name: 425B.cpp
> Author: Stomach_ache
> Mail: sudaweitong@gmail.com
> Created Time: 2014年04月30日 星期三 21时44分14秒
> Propose:
************************************************************************/ #include <cmath>
#include <string>
#include <cstdio>
#include <fstream>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; #define MAX_N (100 + 5)
#define min(x, y) ((x) < (y) ? (x) : (y)) int n, m, k;
int a[MAX_N][MAX_N]; int
main(void) {
scanf("%d %d %d", &n, &m, &k);
for (int i = ; i < n; i++)
for (int j = ; j < m; j++)
scanf("%d", &a[i][j]);
int ans = ;
if (k < n) {
for (int i = ; i < n; i++) {
int tmp = ;
for (int j = ; j < n; j++) if (i != j) {
int cnt1 = , cnt2 = ;
for (int t = ; t < m; t++) {
if (a[j][t] == a[i][t]) {
cnt1++;
} else {
cnt2++;
}
}
tmp += min(cnt1, cnt2);
}
if (tmp < ans) {
ans = tmp;
}
}
} else {
for (int i = ; i < (<<n); i++) {
int tmp = , b[MAX_N];
for (int j = ; j < n; j++) {
if (i & (<<j)) {
if (a[j][] == ) {
b[j] = ;
tmp++;
} else {
b[j] = ;
}
} else {
if (a[j][] == ) {
b[j] = ;
tmp++;
} else {
b[j] = ;
}
}
}
for (int j = ; j < m; j++) {
int cnt1 = , cnt2 = ;
for (int t = ; t < n; t++) {
if (a[t][j] == b[t]) {
cnt1++;
} else {
cnt2++;
}
}
tmp += min(cnt1, cnt2);
}
if (tmp < ans) {
ans = tmp;
}
}
}
if (ans > k) {
puts("-1");
} else {
printf("%d\n", ans);
} return ;
}

Codeforces 425B的更多相关文章

  1. codeforces 425B Sereja and Table (枚举、位图)

    输入n*m的01矩阵.以及k. n,m<=100,k<=10 问修改至多k个,使得矩阵内的各连通块(连着的0或1构成连通块)都是矩形,且不含另外的数字(边界为0(1)的矩形内不含1(0)) ...

  2. codeforces 425B Sereja and Table(状态压缩,也可以数组模拟)

    题目 给出一个n*m的01矩阵, 让你最多改变k个里面的值(0变1,1变0), 使得0.1的连通分量是矩阵.输出最少步数 1 ≤ n, m ≤ 100; 1 ≤ k ≤ 10 题解: 如果01连通分量 ...

  3. Sereja and Table CodeForces - 425B (暴力,状压)

    大意: 给定01矩阵, 求翻转尽量少的数字, 使得所有0或1的连通块为矩形, 若至少要翻转超过k次, 输出-1

  4. python爬虫学习(5) —— 扒一下codeforces题面

    上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...

  5. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

  6. 【Codeforces 738C】Road to Cinema

    http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...

  7. 【Codeforces 738A】Interview with Oleg

    http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...

  8. CodeForces - 662A Gambling Nim

    http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...

  9. CodeForces - 274B Zero Tree

    http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...

随机推荐

  1. VS2010中Cocos2d-x中文乱码问题

    不罗嗦,直接进入主题,VS2010的默认编码是"GB2312",那么以创建一个label为例,当我们使用 CCLabelTTF::create(const char *label, ...

  2. ubuntu下编译安装poco

    系统环境: ubuntu版本:Linux jfcai-VirtualBox 4.15.0-29-generic #31-Ubuntu SMP Tue Jul 17 15:39:52 UTC 2018 ...

  3. spring源码学习之容器的扩展(一)

    在前面的章节,我们一直以BeanFactory接口以及它的默认实现XmlBeanFactory为例进行解析,但是,spring还提供了另一个接口ApplicationContext,用于扩展BeanF ...

  4. MySQL系列(八)--数据库分库分表

    在互联网公司或者一些并发量比较大的项目,虽然有各种项目架构设计.NoSQL.MQ.ES等解决比较高的并发访问,但是对于数据库来说,压力 还是太大,这时候即使数据库架构.表结构.索引等都设计的很好了,但 ...

  5. HttpRequest 工具

    1.根据 url 和 encoding 获取当前url页面的 html 源代码 public static string GetHtml(string url, Encoding encoding) ...

  6. HDU 3714

    最大值最小问题,三分....竟然排第六当时..... #include<stdio.h> #include<string.h> #define max 10000+10 #de ...

  7. Python当前进程信息 (os包)

    Python当前进程信息 (os包) 我们在Linux的概念与体系,多次提及进程的重要性.Python的os包中有查询和修改进程信息的函数.学习Python的这些工具也有助于理解Linux体系. (o ...

  8. Apache HttpComponents 工具类 [ HttpUtil ]

    pom.xml <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId&g ...

  9. ubuntu和win10设置双显示器

    ubuntu:最右上角那个图标,点开找到系统设置,系统设置中找到“显示”中,在其中可以调节双屏显示或者只显示一个屏,图等会补... win10:现在是ubuntu系统所以操作忘记了写不出来,等下换系统 ...

  10. 模拟21 题解(waiting)

    留坑待填 效率!!! 题还没改Oh,NO!!!