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 in an oval-shaped track, with a turnstile cap Able of flipping (i.e., reversing) three consecutive disks. In Figure 1, there is 8 black disks and disks on the track. Spin the turnstile to the flip the three disks in it or shift one position clockwise for each of the disks on the TRAC K (Figure 1).
Figure 1. A flip and a shift
The goal of this puzzle are to gather the disks of the same color in adjacent positions using flips and shifts. (Figure 2)
Figure 2. A goal Sequence
You is to write a program which decides whether a given sequence can reach a goal or not. If a goal is reachable and 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 init Ial sequence. A 0 denotes a white disk and a 1 denotes a black disk. The sum of M and N is at least 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
To summarize the title means that for a string, the machine can exchange data at the position x and (x+2)%length.
So when the number of balls is odd, any two positions can be exchanged,
If they are even, and the odd positions can be exchanged, the even-numbered positions can be exchanged, so this requires that the number of 1 in the odd position and even position should be no more than one
#include <iostream>#include<cmath>#include<cstdio>#include<vector>#include<list>#include<string>#include<cstring>#include<cstdio>#include<algorithm>#include<Set>#include<stack>using namespacestd;intMain () {intN; CIN>>N; while(n--) { intT; CIN>>T; intcnt1=0, cnt2=0; for(inti =0; I < T; ++i) {intnum; CIN>>num; if(num = =1) { ifI2==0) Cnt1++; ElseCnt2++; } } if(t%2==1) cout<<"YES"<<Endl; Else { if(ABS (CNT1-CNT2) <=1) cout<<"YES"<<Endl; Elsecout<<"NO"<<Endl; } }}
Zoj 1028 Flip and Shift