点击打开题目链接 

题意:给定一个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. RabbitMq知识点总结

    一.RabbitMQ简介 AMQP,即Advanced Message Queuing Protocol,一个提供统一消息服务的应用层标准高级消息队列协议,是应用层协议的一个开放标准,为面向消息的中间 ...

  2. Spinrg WebFlux中Cookie的读写

    WebFLux与WebMvc的差异 WebFlux读写Cookie不像WebMvc那么直接,最主要的原因是WebMvc是基于Servlet规范的,而WebFlux仅仅遵守的是HTTP协议.所以在使用的 ...

  3. 解决hive无法传参问题思路

    由于hive执行过程中参数必须写死,无法进行传递参数.利用shell脚本和java编程进行解决 #!/bin/sh#date = `date -d -1days '+%Y-%m-%d'`if [ $# ...

  4. Leetcode448.Find All Numbers Disappeared in an Array找到所有数组中消失的数字

    给定一个范围在  1 ≤ a[i] ≤ n ( n = 数组大小 ) 的 整型数组,数组中的元素一些出现了两次,另一些只出现一次. 找到所有在 [1, n] 范围之间没有出现在数组中的数字. 您能在不 ...

  5. python 中的split()函数和os.path.split()函数

    Python中有split()和os.path.split()两个函数: split():拆分字符串.通过指定分隔符对字符串进行切片,并返回分割后的字符串列表. os.path.split():将文件 ...

  6. linux 下环境变量设置

    Ubuntu Linux系统包含两类环境变量:系统环境变量和用户环境变量.系统环境变量对所有系统用户都有效,用户环境变量仅仅对当前的用户有效. 修改用户环境变量 用户环境变量通常被存储在下面的文件中: ...

  7. fastjson string[]转 json字符串

    @RequestMapping(value = "/friendlink_list/province_list", produces = {"application/js ...

  8. FATAL ERROR in native method: JDWP No transports initialized, jvmtiError=AGENT_ERROR_TRANSPORT_INIT(197)

    自从继承了hibernate ,全都是些奇葩问题. 努力解决中,先发布,以备忘

  9. Java 类与类之间的调用

    方法1. 新建一个类. 然后在调用类中先进行被调用类实例化,然后通过实例化的对象访问. 例如: //先定义一个类 import static java.lang.System.out; public ...

  10. spring JdbcTemplate最基本的使用

    package com.com.jdbctemplate; import org.springframework.jdbc.core.JdbcTemplate; import org.springfr ...