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 原生Cookie使用用法

    var oCookie = { setCookie: function (name, value, expireDays, path, domain) { var expireDays = expir ...

  2. 11G新特性 -- ASM Fast Mirror Resync

    ASM Fast Mirror Resync     在oracle 10g中,如果asm磁盘因为光纤.控制器发生故障而发生failure,asm将无法继续向该磁盘中写入数据.     asm会将发生 ...

  3. tensorflow 笔记7:tf.concat 和 ops中的array_ops.concat

    用于连接两个矩阵: mn = array_ops.concat([a, d], 1) #  按照第二维度相接,shape1 [m,a] shape2 [m,b] ,concat_done shape ...

  4. linux每日命令(12):nl命令

    nl命令在linux系统中用来计算文件中行号.nl 可以将输出的文件内容自动的加上行号!其默认的结果与 cat -n 有点不太一样, nl 可以将行号做比较多的显示设计,包括位数与是否自动补齐 0 等 ...

  5. DBNull与Null的区别

    Null是.net中无效的对象引用. DBNull是一个类.DBNull.Value是它唯一的实例.它指数据库中数据为空(<NULL>)时,在.net中的值. null表示一个对象的指向无 ...

  6. java中的数据加密1 消息摘要

    消息摘要(Message Digest) 又称为数字摘要(Digital Digest).它是一个唯一对应一个消息或文本的固定长度的值,它由一个单向Hash加密函数对消息进行作用而产生.如果消息在途中 ...

  7. Java知多少(2)虚拟机(JVM)以及跨平台原理

    相信大家已经了解到Java具有跨平台的特性,可以“一次编译,到处运行”,在Windows下编写的程序,无需任何修改就可以在Linux下运行,这是C和C++很难做到的. 那么,跨平台是怎样实现的呢?这就 ...

  8. hello alibaba

    http://ifeve.com/dubbo-learn-book/ http://ifeve.com/leader-follower-thread-model/ http://ifeve.com/a ...

  9. PHP最全笔记(四)(值得收藏,不时翻看一下)

    // 序列化(串行化) # 数据传输均是字符串类型 # 除了资源类型,均可序列化 # 序列化在存放数据时,存放数据本身,也存放数据类型 1.在网络传输数据时:2.为了将数组或对象放在磁盘时 # 序列化 ...

  10. [Stats385] Lecture 04: Convnets from Probabilistic Perspective

    本篇围绕“深度渲染混合模型”展开. Lecture slices Lecture video Reading list A Probabilistic Framework for Deep Learn ...