N bulbs
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 85 Accepted Submission(s): 46
Problem Description
N bulbs are in a row from left to right,some are on, and some are off.The first bulb is the most left one. And the last one is the most right one.they are numbered from 1 to n,from left to right.
in order to save electricity, you should turn off all the lights, but you're lazy.
coincidentally,a passing bear children paper(bear children paper means the naughty boy), who want to pass here from the first light bulb to the last one and leave.
he starts from the first light and just can get to the adjacent one at one step.
But after all,the bear children paper is just a bear children paper. after leaving a light bulb to the next one, he must touch the switch, which will change the status of the light.
your task is answer whether it's possible or not to finishing turning off all the lights, and make bear children paper also reach the last light bulb and then leave at the same time.
in order to save electricity, you should turn off all the lights, but you're lazy.
coincidentally,a passing bear children paper(bear children paper means the naughty boy), who want to pass here from the first light bulb to the last one and leave.
he starts from the first light and just can get to the adjacent one at one step.
But after all,the bear children paper is just a bear children paper. after leaving a light bulb to the next one, he must touch the switch, which will change the status of the light.
your task is answer whether it's possible or not to finishing turning off all the lights, and make bear children paper also reach the last light bulb and then leave at the same time.
Input
The first line of the input file contains an integer T, which indicates the number of test cases.
For each test case, there are 2 lines.
The first line of each test case contains 1 integers n.
In the following line contains a 01 sequence, 0 means off and 1 means on.
* 1≤T≤10
* 1≤N≤1000000
For each test case, there are 2 lines.
The first line of each test case contains 1 integers n.
In the following line contains a 01 sequence, 0 means off and 1 means on.
* 1≤T≤10
* 1≤N≤1000000
Output
There should be exactly T lines in the output file.
The i-th line should only contain "YES" or "NO" to answer if it's possible to finish.
The i-th line should only contain "YES" or "NO" to answer if it's possible to finish.
Sample Input
1 5 1 0 0 0 0
Sample Output
YESHintChild's path is: 123234545 all switchs are touched twice except the first one.
题意: 有n个灯,从左到右排成一排。1表示开,0表示关。一个小孩子从左端出发,每次经过一盏灯都会按一下开关(亮的变灭了,灭的变亮了),每次可以走到左边或右边相邻的那盏灯下。 问当孩子从右端离开时是否能将所有灯熄灭(所有数字变为0)。
题解:多列举几种情况就会知道当0的个数为偶数时,可以在右端离开时熄灭所有的灯。
代码如下:
#include<cstdio>
int a[1000010];
int main()
{
int n,t,i;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(i=0;i<n;++i)
scanf("%d",&a[i]);
int cnt=0;
for(i=0;i<n;++i)
{
if(!a[i])
cnt++;
}
if(cnt&1)
printf("NO\n");
else
printf("YES\n");
}
return 0;
}