博客
关于我
HDU 5600 N bulbs (BestCoder Round #67 (div.2))
阅读量:633 次
发布时间:2019-03-14

本文共 1996 字,大约阅读时间需要 6 分钟。

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.

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

Sample Input
1
5
1 0 0 0 0
Sample Output
YES

Hint

Child’s path is: 123234545
all switchs are touched twice except the first one.

题意:题中给一段0和1组成的序列表示灯的初始状态,有个熊孩子他想从第一个灯泡走到最后一个灯泡,然后离开,在这过程中他可以走到任意一个相邻的灯泡上,可以折返,步数不限,每经过一个灯泡,熊孩子会按下灯泡开关,问你他有没有可能把这段灯泡都关闭。

解题思路: 如果经过1(灯泡开着)则直接走过,如果经过0,则分两种情况,第一种,如果后一个还是 0,则走到下一个,再折返回来,再到下一个时,两个灯泡还是0 0状态,符合题意;第二种,如果后一个是 1 ,则用一个变量记录这种情况出现的次数,通过例子的实验,这种情况如果是偶数次,则通过折返法就能使灯泡都关掉,否则无论怎么走都不可能使全部为 0 。

结论:只要 0 连续出现的偶数段都能用折返法使其为 0 不变,0 连续出现的奇数段的数量为偶数则可以通过0 1的折返转递最终都消为 0 。所以只要计算出 0 连续出现的奇数段的数量判段是不是偶数就可得出答案。

#include
int a[1000005];int main(){ int t,i,j,n; scanf("%d",&t); while(t--) { scanf("%d",&n); for(i=0; i

转载地址:http://bcxoz.baihongyu.com/

你可能感兴趣的文章
mysql 查看锁_阿里/美团/字节面试官必问的Mysql锁机制,你真的明白吗
查看>>
MySql 查询以逗号分隔的字符串的方法(正则)
查看>>
MySQL 查询优化:提速查询效率的13大秘籍(避免使用SELECT 、分页查询的优化、合理使用连接、子查询的优化)(上)
查看>>
mysql 查询数据库所有表的字段信息
查看>>
【Java基础】什么是面向对象?
查看>>
mysql 查询,正数降序排序,负数升序排序
查看>>
MySQL 树形结构 根据指定节点 获取其下属的所有子节点(包含路径上的枝干节点和叶子节点)...
查看>>
mysql 死锁 Deadlock found when trying to get lock; try restarting transaction
查看>>
mysql 死锁(先delete 后insert)日志分析
查看>>
MySQL 死锁了,怎么办?
查看>>
MySQL 深度分页性能急剧下降,该如何优化?
查看>>
MySQL 深度分页性能急剧下降,该如何优化?
查看>>
MySQL 添加列,修改列,删除列
查看>>
mysql 添加索引
查看>>
MySQL 添加索引,删除索引及其用法
查看>>
mysql 状态检查,备份,修复
查看>>
MySQL 用 limit 为什么会影响性能?
查看>>
MySQL 用 limit 为什么会影响性能?有什么优化方案?
查看>>
MySQL 用户权限管理:授权、撤销、密码更新和用户删除(图文解析)
查看>>
mysql 用户管理和权限设置
查看>>