Flip and Shift


Time Limit: 2 Seconds      Memory Limit: 65536 KB

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

Figure 1. A flip and a shift

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


Figure 2. A goal sequence

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

Output for the Sample Input

YES
NO


Source: Asia 2001, Taejon (South Korea)

      题意:在一个圆形的首尾相连的容器中放两种球,以任意一个球为中心,交换相邻两球的位置,问是否可以将颜色相同的球放在一起。

其实这题主要考察的是你运用数学知识分析问题的能力。设位置编号为1,2,3,...,n,n为小球的总数目。

     若n为偶数,则不管如何交换小球,各个小球的位置编号奇偶性保持不变,因为交换的步长为2。如果黑球
     是连续,当且仅当奇位置上的黑球个数与偶位置上的黑球个数相差不大于1。
     若n为奇数,则不管如何小球
     的布局如何,一定能使黑白球各自连续。因为在这种情况下,奇位置上的小球通过交换可以移动到偶位置上。

     这样通过交换就可以使当前布局满足连续的必要性了

 #include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <cctype>
#include <string>
#include <map>
#define N 500015
#define INF 1000000
#define ll long long
using namespace std; int main(void)
{
int t,odd,even,n;
scanf("%d",&t);
while(t--)
{
even = odd = ;
scanf("%d",&n);
for(int i = ; i < n; i++)
{
int tp;
scanf("%d",&tp);
if(i & )
odd += tp;
else
even += tp;
}
if(n & )
printf("YES\n");
else
{
if(abs(odd - even) < )
printf("YES\n");
else
printf("NO\n");
}
}
return ;
}

zoj 1028 Flip and Shift(数学)的更多相关文章

  1. POJ1063 Flip and Shift

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

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

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

  3. POJ 1063 - Flip and Shift

    Description This puzzle consists of a random sequence of m black disks and n white disks on an oval- ...

  4. ZOJ 2679 Old Bill(数学)

    主题链接:problemCode=2679" target="_blank">http://acm.zju.edu.cn/onlinejudge/showProbl ...

  5. ZOJ 2680 Clock()数学

    主题链接:problemId=1680" target="_blank">http://acm.zju.edu.cn/onlinejudge/showProblem ...

  6. 1254 Flip and Shift

    这题是目的是把黑球和白球分开连续放,事实上只要把其中一种颜色分好在一边就可以,可以绕一个球转即是第n个球可以放在n-2或者n+2上,因为这是个环,所以只需要把黑球或者白球连续放好就可以,当一共有奇数个 ...

  7. zoj 2722 Head-to-Head Match(数学思维)

    题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2722 题目描述: Our school is planning ...

  8. ZOJ 3203 Light Bulb(数学对勾函数)

    Light Bulb Time Limit: 1 Second      Memory Limit: 32768 KB Compared to wildleopard's wealthiness, h ...

  9. ZOJ - 3866 Cylinder Candy 【数学】

    题目链接 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3866 思路 积分 参考博客 https://blog.csdn. ...

随机推荐

  1. uploadify附件上传 传参

    首先 在刚加载jsp时就加入上传方法,所以 formDate 中的参数 zFileName是页面刚加载时 exp1的值 ,后来通过js方法赋值不被读过来,如果 你想要获得这个值,可在 调用upload ...

  2. java基础之DateFormat类

    DateFormat DateFormat类概述 DateFormat 是日期/时间格式化子类的抽象类,它以与语言无关的方式格式化并解析日期或时间. 是抽象类,所以使用其子类SimpleDateFor ...

  3. hiveUDF的使用

    在此自己总结下UDF的用法 1.首先最简单的UDF(普通用java扩充函数的方式,大多数简便函数可以用这个函数来实现,返回单个字段),其加强版UDGF据说对map一类数据类型有更好兼容,实现上略复杂 ...

  4. Windows API 23 篇 WTSQuerySessionInformation

    函数原型:BOOLWINAPIWTSQuerySessionInformationW(                                                      IN ...

  5. 后缀数组(SA)及height数组

    最近感觉自己越来越蒟蒻了--后缀数组不会,费用流不会-- 看着别人切一道又一道的题,我真是很无奈啊-- 然后,我花了好长时间,终于弄懂了后缀数组. 后缀数组是什么? 后缀SASASA数组 给你一个字符 ...

  6. leyou_07_对数据的操作

    1.目标在数据库的两张表中拿到以下数据,并完成状态.搜索和分页功能 实体类Spu(页面需要的数据) 实体类Category(页面需要的数据) name:商品分类 2.分析: 返回的数据在两个实体类中, ...

  7. Spring注解驱动开发(四)-----aop、声明式事务

    AOP 概念 指在程序运行期间动态的将某段代码切入到指定方法指定位置进行运行的编程方式:-----基于动态代理 一个aop示例 1.导入aop模块:Spring AOP:(spring-aspects ...

  8. 469 Same Tree

    原题网址:https://www.lintcode.com/problem/same-tree/description 描述 检查两棵二叉树是否等价.等价的意思是说,首先两棵二叉树必须拥有相同的结构, ...

  9. 转:Linux fork与vfork的深入分析

    源地址:http://linux.chinaitlab.com/c/831529.html 一)fork的概述 .操作系统对进程的管理,是通过进程表完成的.进程表中的每一个表项,记录的是当前操作系统中 ...

  10. Ajax技术 - (Asynchronous JavaScript + XML)

    Ajax Ajax = 异步JavaScript和XML,Ajax是一种用于创建快速动态网页的技术. 通过在后台与服务器进行少量数据交换,Ajax可以使网页实现异步更新.可以再网页不重新加载的情况下, ...