点击打开题目链接 

题意:给定一个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. PAT甲级——A1018 Public Bike Management

    There is a public bike service in Hangzhou City which provides great convenience to the tourists fro ...

  2. LeetCode简单算法之分割平衡字符串 #1221

    在一个「平衡字符串」中,'L' 和 'R' 字符的数量是相同的. 给出一个平衡字符串 s,请你将它分割成尽可能多的平衡字符串. 返回可以通过分割得到的平衡字符串的最大数量. 示例 1: 输入:s = ...

  3. 数据库访问技术 odbc dao rdo uda jet oledb

    一.UDA(UniversalDataAccess) 这是微软提供的通用数据访问策略.包括ADO.OLEDB和ODBC.它不光提供了数据库的访 问能力,对于其它的数据存储技术也同样支持,如目录服务.E ...

  4. 【python之路25】正则表达式

    一.正则表达式简介 就其本质而言,正则表达式(或RE)是一种小型的.高度专业化的(在python中),它内嵌在python中,并通过RE模块实现.正则表达式编译成一系列字节码,然后由用C编写的匹配引擎 ...

  5. Django项目:CRM(客户关系管理系统)--17--09PerfectCRM实现King_admin显示注册表的内容

    {#table_data_list.html#} {## ————————08PerfectCRM实现King_admin显示注册表的字段表头————————#} {% extends 'king_m ...

  6. 不同尺寸设计图 rem 断点数据记录

    320px宽的设计图 @media screen and (min-width: 320px) { html { font-size: 100px; } } @media screen and (mi ...

  7. Hackerrank--String Function Calculation(后缀数组)

    题目链接 Jane loves string more than anything. She made a function related to the string some days ago a ...

  8. mybatis深入理解(三)-----MyBatis事务管理机制

    MyBatis作为Java语言的数据库框架,对数据库的事务管理是其非常重要的一个方面.本文将讲述MyBatis的事务管理的实现机制.首先介绍MyBatis的事务Transaction的接口设计以及其不 ...

  9. 计算机组成原理(电脑硬件&语言分类)

    计算机组成原理 一.电脑硬件配置 CPU :中央处理器(人类的大脑) -飞机 内存:存放一些临时数据(人类的短暂记忆-右脑) -高铁 硬盘:存储永久数据(左脑-长期记忆) - 汽车 输入输出:键盘鼠标 ...

  10. 2018-2-13-wpf-PreviewTextInput-在鼠标输入获得-_u0003

    title author date CreateTime categories wpf PreviewTextInput 在鼠标输入获得 � lindexi 2018-2-13 17:23:3 +08 ...