[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的更多相关文章

  1. RFID 读写器 Reader Writer Cloner

    RFID读写器的工作原理 RFID的数据采集以读写器为主导,RFID读写器是一种通过无线通信,实现对标签识别和内存数据的读出和写入操作的装置. 读写器又称为阅读器或读头(Reader).查询器(Int ...

  2. 使用 electron 实现类似新版 QQ 的登录界面效果(阴影、背景动画、窗体3D翻转)

    上文<使用 VS2017 和 js 进行桌面程序开发 - electron 之 Hello Word>介绍了如何使用 VS2017 开发 electron 桌面程序,今天来点有看头的,但是 ...

  3. 2018浙江省赛(ACM) The 15th Zhejiang Provincial Collegiate Programming Contest Sponsored by TuSimple

    我是铁牌选手 这次比赛非常得爆炸,可以说体验极差,是这辈子自己最脑残的事情之一. 天时,地利,人和一样没有,而且自己早早地就想好了甩锅的套路. 按理说不开K就不会这么惨了啊,而且自己也是毒,不知道段错 ...

  4. AtCoder Regular Contest 091

    数学场,做到怀疑人生系列 C - Flip,Flip, and Flip...... Time limit : 2sec / Memory limit : 256MB Score : 300 poin ...

  5. 介绍css 的3D 变换(3D transform)

    https://desandro.github.io/3dtransforms/docs/card-flip.html ---------------------------------------- ...

  6. OpenJudge/Poj 1753 Flip Game

    1.链接地址: http://bailian.openjudge.cn/practice/1753/ http://poj.org/problem?id=1753 2.题目: 总时间限制: 1000m ...

  7. [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 ...

  8. Card Flip

    卡牌效果:O(∩_∩)O,只做了webkit浏览器的效果,请用chrome~ 1.首先呢,先用一个框框把卡牌包住,然后呢,搞两个子元素作为卡牌正反面.当然咯,反面是看不见滴~ <section ...

  9. OpenJudge / Poj 1003 Hangover

    链接地址: Poj:http://poj.org/problem?id=1003 OpenJudge:http://bailian.openjudge.cn/practice/1003 题目: Han ...

随机推荐

  1. WebView加载本地html、js文件常见问题及解决办法

    声明:基于android studio平台,php语言搭建服务器 目录: 一.JavaScript脚本语言没有反应 二.alert无法弹出 三.html页面之间不能跳转 四.屏幕缩放没有达到预期效果 ...

  2. 使用TransactionScopeOption 管理事务流

    可通过调用一个方法来嵌套事务范围,该方法在使用其自己范围的方法中使用 TransactionScope,下面示例中的 RootMethod 方法就是前者这样的方法. void RootMethod() ...

  3. Java基础-代理

    我们书写执行一个功能的函数时,经常需要在其中写入与功能不是直接相关但很有必要的代 码,如日志记录,信息发送,安全和事务支持等,这些枝节性代码虽然是必要的,但它会带 来以下麻烦: 枝节性代码游离在功能性 ...

  4. 42.Android之ListView中ArrayAdapter简单学习

    今天学习下Android中ListView关于ArrayAdapter数据绑定, 废话少说直接上代码. 改下布局文件: <?xml version="1.0" encodin ...

  5. BZOJ-1922 大陆争霸 多限制、分层图最短路 (堆+dijkstra)

    1922: [Sdoi2010]大陆争霸 Time Limit: 10 Sec Memory Limit: 64 MB Submit: 1154 Solved: 478 [Submit][Status ...

  6. POJ1947 Rebuilding Roads

    Description The cows have reconstructed Farmer John's farm, with its N barns (1 <= N <= 150, n ...

  7. css常用技巧

    去点号 list-style:none; 字体居中 text-align:center; 链接去下划线 text-decoration:none; 鼠标禁止右键 <body oncontextm ...

  8. Android中调用百度地图

    一.调用百度地图 --第一种方法 1.下载百度地图SDK SDK可以拆分下载,需要使用那一部分功能就下载相应包含的SDK,如下图 核心的的jar 和so包,放在工程中的libs目录下 2.申请key ...

  9. 哈希加密算法 MD5,SHA-1,SHA-2,SHA-256,SHA-512,SHA-3,RIPEMD-160 - aTool

    一.MD5哈希加密算法 atool.org MD5即Message-Digest Algorithm 5(信息-摘要算法 5),用于确保信息传输完整一致.是计算机广泛使用的散列算法之一(又译摘要算法. ...

  10. 高速公路(Highway,ACM/ICPC SEERC 2005,UVa1615)

    I think: #include <stdio.h> #include <stdlib.h> #include <string.h> #include <m ...