Codeforces Round #243 (Div. 2)——Sereja and Table
看这个问题之前,能够先看看这个论文《一类算法复合的方法》,说白了就是分类讨论,可是这个思想非常重要
- 题意:
首先给出联通块的定义:对于相邻(上下和左右)的同样的数字视为一个联通块
现给一个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的更多相关文章
- Codeforces Round #243 (Div. 2)——Sereja and Swaps
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/u012476429/article/details/24665103 题目链接 题意: 给定一个整数 ...
- Codeforces Round #243 (Div. 1)——Sereja and Two Sequences
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/u012476429/article/details/24798219 题目链接 题意:给两个长度分别 ...
- Codeforces Round #243 (Div. 1)——Sereja and Squares
题目链接 题意: 给n个点,求能组成的正方形的个数. 四边均平行与坐标轴 大神的分析: 经典题 我们考虑每一种x坐标,显然仅仅有<= sqrt{N}个x坐标出现了> sqrt{N}次,我们 ...
- Codeforces Round #243 (Div. 2) Problem B - Sereja and Mirroring 解读
http://codeforces.com/contest/426/problem/B 对称标题的意思大概是.应当指出的,当线数为奇数时,答案是线路本身的数 #include<iostream& ...
- 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 ...
- Codeforces Round #243 (Div. 2) C. Sereja and Swaps
由于n比较小,直接暴力解决 #include <iostream> #include <vector> #include <algorithm> #include ...
- Codeforces Round #243 (Div. 2) B. Sereja and Mirroring
#include <iostream> #include <vector> #include <algorithm> using namespace std; in ...
- Codeforces Round #243 (Div. 2) A. Sereja and Mugs
#include <iostream> #include <vector> #include <algorithm> #include <numeric> ...
- Codeforces Round #243 (Div. 2) C. Sereja and Swaps(优先队列 暴力)
题目 题意:求任意连续序列的最大值,这个连续序列可以和其他的 值交换k次,求最大值 思路:暴力枚举所有的连续序列.没做对是因为 首先没有认真读题,没看清交换,然后,以为是dp或者贪心 用了一下贪心,各 ...
随机推荐
- NOIP2017赛前模拟10月30日总结
题目1: n个人参赛(n<=100000),每个人有一个权值··已知两个人权值绝对值之差小于等于K时,两个人都有可能赢,若大于则权值大的人赢···比赛为淘汰制,进行n-1轮·问最后可能赢的人有多 ...
- python和tensorflow安装
一.Python安装 python采用anaconda安装,简单方便,下载python3.6的anaconda linux64的sh安装文件. 1.bash Anaconda-2.1.0-Linux ...
- python爬虫异常处理
import urllib2 try: response = urllib2.urlopen('http://www.baidu.com') except urllib2.URLError, e: p ...
- iOS之UITraitCollection
UITraitCollection 为表征 size class 而生,用来区分设备.你可以在它身上获取到足以区分所有设备的特征. UITraitEnvironment 协议.UIContentCon ...
- ios 瀑布流的那些事情
转载: 屎壳郎情调-成长日记 首先要知道:瀑布流的核心就是要获取到图片的长宽 网上的很多例子都是加载本地图片的 对于新手而言 改成加载网络图片的确是有点压力的 因为本地的图片 我们是很容易就能获取到 ...
- Safari 11.0 已发布,新特性都在这儿了!
Safari 11.0 兼容性 Safari 11.0 可运行于 iOS 11.0 和 macOS 10.1版本的系统环境,同时在macOS 10.12.6 和 10.11.6版本中也可以使用. Hi ...
- Java语法糖(一)
概述 语法糖(Syntactic Sugar):主要作用是提高编码效率,减少编码出错的机会. 解语法糖发生在Java源码被编译成Class字节码的过程中,还原回简单的基础语法结构. 语法糖之一:泛型( ...
- [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 ...
- 解决Manjaro linux的中文输入
系统安装完成后, 首先:安装中文输入法:pacman -S fcitx fcitx-libpinyin kcm-fcitx 接着:修改.xprofile 添加内容如下: export GTK2_RC_ ...
- check source code after macro expand
Some time I'd like check source code after macro expand. We can use -E option to stop after the prep ...