Flashing Fluorescents

时间限制: 1 Sec  内存限制: 128 MB
提交: 56  解决: 19
[提交] [状态] [讨论版] [命题人:admin]

题目描述

You have n lights, each with its own button, in a line. Pressing a light’s button will toggle that light’s state; if the light is on, it will turn off, and if the light is off, it will turn on. The lights change at 1 second timesteps. You can press a button at any time, but it will not take effect until the next timestep. Before each timestep, you may choose to push at most one button (you may also choose to not press any button).
Pushing a button will affect not just the light in question, but all lights down the line. More specifically, if you choose to press the ith button right before the kth timestep, then the (i + m)th light will toggle on the (k + m)th timestep (with i + m ≤ n). For example, if you press button 5 just before time 19, then light 5 will toggle at time 19, light 6 will toggle at time 20, light 7 will toggle at time 21, and so on. If you push a button that will take effect at the same time as its light would have toggled due to an earlier button press, then the two cancel each other out, including subsequent toggles.
Suppose there are three lights, all of which are off at the start. If you press the first button before the first timestep, this will happen in three timesteps:

Now, suppose you press the first button before the first timestep, and then the second button between the first and second timesteps. The button press will cancel out the propagation, and this will happen (note that the propagation will go no further):

Now, suppose you press the first button before the first timestep, and then the third button between the first and second timesteps. All three lights will be on at the second timestep (but not the third):

You wish to turn on all the lights. What is the earliest time you could possibly see all of the lights turned on? Note that if the lights are all on at time t but not at time t + 1 due to this propagation, t is still the correct answer.

输入

Each input will consist of a single test case. Note that your program may be run multiple times on different inputs. Each test case will consist of a single string S (1 ≤ |S| ≤ 16). The string S will contain only the characters 1 and 0, where 1 represents that that light is initially on, and 0 represents that that light is initially off. The first character is light 1, the next is light 2, and so on.

输出

Output a single integer, which is the earliest time at which all of the lights are on.

样例输入

1101

样例输出

1

思路:从1开始枚举答案,遍历已经存在的状态,同时加入新的状态,直到所有灯泡变亮,即now=0.

AC代码:

 #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=(<<);
string s;
int n,vis[maxn];
ll cnt,val;
vector<int> v;
int main()
{
cin>>s;
n=s.size();
for(int i=;i<n;i++)
{
if(s[i]=='') val+=(1ll<<i);
}
if(val==)
{
cout<<""<<endl;
return ;
}
vis[val]=;
cnt=(<<n)-;
v.push_back(val);
for(int ans=;;ans++)
{
ll len=v.size();
ll sec=(<<ans)-;
for(int i=;i<len;i++)
{
ll tmp=v[i];
for(int j=;j<=n;j++)
{
ll now=cnt&(tmp^(sec<<j));
if(vis[now]==)
{
vis[now]=;
if(now==)
{
cout<<ans<<endl;
return ;
}
v.push_back(now);
}
}
}
}
return ;
}

Flashing Fluorescents(状压DP)的更多相关文章

  1. 计蒜客 Flashing Fluorescents(状压DP)

    You have nn lights, each with its own button, in a line. Pressing a light’s button will toggle that ...

  2. BZOJ 1087: [SCOI2005]互不侵犯King [状压DP]

    1087: [SCOI2005]互不侵犯King Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 3336  Solved: 1936[Submit][ ...

  3. nefu1109 游戏争霸赛(状压dp)

    题目链接:http://acm.nefu.edu.cn/JudgeOnline/problemShow.php?problem_id=1109 //我们校赛的一个题,状压dp,还在的人用1表示,被淘汰 ...

  4. poj3311 TSP经典状压dp(Traveling Saleman Problem)

    题目链接:http://poj.org/problem?id=3311 题意:一个人到一些地方送披萨,要求找到一条路径能够遍历每一个城市后返回出发点,并且路径距离最短.最后输出最短距离即可.注意:每一 ...

  5. [NOIP2016]愤怒的小鸟 D2 T3 状压DP

    [NOIP2016]愤怒的小鸟 D2 T3 Description Kiana最近沉迷于一款神奇的游戏无法自拔. 简单来说,这款游戏是在一个平面上进行的. 有一架弹弓位于(0,0)处,每次Kiana可 ...

  6. 【BZOJ2073】[POI2004]PRZ 状压DP

    [BZOJ2073][POI2004]PRZ Description 一只队伍在爬山时碰到了雪崩,他们在逃跑时遇到了一座桥,他们要尽快的过桥. 桥已经很旧了, 所以它不能承受太重的东西. 任何时候队伍 ...

  7. bzoj3380: [Usaco2004 Open]Cave Cows 1 洞穴里的牛之一(spfa+状压DP)

    数据最多14个有宝藏的地方,所以可以想到用状压dp 可以先预处理出每个i到j的路径中最小权值的最大值dis[i][j] 本来想用Floyd写,无奈太弱调不出来..后来改用spfa 然后进行dp,这基本 ...

  8. HDU 1074 Doing Homework (状压dp)

    题意:给你N(<=15)个作业,每个作业有最晚提交时间与需要做的时间,每次只能做一个作业,每个作业超出最晚提交时间一天扣一分 求出扣的最小分数,并输出做作业的顺序.如果有多个最小分数一样的话,则 ...

  9. 【BZOJ1688】[Usaco2005 Open]Disease Manangement 疾病管理 状压DP

    [BZOJ1688][Usaco2005 Open]Disease Manangement 疾病管理 Description Alas! A set of D (1 <= D <= 15) ...

随机推荐

  1. Searching the Web论文阅读

    Searching the Web   (Arvind Arasu etc.) 1. 概述 2000年,23%网页每天更新,.com域内网页40%每天更新.网页生存半衰期是10天.描述方法可用Pois ...

  2. The Falling Leaves UVA - 699

    题目链接:https://vjudge.net/problem/UVA-699 题目大意:给一颗二叉树,每个结点都有一个水平位置 :左子节点在它左边的1个单位,右子结点在它右边1个单位.从左向右输出每 ...

  3. vue-cli生成的重要代码详解

    安装好vue-cli之后,我们可以在package.json中看到下面所示: { // 项目名称 "name": "myvue", // 项目版本 " ...

  4. 如何使用rem单位

    最近搞移动端,真是被rem.em与px的换算要了老命了,看了不少文档,似乎弄明白了,这不今天用又蒙圈了. 好多文档上老是说用rem就给html设置font-size,用em就给body设置font-s ...

  5. POJ 3189——Steady Cow Assignment——————【多重匹配、二分枚举区间长度】

     Steady Cow Assignment Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I ...

  6. SpringBoot如何集成Jedis

    添加jedis依赖 在项目pom.xml文件中添加依赖 <!-- 添加jedis依赖 --> <dependency> <groupId>redis.clients ...

  7. sqlmap 命令详解(自备速查)

    sqlmap速查 /pentest/database/sqlmap/txt/ common-columns.txt 字段字典 common-outputs.txt common-tables.txt ...

  8. PHP迭代器 Iterator

    Iterator是PHP自带的迭代器接口. 实现该接口的类必须实现该接口的方法,以便能够使用foreach进行输出迭代后的数据. interface Iterator extends Traversa ...

  9. vue-样式问题

    问题: 今天在用vue开发单页面应用的时候,遇到一个问题,在A页面,直接刷新,页面的布局样式之类的是没有问题的,不过在B页面跳转到A页面,那么A页面有一些样式就不是预期的效果. 发现解决问题: 用调试 ...

  10. js中各种距离clientWidth

    1: offsetLeft.offsetTop.offsetHeight.offsetWidth 1.1 偏移参照——定位父级offsetParent   offsetParent的定义是:与当前元素 ...