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. my28_mysql内存占用过高降低的方法

    对mysql做压力测试,测试完之后,mysql的内存一直不下降 $ free -m total used free shared buff/cache available Mem: Swap: # t ...

  2. socket阻塞与非阻塞情况下的recv、send、read、write返回值 以及epool_wait返回值学习

    https://www.cnblogs.com/longingforlife/p/3289976.html

  3. 用LaTeX画树形结构

    用LaTeX画树形结构,比如:文件目录树形图,程序中函数调用关系图等. 找到的一个不错的资源: http://www.texample.net/tikz/examples/feature/trees/ ...

  4. PlayMaker 设置颜色 Set Materia lColor

    1. 指定一个游戏对象,Color处设为红色,执行这个行为后就把游戏对象的材质变为了红色: 2. Color也可以使用一个弄好的Color类型的变量,如下图:Color处用了一个提前设置好的变量

  5. Kudu安装(官网推荐的步骤)(installing build Kudu from source)

    不多说,直接上干货! Kudu安装前的建议说明(博主推荐) 这是安装Kudu的另一种方法 Kudu安装(官网推荐的步骤)(installing Kudu using parcels or packag ...

  6. Jetty数据同步使用

    1. Jetty简介 Jetty 是一个开源的servlet容器,它为基于Java的web容器,例如JSP和servlet提供运行环境.Jetty是使用Java语言编写的,它的API以一组JAR包的形 ...

  7. Postman+Newman+Jenkins APItest自动化集成测试

    postman做接口测试简单易用很容易上手,但是接口越来越多,每次手动点击runner进行测试不灵活,最近就研究了下newman,利用newman实现接口的自动化测试,但是每次需要命令行操作也不方便, ...

  8. Android 程序结构介绍

    创建好Android开发环境后,创建一个Android Project, 截图如下:

  9. autofac学习

    Instance Scope 1.instance per dependency    (the default) builder.RegisterType<classes>();等价于 ...

  10. fiddler filters 使用(fiddler只显示指定请求,fiddler不显示指定请求,即filter请求过滤)(转)

    fiddler filters 使用(fiddler只显示指定请求,fiddler不显示指定请求,即filter请求过滤) Fiddler 有一个filters可以很好的帮助我们只显示我们关系的请求或 ...