看这个问题之前,能够先看看这个论文《一类算法复合的方法》,说白了就是分类讨论,可是这个思想非常重要

题目链接

  • 题意:

    首先给出联通块的定义:对于相邻(上下和左右)的同样的数字视为一个联通块

    现给一个n*m的仅仅有0和1的矩形和数字k,求出最小反转个数使得总体包含若干个矩形联通块(即每一个联通块均是矩形)(1 ≤ n, m ≤ 100; 1 ≤ k ≤ 10)

    假设最小次数比k大,输出-1
  • 分析:

    题目的特点是k比較小。也就是说反转的次数比較少,所以能够从这里入手。直接枚举全部的位置肯定是不行了,那么能够这样考虑:(最好还是设n>=m)假设n比k大,那么肯定有一些行是不会有反转的数字的,那么我们能够枚举每一行来处理;假设k比n大,这个时候n小于10,所以这时候我们就能够暴力枚举每一行的全部状态。然后处理。

    以上两种方法处理的时候均根据下边的图形特点,仅仅知道一行的时候就能够求出最小的总反转数

终于仅仅能是

01010...

10101...

...

的形状(当中一个字符代表一个矩形)

const int MAXN = 110;

int ipt[MAXN][MAXN];

int main()
{
// freopen("in.txt", "r", stdin);
int n, m, k;
while (~RIII(n, m, k))
{
REP(i, n) REP(j, m) RI(ipt[i][j]);
if (n < m)
{
REP(i, n) FF(j, i + 1, m) swap(ipt[i][j], ipt[j][i]);
swap(n, m);
}
if (n > k)
{
int ans = INF;
REP(i, n)
{
int tans = 0;
REP(j, n)
{
int cnt = 0;
if (i == j) continue;
REP(k, m)
{
if (ipt[i][k] != ipt[j][k]) cnt++;
}
tans += min(cnt, m - cnt);
}
ans = min(ans, tans);
}
printf("%d\n", ans <= k ? ans: -1);
}
else
{
int ans = INF;
REP(i, n)
{
int all = 1 << m;
for (int q = 0; q < all; q++)
{
int diff = 0;
for (int t = 0, l = 1; t < m; l <<= 1, t++) if (((q & l) != 0) != ipt[i][t]) diff++;
if (diff > k) continue;
int tans = 0;
REP(j, n)
{
if (i == j) continue;
int cnt = 0;
for (int t = 0, l = 1; t < m; t++, l <<= 1) if (((q & l) != 0) != ipt[j][t]) cnt++;
tans += min(cnt, m - cnt);
}
ans = min(ans, diff + tans);
}
}
printf("%d\n", ans <= k ? ans: -1);
}
}
return 0;
}

參照大神的代码后的一些细节改动:

const int MAXN = 110;

int ipt[MAXN][MAXN];

int main()
{
// freopen("in.txt", "r", stdin);
int n, m, k;
while (~RIII(n, m, k))
{
int ans = INF, all = 1 << m;
REP(i, n) REP(j, m) RI(ipt[i][j]);
if (n < m)
{
REP(i, n) FF(j, i + 1, m) swap(ipt[i][j], ipt[j][i]);
swap(n, m);
}
if (n > k)
{
REP(i, n)
{
int tans = 0;
REP(j, n)
{
int cnt = 0;
REP(k, m)
cnt += ipt[i][k] ^ ipt[j][k];
tans += min(cnt, m - cnt);
}
ans = min(ans, tans);
}
}
else
{
for (int mask = 0; mask < all; mask++)
{
int tans = 0;
REP(i, n)
{
int cnt = 0;
REP(j, m) cnt += ipt[i][j] ^ (mask >> j & 1);
tans += min(cnt, m - cnt);
}
ans = min(ans, tans);
}
}
printf("%d\n", ans <= k ? ans: -1);
}
return 0;
}

Codeforces Round #243 (Div. 2)——Sereja and Table的更多相关文章

  1. Codeforces Round #243 (Div. 2)——Sereja and Swaps

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/u012476429/article/details/24665103 题目链接 题意: 给定一个整数 ...

  2. Codeforces Round #243 (Div. 1)——Sereja and Two Sequences

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/u012476429/article/details/24798219 题目链接 题意:给两个长度分别 ...

  3. Codeforces Round #243 (Div. 1)——Sereja and Squares

    题目链接 题意: 给n个点,求能组成的正方形的个数. 四边均平行与坐标轴 大神的分析: 经典题 我们考虑每一种x坐标,显然仅仅有<= sqrt{N}个x坐标出现了> sqrt{N}次,我们 ...

  4. Codeforces Round #243 (Div. 2) Problem B - Sereja and Mirroring 解读

    http://codeforces.com/contest/426/problem/B 对称标题的意思大概是.应当指出的,当线数为奇数时,答案是线路本身的数 #include<iostream& ...

  5. Codeforces Round #243 (Div. 1)A. Sereja and Swaps 暴力

    A. Sereja and Swaps time limit per test 1 second memory limit per test 256 megabytes input standard ...

  6. Codeforces Round #243 (Div. 2) C. Sereja and Swaps

    由于n比较小,直接暴力解决 #include <iostream> #include <vector> #include <algorithm> #include ...

  7. Codeforces Round #243 (Div. 2) B. Sereja and Mirroring

    #include <iostream> #include <vector> #include <algorithm> using namespace std; in ...

  8. Codeforces Round #243 (Div. 2) A. Sereja and Mugs

    #include <iostream> #include <vector> #include <algorithm> #include <numeric> ...

  9. Codeforces Round #243 (Div. 2) C. Sereja and Swaps(优先队列 暴力)

    题目 题意:求任意连续序列的最大值,这个连续序列可以和其他的 值交换k次,求最大值 思路:暴力枚举所有的连续序列.没做对是因为 首先没有认真读题,没看清交换,然后,以为是dp或者贪心 用了一下贪心,各 ...

随机推荐

  1. BZOJ 1010: [HNOI2008]玩具装箱toy(DP+斜率优化)

    [HNOI2008]玩具装箱toy Description P教授要去看奥运,但是他舍不下他的玩具,于是他决定把所有的玩具运到北京.他使用自己的压缩器进行压缩,其可以将任意物品变成一堆,再放到一种特殊 ...

  2. 初识laytpl

    laytpl-精致巧妙的JavaScript模板引擎 这两天在做一个mui项目,列表需要循环很多的数据.在公司同事的指引下认识了这个新的模板--laytpl.我只想说,很好用们很巧妙. 废话不多说,直 ...

  3. 【03】react 之 创建component

    React推出后,出于不同的原因先后出现三种定义react组件的方式,殊途同归:具体的三种方式: 函数式定义的无状态组件 es5原生方式React.createClass定义的组件 es6形式的ext ...

  4. 【转】Nodejs学习笔记(一)--- 简介及安装Node.js开发环境

    目录 学习资料 简介 安装Node.js npm简介 开发工具 Sublime Node.js开发环境配置 扩展:安装多版本管理器 学习资料 1.深入浅出Node.js http://www.info ...

  5. JavaScript 的新特性:类的 #private 字段

    这是什么,如何使用,为什么需要? 一边听“Noise Pollution” —— Portugal. The Man,一边阅读本文简直就是享受 JavaScript 标准的第二阶段(Stage 2)加 ...

  6. SQL查询重复记录方法大全 转

    原文发布时间为:2010-08-09 -- 来源于本人的百度文章 [由搬家工具导入] 查找所有重复标题的记录: SELECT *FROM t_info aWHERE ((SELECT COUNT(*) ...

  7. Java中的内存机制及管理

    1. Java根据虚拟机以及平台的版本不同而在内存中开辟不同大小的内存,通常不会关注这个大小. 2. 程序中的对象存储在内存的堆(heap)中 3. 程序中的方法和局部变量存储在内存的栈(Stack) ...

  8. [LeetCode] Count and Say 字符串

    The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...

  9. [LeetCode] Reorder List 反向插入链表

    Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You must do thi ...

  10. PE笔记之NT头PE签名

    typedef struct _IMAGE_NT_HEADERS {       DWORD Signature;                     //PE头签名PE\0\0       IM ...