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

输入示例

  1.  

输出示例

  1.  

数据规模及约定

见“输入

题解

贪心,我们发现进行两次完全相同的操作是没有必要的,并且操作的先后顺序无所谓,所以我们就从上到下从左到右依次进行反转操作。当 (i, j) 这个格子是 0,则反转 (i, j) 为左上角,(i+m-1, j+m-1) 为右下角的矩形,如果这个矩形超界,就说明不可能全部翻成正面。

每次直接暴力反转是不行的,可以打一个 flip[i][j] 标记,然后查看左上角总共打了多少次标记,记这个次数为 x,那么 x 为奇数时表明这个格子与最初的状态相反,x 为偶数时说明这个格子和最初状态相同,这个标记是动态加的,所以要用数据结构维护,不难发现这是个二维偏序,我们的操作已经将它的一维排好序了,只需要在第二维建立一个树状数组就行了。

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <algorithm>
  4. #include <cmath>
  5. #include <stack>
  6. #include <vector>
  7. #include <queue>
  8. #include <cstring>
  9. #include <string>
  10. #include <map>
  11. #include <set>
  12. using namespace std;
  13.  
  14. const int BufferSize = 1 << 16;
  15. char buffer[BufferSize], *Head, *Tail;
  16. inline char Getchar() {
  17. if(Head == Tail) {
  18. int l = fread(buffer, 1, BufferSize, stdin);
  19. Tail = (Head = buffer) + l;
  20. }
  21. return *Head++;
  22. }
  23. int read() {
  24. int x = 0, f = 1; char c = Getchar();
  25. while(!isdigit(c)){ if(c == '-') f = -1; c = Getchar(); }
  26. while(isdigit(c)){ x = x * 10 + c - '0'; c = Getchar(); }
  27. return x * f;
  28. }
  29.  
  30. #define maxn 1010
  31. int n, m;
  32. bool A[maxn][maxn], flip[maxn][maxn];
  33.  
  34. int C[maxn];
  35. void add(int x, int v) { for(; x <= n; x += x & -x) C[x] += v; return ; }
  36. int sum(int x) { int res = 0; for(; x; x -= x & -x) res += C[x]; return res; }
  37.  
  38. int main() {
  39. n = read(); m = read();
  40. for(int i = 1; i <= n; i++)
  41. for(int j = 1; j <= n; j++) A[i][j] = read();
  42.  
  43. int cnt = 0;
  44. for(int i = 1; i <= n; i++)
  45. for(int j = 1; j <= n; j++) {
  46. add(j, flip[i][j]);
  47. A[i][j] ^= (sum(j) & 1);
  48. if(A[i][j]) continue;
  49. if(i > n - m + 1 || j > n - m + 1) return puts("-1"), 0;
  50. // printf("%d %d\n", i, j);
  51. cnt++;
  52. A[i][j] ^= 1; flip[i][j] ^= 1; flip[i+m][j] ^= 1; flip[i][j+m] ^= 1; flip[i+m][j+m] ^= 1;
  53. add(j, 1);
  54. }
  55.  
  56. printf("%d\n", cnt);
  57.  
  58. return 0;
  59. }

[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. js判断页面点击事件

    <input type="submit" name="sb1" id="sb1" onclick="queryclick() ...

  2. 关于Chrome的开发15个小技巧

    一.快速查找文件 如果你使用过Sublime,那么你会知道’Go to anything’的强大.没错,Chrome现在也有了这一功能. 操作如下: 1.F12打开你的Chrome调试器: 2.按下C ...

  3. SQL删除重复的记录(只保留一条)

    首先新建表: --创建示例表 CREATE TABLE t ( id ,) PRIMARY KEY, a ), b ) ) --插入数据 INSERT INTO t SELECT 'aa','bb' ...

  4. 转:浅谈CSS在前端优化中一些值得注意的关键点

    前端优化工作中要考虑的元素多种多样,而合理地使用CSS脚本可以在很大程度上优化页面的加载性能,以下我们就来浅谈CSS在前端优化中一些值得注意的关键点: 当谈到Web的“高性能”时,很多人想到的是页面加 ...

  5. BZOJ-1699 Balanced Lineup 线段树区间最大差值

    Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 41548 Accepted: 19514 Cas ...

  6. 【poj1716】 Integer Intervals

    http://poj.org/problem?id=1716 (题目链接) 题意 给出n个区间,要求取出最少数量的不同的自然数,使每个区间中至少包含2个取出的数. Solution 差分约束. 运用前 ...

  7. hihocoder1187 Divisors

    传送门 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Given an integer n, for all integers not larger than n, f ...

  8. POJ 2752 Seek the Name, Seek the Fame

    传送门 Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 14761   Accepted: 7407 Description ...

  9. 【干货】Laravel --实战篇 UUID(唯一识别码)

    前言 : 一般的唯一识别id都是各种时间戳.毫秒级时间戳加php内置函数或者加上随机数等手段来生成的. 下面给大家介绍一个组件,也是我在各个实战项目中必不可少的一个组件,ramsey/uuid.一.r ...

  10. easyui form submit 不提交

    http://bbs.csdn.net/topics/390811964 function saveProduct() {             //$('#fm').form('submit',  ...