[CF2147C]Rabbits题解

time limit per test

2 seconds

memory limit per test

256 megabytes

You have n flower pots arranged in a line numbered from 1 to n left to right. Some of the pots contain flowers, while others are empty. You are given a binary string s describing which pots contain flowers (si=1) and which are empty (si=0). You also have some rabbits, and you want to take a nice picture of rabbits and flowers. You want to put rabbits in every empty pot (si=0), and for each rabbit, you can put it looking either to the left or to the right. Unfortunately, the rabbits are quite naughty, and they will try to jump, which will ruin the picture.

Each rabbit will prepare to jump into the next pot in the direction they are looking, but they won't jump if there is a rabbit in that pot already or if there is another rabbit that prepares to jump into the same pot from the opposite side. Rabbits won't jump out of the borders (a rabbit at pot 1 looking to the left won't jump, same for a rabbit looking to the right at pot n).

Your goal is to choose the directions of the rabbits so that they never jump, allowing you to take your time to take the picture. You need to determine if there is a valid arrangement of rabbits such that no rabbit ever jumps.

有道 翻译

将 n 花盆从左到右排列成一排,编号从 1 到 n 。有些花盆里有花,有些则是空的。你得到一个二进制字符串 s ,描述哪些花盆里有花( si=1 ),哪些是空的( si=0 )。你也有一些兔子,你想给兔子和花拍一张漂亮的照片。你想把兔子放在每个空罐子里( si=0 ),对于每只兔子,你可以让它向左或向右看。不幸的是,兔子很淘气,他们会试图跳,这会破坏图片。

每只兔子都会准备跳到它们所看到的方向的下一个罐子里,但如果那个罐子里已经有一只兔子,或者有另一只兔子准备从相反的方向跳进同一个罐子里,它们就不会跳了。兔子不会跳出边界(向左看 1 的兔子不会跳出边界,向右看 n 的兔子也不会跳出边界)。

你的目标是选择兔子的方向,使它们永远不会跳跃,让你有时间拍照。你需要确定是否有兔子的有效排列,这样就不会有兔子跳跃。

Input

Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤104). The description of the test cases follows.

The first line of each test case contains an integer n (1≤n≤2⋅105).

The second line contains a binary string s of size n, denoting the occupied and empty pots.

It is guaranteed that the sum of n over all test cases does not exceed 2⋅105.

有道 翻译

输入** **

每个测试包含多个测试用例。第一行包含测试用例的数量 t ( 1≤t≤104 )。下面是测试用例的描述。

每个测试用例的第一行包含一个整数 n ( 1≤n≤2⋅105 )。

第二行包含一个大小为 n 的二进制字符串 s ,表示已占用的和空的容器。

保证所有测试用例 n 的和不超过 2⋅105 。

Output

For each test case, print "YES" if there exists a configuration of rabbits that satisfies the condition, and "NO" otherwise.

You can output the answer in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses.

有道 翻译

** **输出

对于每个测试用例,如果存在满足条件的兔子配置,则打印“YES”,否则打印“NO”。

您可以在任何情况下输出答案(上或下)。例如,字符串“yEs”、“yEs”、“yEs”和“yEs”将被识别为积极响应。

Example

Input

Copy

 

12

4

0100

3

000

8

11011011

5

00100

1

1

5

01011

2

01

7

0101011

7

1101010

5

11001

4

1101

9

001101100

Output

Copy

 

YES

YES

NO

YES

YES

YES

YES

YES

YES

YES

NO

NO

Note

Visualizer link

In the first test case, one of the valid configurations is to put a rabbit looking to the right at position 1, a rabbit looking to the left at position 3, and a rabbit looking to the left at position 4. No rabbit will move since:

  • The rabbit at pot 1 won't move to pot 2 since the rabbit at pot 3 is looking to the left.
  • The rabbit at pot 3 won't move to pot 2 since the rabbit at pot 1 is looking to the right.
  • The rabbit at pot 4 won't move to pot 3 since there is a rabbit in there.

In the second test case, one of the valid configurations is to put a rabbit looking to the left at position 1, a rabbit looking to the right at position 2, and a rabbit looking to the left at position 3. No rabbit will move since:

  • The rabbit at pot 1 won't move since it is looking at the left border.
  • The rabbit at pot 2 won't move to pot 3 since there is a rabbit in there.
  • The rabbit at pot 3 won't move to pot 2 since there is a rabbit in there.

It can be proven that there is no valid arrangement of rabbits in the third test case.

有道 翻译

注意

(可视化工具链接)(https://codeforces.com/assets/contests/2147/C_2iM9A1dE03B4IrDfFG54.html)

在第一个测试用例中,有效的配置之一是在位置 1 上放一只向右看的兔子,在位置 3 上放一只向左看的兔子,在位置 4 上放一只向左看的兔子。没有兔子会动,因为:

  • 1 的兔子不会移动到 2 ,因为 3 的兔子是向左看的。

-锅 3 上的兔子不会移动到锅 2 上,因为锅 1 上的兔子是向右看的。

-锅 4 的兔子不会移动到锅 3 ,因为那里有一只兔子。

! [] (https://espresso.codeforces.com/774410cf1eab1d16906b6c1e48e52d02a889b494.png)

在第二个测试用例中,有效的配置之一是在位置 1 上放一只向左看的兔子,在位置 2 上放一只向右看的兔子,在位置 3 上放一只向左看的兔子。没有兔子会动,因为:

-罐子 1 上的兔子不会移动,因为它在看左边的边界。

  • 2 的兔子不会移动到 3 ,因为那里有一只兔子。

-锅 3 的兔子不会移动到锅 2 ,因为那里有一只兔子。

! [] (https://espresso.codeforces.com/95e25f6cef45ea02d1213238095422d1fbdd6b28.png)

可以证明在第三个测试用例中不存在兔子的有效排列。

思路

找出规律即可。

代码见下

#include<bits/stdc++.h>
using namespace std;
long long t,n,b[200005],c[200005],ks[200005],lk=0,tt[200005],ww[200005];
char a[200005];
int main(){
	cin>>t;
	while(t--){
		cin>>n;
		for(int i=1;i<=n;i++){
			cin>>a[i];
			ks[i]=0;
		}
		a[0]=a[n+1]=a[n+2]='1';
		ks[n+1]=ks[n+2]=0;
		b[0]=0;
		tt[0]=0;
		for(int i=2;i<=n-1;i++){
			if(a[i]=='0'&&a[i-1]=='1'&&a[i+1]=='1'){
				b[++b[0]]=i;
				ks[i]=1;
			}
		}
		for(int i=1;i<=b[0];i++){
			//cout<<b[i]<<" ";
			if(i==1){
				if(b[0]!=1&&b[i]==b[i+1]-2){
					tt[++tt[0]]=b[i];
				}
				else{
					tt[++tt[0]]=b[i];
					ww[tt[0]]=b[i];
				}
			}	
			else if(i==b[0]){
				if(b[i]==b[i-1]+2){
					ww[tt[0]]=b[i];
				}
				else{
					tt[++tt[0]]=b[i];
					ww[tt[0]]=b[i];					
				}
			}
			else if(b[i]!=b[i-1]+2&&b[i]==b[i+1]-2){
				tt[++tt[0]]=b[i];
			} 	
			else if(b[i]!=b[i+1]-2&&b[i]==b[i-1]+2){
				ww[tt[0]]=b[i];
			}
			else if(b[i]!=b[i+1]-2&&b[i]!=b[i-1]+2){
				tt[++tt[0]]=b[i];
				ww[tt[0]]=b[i];
			}				
		}
		c[0]=0;
		for(int i=1;i<=n;i++){
			if((i==1||i==n||(a[i]==a[i+1]&&a[i]!=a[i-1])||(a[i]==a[i-1]&&a[i]!=a[i+1]))&&a[i]=='0'){
				c[++c[0]]=i;
				ks[i]=2;
			}
		}
		lk=0;
		//cout<<tt[0]<<endl;
		for(int i=1;i<=tt[0];i++){
			//cout<<ww[i]<<" "<<tt[i]<<endl;
			if(((ww[i]-tt[i])/2+1)%2==1){
				//cout<<a[tt[i]-2]<<endl;
				if(a[tt[i]-2]=='1'&&a[ww[i]+2]=='1'){
					lk=1;
					break;
				}
				else if(a[tt[i]-2]=='1'){
					a[ww[i]+2]='5';
				}
			}
		}
		if(lk==0){
			cout<<"YES"<<endl;
		}
		else{
			cout<<"NO"<<endl;
		}
	}
	return 0;
}

posted @ 2025-10-16 10:48  bz02_2023f2  阅读(7)  评论(0)    收藏  举报  来源