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

相关文章

Python中4大日志记录库比较的终极PK

《Python中4大日志记录库比较的终极PK》日志记录框架是一种工具,可帮助您标准化应用程序中的日志记录过程,:本文主要介绍Python中4大日志记录库比较的相关资料,文中通过代码介绍的非常详细,... 目录一、logging库1、优点2、缺点二、LogAid库三、Loguru库四、Structlogphp

Java Map排序如何按照值按照键排序

《JavaMap排序如何按照值按照键排序》该文章主要介绍Java中三种Map(HashMap、LinkedHashMap、TreeMap)的默认排序行为及实现按键排序和按值排序的方法,每种方法结合实... 目录一、先理清 3 种 Map 的默认排序行为二、按「键」排序的实现方式1. 方式 1:用 TreeM

docker编写java的jar完整步骤记录

《docker编写java的jar完整步骤记录》在平常的开发工作中,我们经常需要部署项目,开发测试完成后,最关键的一步就是部署,:本文主要介绍docker编写java的jar的相关资料,文中通过代... 目录all-docker/生成Docker打包部署文件配置服务A的Dockerfile (a/Docke

MySQL使用EXISTS检查记录是否存在的详细过程

《MySQL使用EXISTS检查记录是否存在的详细过程》EXISTS是SQL中用于检查子查询是否返回至少一条记录的运算符,它通常用于测试是否存在满足特定条件的记录,从而在主查询中进行相应操作,本文给大... 目录基本语法示例数据库和表结构1. 使用 EXISTS 在 SELECT 语句中2. 使用 EXIS

深入解析C++ 中std::map内存管理

《深入解析C++中std::map内存管理》文章详解C++std::map内存管理,指出clear()仅删除元素可能不释放底层内存,建议用swap()与空map交换以彻底释放,针对指针类型需手动de... 目录1️、基本清空std::map2️、使用 swap 彻底释放内存3️、map 中存储指针类型的对象

基于Spring Boot 的小区人脸识别与出入记录管理系统功能

《基于SpringBoot的小区人脸识别与出入记录管理系统功能》文章介绍基于SpringBoot框架与百度AI人脸识别API的小区出入管理系统,实现自动识别、记录及查询功能,涵盖技术选型、数据模型... 目录系统功能概述技术栈选择核心依赖配置数据模型设计出入记录实体类出入记录查询表单出入记录 VO 类(用于

java中pdf模版填充表单踩坑实战记录(itextPdf、openPdf、pdfbox)

《java中pdf模版填充表单踩坑实战记录(itextPdf、openPdf、pdfbox)》:本文主要介绍java中pdf模版填充表单踩坑的相关资料,OpenPDF、iText、PDFBox是三... 目录准备Pdf模版方法1:itextpdf7填充表单(1)加入依赖(2)代码(3)遇到的问题方法2:pd

Zabbix在MySQL性能监控方面的运用及最佳实践记录

《Zabbix在MySQL性能监控方面的运用及最佳实践记录》Zabbix通过自定义脚本和内置模板监控MySQL核心指标(连接、查询、资源、复制),支持自动发现多实例及告警通知,结合可视化仪表盘,可有效... 目录一、核心监控指标及配置1. 关键监控指标示例2. 配置方法二、自动发现与多实例管理1. 实践步骤

Kotlin Map映射转换问题小结

《KotlinMap映射转换问题小结》文章介绍了Kotlin集合转换的多种方法,包括map(一对一转换)、mapIndexed(带索引)、mapNotNull(过滤null)、mapKeys/map... 目录Kotlin 集合转换:map、mapIndexed、mapNotNull、mapKeys、map

在Spring Boot中集成RabbitMQ的实战记录

《在SpringBoot中集成RabbitMQ的实战记录》本文介绍SpringBoot集成RabbitMQ的步骤,涵盖配置连接、消息发送与接收,并对比两种定义Exchange与队列的方式:手动声明(... 目录前言准备工作1. 安装 RabbitMQ2. 消息发送者(Producer)配置1. 创建 Spr