15年湖南ACM省赛 Internet of Lights and Switches(异或运算+状压+二分)
来源:互联网 发布:电气绘图软件 编辑:程序博客网 时间:2024/05/01 15:22
Description
You are a fan of "Internet of Things"(IoT, 物联网), so you build a nice Internet of Lights and Switches in your huge mansion. Formally, there are n lights and m switches, each switch controls one or more lights, i.e. pressing that switch flips the status of those lights (on->off, off->on).
Initially, all the lights are on. Your task is to count the number of ways to turn off all the lights by pressing someconsecutive switches. Each switch should not be pressed more than once. There is only one restriction: the number of switches you pressed should be between a and b (inclusive).
Input
Output
Sample Input
2 4 1 401 10 11 00 2 4 3 301 10 11 006 3 1 3101001010110101001
Sample Output
Case 1: 3Case 2: 0Case 3: 2
题意:有n个灯泡,一开始全是开着的,现在给你m个开关用一个长为n的字符串表示,第i个字符为0表示开关不能控制灯泡i,第i个字符
为1表示开关能控制灯泡i,每个开关只能使用一次,现在要求只能按长度>=a且<=b连续相邻的开关,问有多少种方法关掉所有灯泡
题解:用二进制表示开关的开关状态:1是开,0是关.初始状态就是sum=(1<<n)-1,我们可以想到,如果用异或前缀和sz[]表示从第1个
开关按到第n个开关时,灯泡的状态,那么如果要按区间[st,ed]内的开关来关掉所有灯泡,那么按下第ed个开关时的状态一定要等于
按下第st-1个开关时的状态^sum,因为这样按下[st,ed]内的开关可以使所有灯泡的暗亮状态反转,自然也可以让初始状态反转成0,
于是我们可以用map储存状态t和每次出现状态t时候按下的开关的编号,设按下第i个开关的状态是t,二分查找区间[i-a+1,i-b+1]
内状态为t^sum的个数,全部加起来就是方法数
#include<cstdio>#include<iostream>#include<string.h>#include<algorithm>#include<map>#include<queue>#include<vector>using namespace std;typedef long long LL;int n, m, a, b;LL sz[333333]; //sz储存异或前缀和int main() { int T = 1; char s[100]; // freopen("in.txt", "r", stdin); while(~scanf("%d%d%d%d", &n, &m, &a, &b)) { LL sum = (1ll << n) - 1; map<LL, vector<int> > my; //first储存当前开关状态,second储存这个状态分别出现在第几个开关 my[sum].push_back(0); sz[0] = sum; for(int i = 1; i <= m; i++) { scanf("%s", s); LL cnt = 0; for(int j = 0; j < n; j++) if(s[j] == '1') cnt += (1ll << j); sz[i] = cnt ^ sz[i - 1]; my[sz[i]].push_back(i); } int cnt = 0; for(int i = a; i <= m; i++) { LL t = sum ^ sz[i]; if(!my[t].size()) continue; int l = lower_bound(my[t].begin(), my[t].end(), i - b) - my[t].begin();//查找出现状态t的最开始的位置 int r = upper_bound(my[t].begin(), my[t].end(), i - a) - my[t].begin();//查找出现状态t的最后位置+1 int tt = r - l; //出现状态t的次数 cnt += tt; } printf("Case %d: %d\n", T++, cnt); } return 0;}
- 15年湖南ACM省赛 Internet of Lights and Switches(异或运算+状压+二分)
- Internet of Lights and Switches 湖南2015省赛I题 (异或+map)
- [NBUT 1646 Internet of Lights and Switches] 前缀异或和+二分
- uva Internet of Lights and Switches (异或运算+状态压缩)
- 第十一届湖南省省赛 - Internet of Lights and Switches(二分 + 位运算)
- Internet of Lights and Switches(MAP记录+二分) 2015年湖南省赛第 I 题
- [2015-I]-Internet of Lights and Switches
- Internet of Lights and Switches湖南省第十一届大学生计算机程序设计竞赛
- CSU 1784 Internet of Lights and Switches(状态压缩+组合计数)
- osql and sqlcmd switches
- leaf and spine switches
- 二分图一•二分图判定+异或运算
- 异或高斯消元+暴搜 lights 燈
- UVAlive 7487 Association of Cats and Magical Lights(线段树)
- What Kind of Switches and Patch Cables Should I Choose for SFP Transceiver?
- Analytics and the ‘Internet of Things’
- High Performance Switches and Routers
- MySQL and Linux Context Switches
- 面试小记
- JZOJ[8.14比赛]题解
- AndroidUI控件-AlertDialog弹窗控件
- 取得系统图片地址 android
- HDOJ 1374 The Circumference of the Circle (求外接圆)
- 15年湖南ACM省赛 Internet of Lights and Switches(异或运算+状压+二分)
- NOip2000税收与补贴问题
- imageNamed和imageWithFile
- BIND中基数树的建立
- 工作中小困难解决集合
- 2016.8.15 C组总结
- 入浴剂成分
- 记录androidStudio2.0+版本一些目前已在用的有效且常用的快捷键
- 欢迎使用CSDN-markdown编辑器