Flip and Shift

本文介绍了一种基于黑白盘的谜题,通过分析盘子的位置变化规律,提出了判断谜题是否可解的算法。该算法考虑了盘子总数的奇偶性及黑白盘分布的特点。

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

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

题意:给你一个黑白的盘组成的环,盘i可以和i-2或i+2交换位置,求能否使得最后分为黑白两条带

思路:很明显i可以可i-2和i+2交换,也就是说交换后i的奇偶性并为改变,而目标是使得黑白的都是奇偶相连

则有当n(黑白盘的总数)奇数时,可以发现任何奇偶的位置都是可以交换的,所以输出YES

例如:n=8;从1开始1->>3->>5->>8->>2

而当n为偶数时,可以想像当以黑白排一排时是奇偶相间的,也就是说黑白盘的奇偶数的差值在不大于1的情况下是可以满足的,

也就是说假设黑盘数为偶数,则要要求它位置的奇偶数是相等的,而当黑盘数为奇数时则要求奇偶数的差的绝对值为1则可

#include <iostream>
#include <stdio.h>
using namespace std;
int main(){
	int t;
	scanf("%d", &t);
	while(t--){
		int n;
		scanf("%d", &n);
		int s1=0,s2=0;
		int a; 
		for(int i = 1; i <= n; i++){
			scanf("%d",&a);
			if(n%2==1)//奇数直接一直跳过就行 
				continue;
			if(a==0 && i%2==1)//记录白色的盘的位置(奇数的个数) 
				s1 ++;
			else if(a==0 && i%2==0)//记录白色的盘的位置(偶数的个数) 
				s2 ++;
		}
		if(n%2==1){
			printf("YES\n");
			continue;
		}
		if(s1-s2<=1 && s1-s2>=-1)
			printf("YES\n");
		else
			printf("NO\n");
	}
	return 0;
}

 

### Flip-Flops in Digital Electronics Flip-flops are fundamental components in digital electronics, serving as the basic building blocks for sequential logic circuits. They are used to store binary data and are essential in the design of registers, counters, and memory units. Flip-flops are bistable multivibrators, meaning they have two stable states, which can be used to represent binary data (0 or 1). ### Types of Flip-Flops There are several types of flip-flops, each with its own characteristics and applications: - **SR Flip-Flop (Set-Reset)**: This is one of the simplest types of flip-flops. It has two inputs, Set (S) and Reset (R), and two outputs, Q and Q'. The SR flip-flop can be constructed using NAND or NOR gates. It has three possible input combinations: set, reset, and hold. However, the SR flip-flop has an undefined state when both S and R are active simultaneously[^1]. - **D Flip-Flop (Data or Delay)**: The D flip-flop is widely used in digital systems for data storage. It has a single data input (D), a clock input (CLK), and outputs Q and Q'. The D flip-flop captures the value of the D input at the rising or falling edge of the clock signal and holds it until the next clock edge. This makes it ideal for use in shift registers and data storage applications. - **JK Flip-Flop**: The JK flip-flop is an enhanced version of the SR flip-flop that eliminates the undefined state. It has two inputs, J and K, which correspond to the Set and Reset functions. When both J and K are high, the flip-flop toggles its state on the clock edge. This feature allows the JK flip-flop to be used in a wide range of applications, including counters and shift registers. - **T Flip-Flop (Toggle)**: The T flip-flop is a specialized type of JK flip-flop where the J and K inputs are tied together. It has a single input (T), and when T is high, the flip-flop toggles its state on the clock edge. The T flip-flop is commonly used in binary counters. ### Applications of Flip-Flops Flip-flops are used in various applications within digital electronics: - **Registers**: Flip-flops are used to build registers, which are used to store multiple bits of data. A register is typically composed of a series of D flip-flops connected in parallel, with a common clock signal controlling the storage of data. - **Counters**: Flip-flops are used to construct counters, which count the number of clock pulses or events. Counters can be designed to count up, count down, or follow a specific sequence. Binary counters, ring counters, and Johnson counters are examples of counters built using flip-flops. - **Memory Units**: In larger systems, flip-flops are used as the basic storage elements in memory units. They form the basis of static random-access memory (SRAM), where each memory cell consists of several transistors configured as flip-flops. - **Synchronization**: Flip-flops are used to synchronize signals between different clock domains. As mentioned earlier, when signals are passed between different clock domains, metastability can occur. To mitigate this issue, synchronizing flip-flops are used to ensure that the signals are properly aligned with the destination clock domain. - **State Machines**: Flip-flops are used to implement finite state machines (FSMs), which are used to control the operation of digital systems. FSMs use flip-flops to store the current state and transition to the next state based on input conditions. In summary, flip-flops are crucial in the design and implementation of digital circuits, providing the necessary functionality for data storage, synchronization, and control in a variety of digital systems. ```verilog // Example of a D Flip-Flop in Verilog module d_flip_flop ( input clk, input d, output reg q ); always @(posedge clk) begin q <= d; end endmodule ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值