Internet of Lights and Switches(MAP记录+二分) 2015年湖南省赛第 I 题

2024-05-12 20:18

本文主要是介绍Internet of Lights and Switches(MAP记录+二分) 2015年湖南省赛第 I 题,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

题目描述

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).

blob.png

Initially, all the lights are on. Your task is to count the number of ways to turn off all the lights by pressing some consecutive 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).


输入描述

There will be at most 20 test cases. Each test case begins with a line containing four integers n, m, a, b (2<=n<=50, 1<=a<=b<=m<=300000). Each of the following m lines contains a 01 string of length n. The i-th character is 1 if and only if that switch controls the i-th light. The size of the whole input file does not exceed 8MB.


输出描述

For each test case, print the case number, and the number of ways to turn off all the lights.


输入样例
2 4 1 4
01
10
11
00
2 4 3 3
01
10
11
00
6 3 1 3
101001
010110
101001

输出样例

Case 1: 3 Case 2: 0 Case 3: 2

题意:有n个灯最初是亮的,现在要通过连续的操作使得灯全灭,现在m个操作,可以连续的操作个数为a~b,问有多少种情况使得灯全灭。

解题:用map<long long , vector<int> >记录每种灯的状态所对应第 i 个操作(第i个操作的状态为:输入的前i个操作的异或状态) + 二分.

#include<stdio.h>
#include<string.h>
#include<string>
#include<map>
#include<vector>
using namespace std;
const int N = 300010 ;
#define ll long long
ll num[N] ;
map<ll, vector<int> >mp;
void two(ll tnum , int n , int& L , int& R )
{int l=0,r=mp[tnum].size()-1 , mid;while(l<=r){mid = (l+r)>>1;if(mp[tnum][mid]>=L)r = mid - 1 ;elsel = mid + 1;}L  = l ;if(L==mp[tnum].size()){L = -1 ;R =  L - 1 ;return ;}l = 0 , r = mp[tnum].size()-1 ;while(l<=r){mid = (l+r)>>1;if(mp[tnum][mid]>=R)r = mid - 1 ;elsel = mid + 1;}if(l==mp[tnum].size())R = l-1;else if(mp[tnum][l]>R)R = l-1 ;elseR = l ;
}int main()
{int n,m,a,b , ans , T = 0;char s[100] ;while(scanf("%d%d%d%d",&n,&m,&a,&b)>0){mp[0].push_back(0);num[0]=0;for(int i=1; i<=m; i++){scanf("%s",s);num[i] = 0 ;for(int j=0; j<n; j++)if(s[j]=='1'&& !(num[i-1]&(1LL<<j)) || s[j]=='0'&&(num[i-1]&(1LL<<j))==(1LL<<j))num[i] |= 1LL<<j ;mp[ num[i] ].push_back(i) ;// printf("size = %d\n",mp[num[i]].size());}ans = 0 ;for(int i=1; i<=m; i++){ll tnum = 0 ;for(int j=0; j<n; j++)if((num[i]&(1LL<<j))==0)tnum |= 1LL<<j;int L = i-b , R = i-a ;two(tnum , n , L , R );if(R>=L)ans += R-L+1 ;// printf("[ %d , %d ] num = %lld ,tnum = %lld size = %d\n",L,R,num[i],tnum,mp[tnum].size());}for(int i=0; i<=m; i++)mp[num[i]].clear() ;printf("Case %d: %d\n",++T , ans ) ;}
}

这篇关于Internet of Lights and Switches(MAP记录+二分) 2015年湖南省赛第 I 题的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/983635

相关文章

Java中Map.Entry()含义及方法使用代码

《Java中Map.Entry()含义及方法使用代码》:本文主要介绍Java中Map.Entry()含义及方法使用的相关资料,Map.Entry是Java中Map的静态内部接口,用于表示键值对,其... 目录前言 Map.Entry作用核心方法常见使用场景1. 遍历 Map 的所有键值对2. 直接修改 Ma

统一返回JsonResult踩坑的记录

《统一返回JsonResult踩坑的记录》:本文主要介绍统一返回JsonResult踩坑的记录,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录统一返回jsonResult踩坑定义了一个统一返回类在使用时,JsonResult没有get/set方法时响应总结统一返回

Go学习记录之runtime包深入解析

《Go学习记录之runtime包深入解析》Go语言runtime包管理运行时环境,涵盖goroutine调度、内存分配、垃圾回收、类型信息等核心功能,:本文主要介绍Go学习记录之runtime包的... 目录前言:一、runtime包内容学习1、作用:① Goroutine和并发控制:② 垃圾回收:③ 栈和

java对接海康摄像头的完整步骤记录

《java对接海康摄像头的完整步骤记录》在Java中调用海康威视摄像头通常需要使用海康威视提供的SDK,下面这篇文章主要给大家介绍了关于java对接海康摄像头的完整步骤,文中通过代码介绍的非常详细,需... 目录一、开发环境准备二、实现Java调用设备接口(一)加载动态链接库(二)结构体、接口重定义1.类型

Java中JSON格式反序列化为Map且保证存取顺序一致的问题

《Java中JSON格式反序列化为Map且保证存取顺序一致的问题》:本文主要介绍Java中JSON格式反序列化为Map且保证存取顺序一致的问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未... 目录背景问题解决方法总结背景做项目涉及两个微服务之间传数据时,需要提供方将Map类型的数据序列化为co

apache的commons-pool2原理与使用实践记录

《apache的commons-pool2原理与使用实践记录》ApacheCommonsPool2是一个高效的对象池化框架,通过复用昂贵资源(如数据库连接、线程、网络连接)优化系统性能,这篇文章主... 目录一、核心原理与组件二、使用步骤详解(以数据库连接池为例)三、高级配置与优化四、典型应用场景五、注意事

SpringBoot实现文件记录日志及日志文件自动归档和压缩

《SpringBoot实现文件记录日志及日志文件自动归档和压缩》Logback是Java日志框架,通过Logger收集日志并经Appender输出至控制台、文件等,SpringBoot配置logbac... 目录1、什么是Logback2、SpringBoot实现文件记录日志,日志文件自动归档和压缩2.1、

qtcreater配置opencv遇到的坑及实践记录

《qtcreater配置opencv遇到的坑及实践记录》我配置opencv不管是按照网上的教程还是deepseek发现都有些问题,下面是我的配置方法以及实践成功的心得,感兴趣的朋友跟随小编一起看看吧... 目录电脑环境下载环境变量配置qmake加入外部库测试配置我配置opencv不管是按照网上的教程还是de

使用nohup和--remove-source-files在后台运行rsync并记录日志方式

《使用nohup和--remove-source-files在后台运行rsync并记录日志方式》:本文主要介绍使用nohup和--remove-source-files在后台运行rsync并记录日... 目录一、什么是 --remove-source-files?二、示例命令三、命令详解1. nohup2.

Java使用Stream流的Lambda语法进行List转Map的操作方式

《Java使用Stream流的Lambda语法进行List转Map的操作方式》:本文主要介绍Java使用Stream流的Lambda语法进行List转Map的操作方式,具有很好的参考价值,希望对大... 目录背景Stream流的Lambda语法应用实例1、定义要操作的UserDto2、ListChina编程转成M