[OpenJudge 3061]Flip The Card
[OpenJudge 3061]Flip The Card
试题描述
There are N× Ncards, which form an N× Nmatrix. The cards can be placed upwards or downwards. Now Acer is going to do some operations so that all the cards are placed upwards after the operations. In each operation, Acer can flip over exactly an M× Msub-matrix of cards. Acer wants to know the minimum number of operations to achieve his goal.
输入
The first line contains two integers, N and M (0 < M ≤ N ≤ 1000).
Each of the next N lines contains N integers. If the integer is 1, the corresponding card is placed upwards initially; otherwise it is placed downwards initially.
输出
Output an integer, which indicate the minimum number of operations. If there is no solution, output -1.
输入示例
输出示例
数据规模及约定
见“输入”
题解
贪心,我们发现进行两次完全相同的操作是没有必要的,并且操作的先后顺序无所谓,所以我们就从上到下从左到右依次进行反转操作。当 (i, j) 这个格子是 0,则反转 (i, j) 为左上角,(i+m-1, j+m-1) 为右下角的矩形,如果这个矩形超界,就说明不可能全部翻成正面。
每次直接暴力反转是不行的,可以打一个 flip[i][j] 标记,然后查看左上角总共打了多少次标记,记这个次数为 x,那么 x 为奇数时表明这个格子与最初的状态相反,x 为偶数时说明这个格子和最初状态相同,这个标记是动态加的,所以要用数据结构维护,不难发现这是个二维偏序,我们的操作已经将它的一维排好序了,只需要在第二维建立一个树状数组就行了。
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <stack>
#include <vector>
#include <queue>
#include <cstring>
#include <string>
#include <map>
#include <set>
using namespace std; const int BufferSize = 1 << 16;
char buffer[BufferSize], *Head, *Tail;
inline char Getchar() {
if(Head == Tail) {
int l = fread(buffer, 1, BufferSize, stdin);
Tail = (Head = buffer) + l;
}
return *Head++;
}
int read() {
int x = 0, f = 1; char c = Getchar();
while(!isdigit(c)){ if(c == '-') f = -1; c = Getchar(); }
while(isdigit(c)){ x = x * 10 + c - '0'; c = Getchar(); }
return x * f;
} #define maxn 1010
int n, m;
bool A[maxn][maxn], flip[maxn][maxn]; int C[maxn];
void add(int x, int v) { for(; x <= n; x += x & -x) C[x] += v; return ; }
int sum(int x) { int res = 0; for(; x; x -= x & -x) res += C[x]; return res; } int main() {
n = read(); m = read();
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++) A[i][j] = read(); int cnt = 0;
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++) {
add(j, flip[i][j]);
A[i][j] ^= (sum(j) & 1);
if(A[i][j]) continue;
if(i > n - m + 1 || j > n - m + 1) return puts("-1"), 0;
// printf("%d %d\n", i, j);
cnt++;
A[i][j] ^= 1; flip[i][j] ^= 1; flip[i+m][j] ^= 1; flip[i][j+m] ^= 1; flip[i+m][j+m] ^= 1;
add(j, 1);
} printf("%d\n", cnt); return 0;
}
[OpenJudge 3061]Flip The Card的更多相关文章
- RFID 读写器 Reader Writer Cloner
		
RFID读写器的工作原理 RFID的数据采集以读写器为主导,RFID读写器是一种通过无线通信,实现对标签识别和内存数据的读出和写入操作的装置. 读写器又称为阅读器或读头(Reader).查询器(Int ...
 - 使用 electron 实现类似新版 QQ 的登录界面效果(阴影、背景动画、窗体3D翻转)
		
上文<使用 VS2017 和 js 进行桌面程序开发 - electron 之 Hello Word>介绍了如何使用 VS2017 开发 electron 桌面程序,今天来点有看头的,但是 ...
 - 2018浙江省赛(ACM) The 15th Zhejiang Provincial Collegiate Programming Contest Sponsored by TuSimple
		
我是铁牌选手 这次比赛非常得爆炸,可以说体验极差,是这辈子自己最脑残的事情之一. 天时,地利,人和一样没有,而且自己早早地就想好了甩锅的套路. 按理说不开K就不会这么惨了啊,而且自己也是毒,不知道段错 ...
 - AtCoder Regular Contest 091
		
数学场,做到怀疑人生系列 C - Flip,Flip, and Flip...... Time limit : 2sec / Memory limit : 256MB Score : 300 poin ...
 - 介绍css 的3D 变换(3D transform)
		
https://desandro.github.io/3dtransforms/docs/card-flip.html ---------------------------------------- ...
 - OpenJudge/Poj 1753 Flip Game
		
1.链接地址: http://bailian.openjudge.cn/practice/1753/ http://poj.org/problem?id=1753 2.题目: 总时间限制: 1000m ...
 - [CSS] Create a Card Flip Animation with CSS
		
Animation can be a powerful way to enhance a user experience. In this lesson, we'll walk through the ...
 - Card Flip
		
卡牌效果:O(∩_∩)O,只做了webkit浏览器的效果,请用chrome~ 1.首先呢,先用一个框框把卡牌包住,然后呢,搞两个子元素作为卡牌正反面.当然咯,反面是看不见滴~ <section ...
 - OpenJudge / Poj 1003 Hangover
		
链接地址: Poj:http://poj.org/problem?id=1003 OpenJudge:http://bailian.openjudge.cn/practice/1003 题目: Han ...
 
随机推荐
- Bootstrap系列 -- 22. 按钮详解
			
Bootstrap框架首先通过基础类名“.btn”定义了一个基础的按钮风格,然后通过“.btn-default”定义了一个默认的按钮风格.默认按钮的风格就是在基础按钮的风格的基础上修改了按钮的背景颜色 ...
 - 实施项目--.NET实现仓库看板的一些感想
			
从一名技术开发人员到实施人员的蜕变,从不同的角度看待同一个问题,或许会有不一样的结果.这里记录一下最近一个项目实施的案例,非常有感触! 一. 项目情况简介 本次项目是给一个国外生产型企业做仓库方面的系 ...
 - C#基础知识系列六(静态类和静态类成员)
			
静态类 静态类与非静态类基本相同,但存在一个区别:静态类不能实例化. 也就是说,不能使用 new 关键字创建静态类类型的变量. 因为没有实例变量,所以要使用类名本身访问静态类的成员. 例如,如果名为 ...
 - No plugin found for prefix 'jetty' in the current project and in the plugin groups  【转】
			
在maven进行jetty的调试中出现错误: [plain] view plaincopyprint? [ERROR] No plugin found for prefix 'jetty' in th ...
 - 解决 pathForResource 返回 nil的问题
			
点击(此处)折叠或打开 NSString* path = [[NSBundle mainBundle] pathForResource:@"sample" ofType:@&quo ...
 - ASP.NET MVC实现POST方式的Redirect
			
我们知道,在ASP.NET MVC中,要从一个Action跳转到另一个Action,通常是用一系列以“Redirect”开头的方法 Redirect RedirectToAction Redirect ...
 - POJ-2352 Stars   树状数组
			
Stars Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 39186 Accepted: 17027 Description A ...
 - 【bzoj1046】 HAOI2007—上升序列
			
http://www.lydsy.com/JudgeOnline/problem.php?id=1046 (题目链接) 题意 给出一个数列,求数列中长度为L的下标字典序最小的上升子序列. Soluti ...
 - 洛谷P1755 攻击火星
			
题目描述 一群外星人将要攻击火星. 火星的地图是一个n个点的无向图.这伙外星人将按照如下方法入侵,先攻击度为0的点(相当于从图中删除掉它),然后是度为1的点,依此类推直到度为n-1的点. 所有的点度统 ...
 - MySql批处理的小窍门:排行榜类数据生成
			
MySql批处理的小窍门:排行榜类数据生成 最近在做新版本的开发,其中涉及到排行榜的批量预生成,在此分享给大家. 关键点 名次的计算(不考虑用游标) 单榜单查询 对于排行榜这种类型的数据,当只查一个排 ...