计蒜客 Flashing Fluorescents(状压DP)

You have nn 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 i^th button right before the k^th 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.

Input Format

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 SS 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 Format

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

样例输入1

1101

样例输出1

1

样例输入2

1

样例输出2

0

样例输入3

000

样例输出3

2

题意

给你一个01串,1表示亮0表示灭,每分钟可以让一盏灯亮,但下一分钟后一盏灯会改变,下下一分钟后后一盏灯会改变,直到N。问你最少几分钟可以让所有灯亮。

题解

答案ans<=n,要构造全1,那么就是给你若干个01字符串,问你异或和为全1,显然是一定能构造出来的。

f[ans][S]代表第ans分钟是否有子集S。

代码

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3
 4 bool f[20][1<<16];
 5 char a[20];
 6 int main()
 7 {
 8     while(scanf("%s",a)!=EOF)
 9     {
10         int n=strlen(a),i,j,now=0,S=0;
11         memset(f,0,sizeof f);
12         for(int i=0;i<n;i++)if(a[i]==‘0‘)S|=1<<i;
13         f[0][S]=1;
14         while(!f[now][0])
15         {
16             for(int S=0;S<1<<n;S++)f[now+1][S]=f[now][S];
17             for(int i=0;i<n;i++)
18             {
19                 int mask=0;
20                 for(int j=0;j<now+1&&i+j<n;j++)mask|=1<<(i+j);
21                 for(int S=0;S<1<<n;S++)if(f[now][S])f[now+1][S^mask]=1;
22             }
23             now++;
24         }
25         printf("%d\n",now);
26     }
27     return 0;
28 }

原文地址:https://www.cnblogs.com/taozi1115402474/p/11300548.html

时间: 2024-10-09 00:01:04

计蒜客 Flashing Fluorescents(状压DP)的相关文章

计蒜客- AC Challenge 状压dp

题目链接:https://nanti.jisuanke.com/t/30994 Dlsj is competing in a contest with n(0<n≤20)n (0 < n \le 20)n(0<n≤20) problems. And he knows the answer of all of these problems. However, he can submit iii-th problem if and only if he has submitted (and

Flashing Fluorescents(状压DP)

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

计蒜客 Red Black Tree(树形DP)

You are given a rooted tree with n nodes. The nodes are numbered 1..n. The root is node 1, and m of the nodes are colored red, the rest are black. You would like to choose a subset of nodes such that there is no node in your subset which is an ancest

牛客网 Wannafly挑战赛8 C-小C打比赛 (状压DP)

小C现在要参加一场wannafly挑战赛,一场挑战赛一共有n道题,一共有m分钟. 对于第i道题,小C解决它需要恰好j分钟的概率是pi,j. 小C每次会选择某一道没做完的题,然后把它解决(不能中途放弃),之后再决策下一道要做的题是哪道. 求小C在最优策略下,期望能做出几道题. 输入描述: 第一行两个正整数n,m接下来一共n行,每行有m个小数,第i行的第j个小数表示p i,j (这里假设不存在0分钟A题的dalao). 输出描述: 输出一个小数,表示期望能做出几道题,保留小数点后五位. 示例1 输入

计蒜客 无脑博士和他的试管们

无脑博士有三个容量分别是A,B,C升的试管,A,B,C分别是三个从1到20的整数,最初,A和B试管都是空的,而C试管是装满硫酸铜溶液的.有时,无脑博士把硫酸铜溶液从一个试管倒到另一个试管中,直到被灌试管装满或原试管空了.当然每一次灌注都是完全的.由于无脑博士天天这么折腾,早已熟练,溶液在倒的过程中不会有丢失. 写一个程序去帮助无脑博士找出当A是个是空的时候,C试管中硫酸铜溶液所剩量的所有可能性. 输入包括一行,为空格分隔开的三个数,分别为整数A,B和C. 输出包括一行,升序地列出当A试管是空的时

POJ 2411 Mondriaan&#39;s Dream ——状压DP 插头DP

[题目分析] 用1*2的牌铺满n*m的格子. 刚开始用到动规想写一个n*m*2^m,写了半天才知道会有重复的情况. So Sad. 然后想到数据范围这么小,爆搜好了.于是把每一种状态对应的转移都搜了出来. 加了点优(gou)化(pi),然后poj上1244ms垫底. 大概的方法就是考虑每一层横着放的情况,剩下的必须竖起来的情况到下一层取反即可. 然后看了 <插头DP-从入门到跳楼> 这篇博客,怒抄插头DP 然后16ms了,自己慢慢YY了一下,写出了风(gou)流(pi)倜(bu)傥(tong)

JZOJ 3747(状压DP)

这题是一道状压DP,暂时还不是很懂. #include<cstdio> #include<cstring> #include<iostream> using namespace std; const int LENM=1010,N=11,M=1e9+7; char s[N]; int n,m,u,ind[200],f[LENM][1<<10],g[N],ans[N],gg[N],num1[1<<10],ff[1<<10][5]; in

【vijos】1286 座位安排(状压dp)

https://vijos.org/p/1286 这题好神不会捉... 首先我们知道所有情况有C(n*m, k)种,这个好搞.但是两两不相邻这个有点难搞.. 原来是状压dp..sigh. 设状态f[i][j][k]表示第i行放置的摆放状态是j放了k个人的方案,那么有 f[i][j][k]=sum{f[i-1][x][k-num[x]]},当j的人两两不冲突,且j和x的人两两不冲突,num[x]是x状态的人的数量 最后不冲突的数目是sum{f[n][j][k]},k是题目所给的,j是所有的状态 显

P1879 [USACO06NOV]玉米田Corn Fields 状压dp

链接在此! 正解:状压dp(emm……据说插头dp也可以趴但我不管!!!不会!!! 解题报告: ……我真的太菜了……我以为一个小时前要搞完的题目调错误调了一个小时……90分到100我差不多搞了一个小时…… 然后这题还是做过的……就很气,觉得确实是要搞下博客没事儿复习下不然做过的题目还花俩小时我真的哭死…… 先放上错误的90分代码讲一下错哪儿了(因为……其实100并不难是可以想到的……没有太大讲的意义,主要我太菜了所以才会搞这么久TT 点我♂看♂沙雕灵巧在线WA题 然后错误的点是最后一个点RE,开