1. 程式人生 > >Flip and Shift ZOJ - 1028

Flip and Shift ZOJ - 1028

題目連結:QAQ

 

題意:簡單說就是給你一個迴圈的01串,問你若干個步長為二的交換後能否使1都在一起(0都在一起)

 

思路:很簡單的數學題,奇數的話肯定可以,偶數的話只要判斷奇數位上的1和偶數位上的1的數量差就行(就和插空一樣)

 

 

附上程式碼:

#include<stdio.h>
#include<math.h>
#include<algorithm>
using namespace std;
int ax[100];
int main(void) {
	int t;
	scanf("%d", &t);
	while (t--) {
		int n;
		scanf("%d", &n);
		int tot1 = 0;
		int tot2 = 0;
		for (int i = 0; i < n; i++) {
			scanf("%d", &ax[i]);
			if (ax[i] == 1) {
				if (i % 2 == 0) {
					tot1++;
				}
				else
					tot2++;
			}
		}
		if (n % 2 == 1 || n == 3 || n == 1) {
			printf("YES\n");
			continue;
		}
		if (abs(tot2 - tot1) > 1) {
			printf("NO\n");
		}
		else printf("YES\n");
	}
	return 0;
}