Loading

POJ1063 Flip and Shift(思维)

链接:https://ac.nowcoder.com/acm/problem/105660
来源:牛客网

题目描述

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

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

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

输入描述:

The input consists of T test cases. The number of test cases ) (T is given in the first line of the input file. 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.

输出描述:

The output should print either "YES" or "NO" for each test case, one per line.

示例1

输入

[复制](javascript:void(0)😉

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

输出

[复制](javascript:void(0)😉

YES
NO

题意相当于可以任取首位相连的序列的相邻三个数reverse,问能否把黑色白色分别凑到一起。

n为奇数一定可以,因为这样奇数位置的数就能换到偶数位置,偶数位置的数也能换到奇数位置。

n为偶数的话偶数位置的数只能在内部换,奇数位置同理,这种情况下由于偶数位置奇数位置个数是一样的,必须要求奇数位置的黑色和偶数位置的黑色相差不超过1,如下所示:

1 2 3 4 5 6 7 8

1 1 1 1 0 0 0 0

1 2 3 4 5 6 7 8

1 1 1 0 0 0 0 0

1 2 3 4 5 6 7 8

0 1 1 1 0 0 0 0

而下面这种相差为2的情况尽力凑的结果也是不行的(偶数位置的1已经尽力贴近奇数位置的一个1了):

1 2 3 4 5 6 7 8

1 1 0 1 0 0 0 1

#include <iostream>
#include <cmath>
using namespace std;
bool a[100005];
int main() {
	int t;
	cin >> t;
	while(t--) {
		int n;
		cin >> n;
		for(int i = 1; i <= n; i++) {
			cin >> a[i];
		}
		if(n & 1) cout << "YES" << endl;
		else {
			int b1 = 0, b2 = 0;
			for(int i = 1; i <= n; i++) {
				if(a[i]) {
					if(i & 1) b1++;
					else b2++;
				}
			}
            int tmp = b1 - b2;
            if(tmp < 0) tmp = -tmp;
			if(tmp <= 1) cout << "YES" << endl;
			else cout << "NO" << endl;
		}
	}
}
posted @ 2021-05-04 15:36  脂环  阅读(69)  评论(0编辑  收藏  举报