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;
        }
    }
}

 

posted @ 2016-04-07 23:24  JackWang822  阅读(753)  评论(0编辑  收藏  举报