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

题目链接

  • 题意:

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

    现给一个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. TypeError: $.ajaxFileUpload(…) is not a function

    今天做一个图片上传功能,用到了ajaxFileUpload,控制台报错TypeError: $.ajaxFileUpload(…) is not a function,都说是jQuery版本问题,也试 ...

  2. Google Code Jam 2008 Round 1A C Numbers(矩阵快速幂+化简方程,好题)

    Problem C. Numbers This contest is open for practice. You can try every problem as many times as you ...

  3. 计数(count)

    计数(count) 题目描述 既然是萌萌哒 visit_world 的比赛,那必然会有一道计数题啦! 考虑一个 NN个节点的二叉树,它的节点被标上了 1∼N1∼N 的编号. 并且,编号为 ii的节点在 ...

  4. 2017-2018-2 20179204《网络攻防实践》第十三周学习总结 python实现国密算法

    国密商用算法是指国密SM系列算法,包括基于椭圆曲线的非对称公钥密码SM2算法.密码杂凑SM3算法.分组密码SM4算法,还有只以IP核形式提供的非公开算法流程的对称密码SM1算法等. 第1节 SM2非对 ...

  5. pat 1074. 宇宙无敌加法器(20)

    1074. 宇宙无敌加法器(20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 地球人习惯使用十进制数,并且默 ...

  6. configurationmanager.getsection usage

    public static void CreateAppSettings() { // Get the application configuration file. System.Configura ...

  7. hdu 1576(逆元)

    A/B Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  8. LeetCode OJ-- Flatten Binary Tree to Linked List **

    https://oj.leetcode.com/problems/flatten-binary-tree-to-linked-list/ 二叉树的处理,将二叉树转换成类似一个单链表的东东,并且原地. ...

  9. LeetCode OJ-- Count and Say

    https://oj.leetcode.com/problems/count-and-say/ 求经过n次变换后,变成了什么. 1  11  21  1211   111221 ps. 3 变成 ‘3 ...

  10. HttpModel 和 HttpHandle

    页面的请求过程: HttpRequest-> inetinfo.exe-> aspnet_isapi.dll-> Http pipeline(命名管道)-> aspnet_wp ...