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. 学而有道--思维导图式总结(一):Nosql分类

    前言: 众所周知,学习是需要方法的.作为一名java程序员,我们需要学习无数的技能,然而我们的大脑并不买账,学习了一项知识,时间一久就会遗忘, 如何更好高效的回忆起曾经学习过的知识,是极其重要的. 有 ...

  2. Markdown的入门教程,非常的使用

    原文链接: https://www.jianshu.com/p/20e82ddb37cb 链接地址  点我      粘贴进来的内容竟然没有图片,好气呦 目录 概述 简介 官方文档 Markdown编 ...

  3. 快速I/O 51node 1406

    #include <bits/stdc++.h> using namespace std; #define LL long long typedef pair<int,int> ...

  4. Odoo的权限

    Odoo的权限的核心是权限组(res_groups).对每个权限组,可以设置权限组的菜单表示,对象表示,记录规则表示,字段表示. 1.菜单/对象级别 设置哪些人可以访问哪些菜单/对象,对象的访问权限包 ...

  5. hdu1693 Eat the Trees [插头DP经典例题]

    想当初,我听见大佬们谈起插头DP时,觉得插头DP是个神仙的东西. 某大佬:"考场见到插头DP,直接弃疗." 现在,我终于懂了他们为什么这么说了. 因为-- 插头DP很毒瘤! 为什么 ...

  6. HZOI20190903模拟36 字符,蛋糕,游戏

    题面:https://www.cnblogs.com/Juve/articles/11461528.html A:字符 暴力模拟一下,细节很多,但是暴力思路都不大一样 先枚举循环节长度,然后处理一个b ...

  7. 原生微信小程序的生命周期

    小程序的生命周期函数:onLaunch:function(){当启动小程序时触发小程序只会启动1次,一般为初次打开时一般只会触发一次},onShow:function(){当小程序从后台切入到前台时触 ...

  8. git异常处理(一)

    请输入提交消息来解释为什么这种合并是必要的 git 在pull或者合并分支的时候有时会遇到这个界面.可以不管(直接下面3,4步),如果要输入解释的话就需要: 1.按键盘字母 i 进入insert模式 ...

  9. centos7 盘符变动 绑定槽位

    服务器下的硬盘主有机械硬盘.固态硬盘以及raid阵列,通常内核分配盘符的顺序是/dev/sda./dev/sdb… ….在系统启动过程中,内核会按照扫描到硬盘的顺序分配盘符(先分配直通的,再分配阵列) ...

  10. Django REST Framework概述

    什么是REST REST与技术无关,代表的是一种软件架构风格,REST是Representational State Transfer的简称,中文翻译为“表征状态转移”.这里说的表征性,就是指资源,通 ...