Description

This puzzle consists of a random sequence of m black disks and n white disks on an oval-shaped track, with a turnstile capable of flipping (i.e., reversing) three consecutive disks. In Figure 1, there are 8 black disks and 10 white disks on the track. You may spin the turnstile to flip the three disks in it or shift one position clockwise for each of the disks on the track (Figure 1). 

The goal of this puzzle is to gather the disks of the same color in adjacent positions using flips and shifts. (Figure 2) 

You are to write a program which decides whether a given sequence can reach a goal or not. If a goal is reachable, then write a message "YES"; otherwise, write a message "NO".

Input

The input consists of T test cases. The number of test cases ) (T is given in the first line of the input file. Each of the next T lines gives a test case. A test case consists of an integer, representing the sum of m and n, and a sequence of m+n 0s and 1s, representing an initial sequence. A 0 denotes a white disk and a 1 denotes a black disk. The sum of m and n is at least 10 and does not exceed 30. There is a space between numbers.

Output

The output should print either "YES" or "NO" for each test case, one per line.

Sample Input

2
18 0 0 1 0 1 1 1 1 0 1 0 0 1 0 0 0 0 1
14 1 1 0 0 1 1 1 0 0 1 1 0 1 0

Sample Output

YES
NO

Source

 
不算水题,搞清楚原理后属于简单题。

当总的个数n为odd时,任何一个球都能到达圆圈中的每一个位置,所以就是YES。

当总的个数n为even时,那么如果这个球的位置在odd位置上,那么它能到任意一个odd位置,若在even位置上,则能到任意一个even位置。

那么不妨把黑球全部移到一起,不去管白球,那么如果偶数位置的黑球更多一些,中间就夹着少一个的奇数的空位,把剩下的那些奇数个黑球放进去,如果 even - odd = 0 or 1 ,那么这个时候是可以把空位放满的,否则就放不满,即NO;类似的如果odd位置的黑球更多一点,就判断odd - even = 0 or 1是否为真即可。

 #include<stdio.h>
#include<stdlib.h>
int main()
{
  int even,odd;
  int i,n,disk[],T;scanf("%d",&T);
  while(T){
    scanf("%d",&n);
    for(i=;i<n;i++) scanf("%d",&disk[i]);
    if( n% == ) printf("YES\n"); //n为odd,必然YES
    else{
      even=,odd=; //初始化even位和odd位的黑球数
      for(i=;i<n;i++){
        if( disk[i]== && i%== ) odd++; //odd位黑球计数
        if( disk[i]== && i%== ) even++; //even位黑球计数
    }
    if(abs(odd-even)>) printf("NO\n"); //判断"|odd - even| = 0 or 1"是否成立
    else printf("YES\n");
  }
  T--;
  }
}

POJ 1063 - Flip and Shift的更多相关文章

  1. POJ 1063 Flip and Shift 最详细的解题报告

    题目来源:Flip and Shift 题目大意:一个椭圆形的环形容器中有黑色和白色两种盘子,问你是否可以将黑色的盘子连续的放在一起.你可以有以下两种操作: 1.顺时针旋转所有的盘子 2.顺时针旋转3 ...

  2. POJ1063 Flip and Shift

    题目来源:http://poj.org/problem?id=1063 题目大意: 有一种游戏如图所示.一个填满黑白球的转盘,它可以有两种操作,一种是将大转盘顺时针旋转,所有球的位置顺时针挪一位,另一 ...

  3. 枚举 POJ 1753 Flip Game

    题目地址:http://poj.org/problem?id=1753 /* 这题几乎和POJ 2965一样,DFS函数都不用修改 只要修改一下change规则... 注意:是否初始已经ok了要先判断 ...

  4. zoj 1028 Flip and Shift(数学)

    Flip and Shift Time Limit: 2 Seconds      Memory Limit: 65536 KB This puzzle consists of a random se ...

  5. POJ 1753. Flip Game 枚举or爆搜+位压缩,或者高斯消元法

    Flip Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 37427   Accepted: 16288 Descr ...

  6. OpenJudge/Poj 1753 Flip Game

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

  7. POJ 1753 Flip Game(高斯消元+状压枚举)

    Flip Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 45691   Accepted: 19590 Descr ...

  8. POJ 1753 Flip Game DFS枚举

    看题传送门:http://poj.org/problem?id=1753 DFS枚举的应用. 基本上是参考大神的.... 学习学习.. #include<cstdio> #include& ...

  9. POJ 1753 Flip Game(状态压缩+BFS)

    题目网址:http://poj.org/problem?id=1753 题目: Flip Game Description Flip game is played on a rectangular 4 ...

随机推荐

  1. javascript promises powered by BlueBird

    什么是promises? 为什么需要promises? 参见: https://promisesaplus.com/ 使用示例: 使用promises之前,代码的编写方式: 使用promises之后: ...

  2. Effective Java 第三版——46. 优先考虑流中无副作用的函数

    Tips <Effective Java, Third Edition>一书英文版已经出版,这本书的第二版想必很多人都读过,号称Java四大名著之一,不过第二版2009年出版,到现在已经将 ...

  3. [转]ssh和SSH服务(包含隧道内容)

    本文对SSH连接验证机制进行了非常详细的分析,还详细介绍了ssh客户端工具的各种功能,相信能让各位对ssh有个全方位较透彻的了解,而不是仅仅只会用它来连接远程主机. 另外,本人翻译了ssh客户端命令的 ...

  4. 使用tar解压文件提示gzip: stdin: not in gzip format错误

    使用tar解压文件提示gzip: stdin: not in gzip format错误 1. 问题描述 使用docker save xxxx > xxx.tar导出镜像,由于文件太大,需要sp ...

  5. elasticsearch和mysql排序问题

    elasticsearch 字段类型错误 最近用elasticseach做排序,排序字段是float型的,没有使用mapping,是直接写代码导入的,没想到排序时如果有小数和整数就会出现错误. 于是查 ...

  6. 【iCore4 双核心板_ARM】例程六:IWDG看门狗实验——复位ARM

    实验原理: STM32内部包含独立看门狗,通过看门狗可以监控程序远行,程序运行错误时, 未在规定时间内喂狗,自动复位ARM.本实验通过按键按下,停止喂狗,制造程序运行 错误,从而产生复位. 核心代码: ...

  7. 【emWin】例程十七:窗口对象——Button

    介绍: 按钮小工具通常用作触摸屏的主要用户界面元素,本例程介绍按钮小工具的创建及使用方法. 触摸校准(上电可选择是否进入校准界面) 自绘按钮(通过回调函数来自行绘制各种形状的按钮) 设置按钮字体 设置 ...

  8. swift 遍历枚举

      // see at http://swifter.tips/enum-enumerate/ // 貌似有些空格在粘贴的时候没有了...    = =! import Foundation   en ...

  9. java反编译工具(Java Decompiler)

    1,下载地址,包括GUI,Eclipse插件 http://jd.benow.ca/ 2,Eclipse插件的安装参看 https://blog.csdn.net/yh_zeng2/article/d ...

  10. 3D点云的深度学习

    使用卷积神经网络(CNN)架构的深度学习(DL)现在是解决图像分类任务的标准解决方法.但是将此用于处理3D数据时,问题变得更加复杂.首先,可以使用各种结构来表示3D数据,所述结构包括: 1  体素网格 ...