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

相关文章

JSON字符串转成java的Map对象详细步骤

《JSON字符串转成java的Map对象详细步骤》:本文主要介绍如何将JSON字符串转换为Java对象的步骤,包括定义Element类、使用Jackson库解析JSON和添加依赖,文中通过代码介绍... 目录步骤 1: 定义 Element 类步骤 2: 使用 Jackson 库解析 jsON步骤 3: 添

将sqlserver数据迁移到mysql的详细步骤记录

《将sqlserver数据迁移到mysql的详细步骤记录》:本文主要介绍将SQLServer数据迁移到MySQL的步骤,包括导出数据、转换数据格式和导入数据,通过示例和工具说明,帮助大家顺利完成... 目录前言一、导出SQL Server 数据二、转换数据格式为mysql兼容格式三、导入数据到MySQL数据

Java中List转Map的几种具体实现方式和特点

《Java中List转Map的几种具体实现方式和特点》:本文主要介绍几种常用的List转Map的方式,包括使用for循环遍历、Java8StreamAPI、ApacheCommonsCollect... 目录前言1、使用for循环遍历:2、Java8 Stream API:3、Apache Commons

关于rpc长连接与短连接的思考记录

《关于rpc长连接与短连接的思考记录》文章总结了RPC项目中长连接和短连接的处理方式,包括RPC和HTTP的长连接与短连接的区别、TCP的保活机制、客户端与服务器的连接模式及其利弊分析,文章强调了在实... 目录rpc项目中的长连接与短连接的思考什么是rpc项目中的长连接和短连接与tcp和http的长连接短

Oracle查询优化之高效实现仅查询前10条记录的方法与实践

《Oracle查询优化之高效实现仅查询前10条记录的方法与实践》:本文主要介绍Oracle查询优化之高效实现仅查询前10条记录的相关资料,包括使用ROWNUM、ROW_NUMBER()函数、FET... 目录1. 使用 ROWNUM 查询2. 使用 ROW_NUMBER() 函数3. 使用 FETCH FI

Python MySQL如何通过Binlog获取变更记录恢复数据

《PythonMySQL如何通过Binlog获取变更记录恢复数据》本文介绍了如何使用Python和pymysqlreplication库通过MySQL的二进制日志(Binlog)获取数据库的变更记录... 目录python mysql通过Binlog获取变更记录恢复数据1.安装pymysqlreplicat

Servlet中配置和使用过滤器的步骤记录

《Servlet中配置和使用过滤器的步骤记录》:本文主要介绍在Servlet中配置和使用过滤器的方法,包括创建过滤器类、配置过滤器以及在Web应用中使用过滤器等步骤,文中通过代码介绍的非常详细,需... 目录创建过滤器类配置过滤器使用过滤器总结在Servlet中配置和使用过滤器主要包括创建过滤器类、配置过滤

正则表达式高级应用与性能优化记录

《正则表达式高级应用与性能优化记录》本文介绍了正则表达式的高级应用和性能优化技巧,包括文本拆分、合并、XML/HTML解析、数据分析、以及性能优化方法,通过这些技巧,可以更高效地利用正则表达式进行复杂... 目录第6章:正则表达式的高级应用6.1 模式匹配与文本处理6.1.1 文本拆分6.1.2 文本合并6

python与QT联合的详细步骤记录

《python与QT联合的详细步骤记录》:本文主要介绍python与QT联合的详细步骤,文章还展示了如何在Python中调用QT的.ui文件来实现GUI界面,并介绍了多窗口的应用,文中通过代码介绍... 目录一、文章简介二、安装pyqt5三、GUI页面设计四、python的使用python文件创建pytho

hdu2241(二分+合并数组)

题意:判断是否存在a+b+c = x,a,b,c分别属于集合A,B,C 如果用暴力会超时,所以这里用到了数组合并,将b,c数组合并成d,d数组存的是b,c数组元素的和,然后对d数组进行二分就可以了 代码如下(附注释): #include<iostream>#include<algorithm>#include<cstring>#include<stack>#include<que