zoj 1028 Flip and Shift

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 


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

概括下题目的意思就是对于一个字符串,该机器可以交换位置为x和(x+2)%length的数据。

因此当球的数量为奇数的时候,任何两个位置又可以进行交换,

如果为偶数,而奇数位置上的可以互相交换,偶数位置上的可以互相交换,因此这就需要在奇数位置和偶数位置上的1的个数差距不能超过一

#include <iostream>
#include <cmath>
#include <cstdio>
#include <vector>
#include <list>
#include <string>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <set>
#include <stack>

using namespace std;

int main()
{
    int n;
    cin >> n;

    while (n--)
    {
        int t;
        cin >> t;
        int cnt1=0, cnt2=0;
        for (int i = 0; i < t; ++i)
        {
            int num;
            cin >> num;
            if (num == 1)
            {
                if (i % 2 == 0)
                    cnt1++;
                else
                    cnt2++;
            }

        }
        if (t % 2 == 1)
            cout << "YES" << endl;
        else
        {
            if (abs(cnt1 - cnt2) <= 1)
                cout << "YES" << endl;
            else
                cout << "NO" << endl;
        }
    }
}
时间: 2024-10-11 04:45:49

zoj 1028 Flip and Shift的相关文章

zoj 1028 Flip and Shift(数学)

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

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

题目来源:Flip and Shift 题目大意:一个椭圆形的环形容器中有黑色和白色两种盘子,问你是否可以将黑色的盘子连续的放在一起.你可以有以下两种操作: 1.顺时针旋转所有的盘子 2.顺时针旋转3个盘子 解题思路:第一种操作表明你可以对任意一个盘子执行第二种操作,所以重点在于第二种操作.仔细分析一下会发现,第二种操作其实就是将某一个盘子当前的index加2,所以我们可以先统计一下黑色盘子的个数count,然后用一个数组将所有的盘子存起来,使数组中0-count所存放的是黑色盘子(通过下标加2

1254 Flip and Shift

这题是目的是把黑球和白球分开连续放,事实上只要把其中一种颜色分好在一边就可以,可以绕一个球转即是第n个球可以放在n-2或者n+2上,因为这是个环,所以只需要把黑球或者白球连续放好就可以,当一共有奇数个球时,如:7个  x 1 0 1 0 1 0(x为任意颜色球)——x一开始是为奇位,然后开始把x向右移动.0 1 x 1 0 1 0——0 1 0 1 x 1 0——0 1 0 1 0 1 x——0 x 0 1 0 1 1(注意这是环)   到这里x到了偶数位..所以就是说当总球数为奇时奇偶位是可以

poj 1063 Flip and Shift

题目链接 输入n个数(0或1),问是否能通过若干次交换相间的两个数字使得所有1都相邻. 解法: 当n为奇数时任意两个数字都能交换,当然是yes. 当n为偶数时相间的两数字可以互相交换,这样会出现两组排列,然后把排列a插入排列b.并且b的相邻两个元素间只能插一个a的元素.要使所有的1相邻,显然a,b中1的个数要么相等,要么相差1.因此直接判定yes或no #include <iostream> #include <cstring> #include <algorithm>

zoj题目分类

饮水思源---zoj 转载自:http://bbs.sjtu.edu.cn/bbscon,board,ACMICPC,file,M.1084159773.A.html 注:所有不是太难的题都被归成了“简单题”,等到发现的时候已经太晚了,我太死脑筋 了……:( 有些题的程序我找不到了,555……:( SRbGa的题虽然都很经典……但是由于其中的大部分都是我看了oibh上的解题报告后做 的,所以就不写了…… 题目排列顺序没有规律……:( 按照个人感觉,最短路有的算做了DP,有的算做了图论. 有些比较

POJ百道水题列表

以下是poj百道水题,新手可以考虑从这里刷起 搜索1002 Fire Net1004 Anagrams by Stack1005 Jugs1008 Gnome Tetravex1091 Knight Moves1101 Gamblers1204 Additive equations 1221 Risk1230 Legendary Pokemon1249 Pushing Boxes 1364 Machine Schedule1368 BOAT1406 Jungle Roads1411 Annive

HOJ 题目分类

转自:http://blog.sina.com.cn/s/blog_65f3869301011a1o.html ******************************************************************************* 简单题(包括枚举,二分查找,(复杂)模拟,基础数据结构(栈.队列),杂题等 ****************************************************************************

HDU 1988 &amp; ZOJ 2991 Flipping Burned Pancakes(数学啊+模拟)

题目链接: HDU:http://acm.hdu.edu.cn/showproblem.php?pid=1988 ZOJ:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1990 Problem Description The cook at the Frobbozz Magic Pancake House sometimes falls asleep on the job while cooking pancakes. As

ZOJ 3048 (HDU 2258) Continuous Same Game (1)

Problem Description Continuous Same Game is a simple game played on a grid of colored blocks. Groups of two or more connected (orthogonally, not diagonally) blocks that are the same color may be removed from the board. When a group of blocks is remov