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. Vuejs 实现权限管理

    程序运行时,router只配置登陆 首页404 等基本页面 import Main from '@/views/Main.vue'; // 不作为Main组件的子页面展示的页面单独写,如下 expor ...

  2. 几种经过整理的文件上传压缩和前台js压缩的方法

    /** * 图片压缩上传 * @param $im,图片资源 * @param int $maxwidth,最大宽度,超过这个宽度则进行压缩 * @param int $maxheight,最大高度, ...

  3. Java日志组件1---Jdk自带Logger(java.util.logging.Logger)

    最近在看日志的一些东西,发现利用JDK自带的log也可以简单的实现日志的输出,将日志写入文件的过程记录如下: 1.新建LogUtil.Java( 里面写了几个静态方法,为log设置等级.添加log控制 ...

  4. swing线程机制

    在介绍swing线程机制之前,先介绍一些背景概念. 背景概念 同步与异步:     同步是指程序在发起请求后开始处理事件并等待处理的结果或等待请求执行完毕,在此之前程序被阻塞(block)直到请求完成 ...

  5. pat05-图2. Saving James Bond - Easy Version (25)

    05-图2. Saving James Bond - Easy Version (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作 ...

  6. 使用cookie的注意事项

    一.什么是cookie? Cookie(复数形态Cookies),中文名称为小型文本文件或小甜饼[1],指某些网站为了辨别用户身份而储存在用户本地终端(Client Side)上的数据(通常经过加密) ...

  7. Fedora 安装 phpMyAdmin(可能只有自己看得懂)

    yum groupinstall "Development Tools" yum install php-mysqli php-mbstring php-mcrypt yum in ...

  8. HDU 5353—— Average——————【贪心+枚举】

    Average Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total S ...

  9. js删除数组里指定的元素

    js删除数组里指定的元素 首先可以给JS的数组对象定义一个函数,用于查找指定的元素在数组中的位置,即索引,代码为: Array.prototype.indexOf = function(val) { ...

  10. 通用代码——makefile文件

    ver=debug ifeq ($(ver),debug) TARGET = testmain_d FLAG=-g -D debug else TARGET = testmain_r FLAG=-O3 ...