湖南省第十一届大学生程序设计竞赛:Internet of Lights and Switches(HASH+二分+异或前缀和)

Internet of Lights and Switches

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 3  Solved: 3
[Submit][Status][Web Board]

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

Input

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.  

Output

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

Sample Input

2 4 1 4
01
10
11
00
2 4 3 3
01
10
11
00
6 3 1 3
101001
010110
101001

Sample Output

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

题意:现在有m盏灯,有n个开关(m<=50,n<=300000) 现在开关用01串表示,为1表示开关可以操作当前的灯,现在必须操纵连续k盏灯使得这些这些灯全部关闭,A<=k<=B,问现在有多少这样连续的开关满足条件?题解:去年省赛竟然有几支队A了,ORZ,映射好难想到啊~.这题的话我们保存一个异或前缀和,如果某段的异或和为全1,那么这段就是可取的.但是怎么找到呢?我们假设xorsum[1,i]^xorsum[1,j] 为全1.那么xor[j+1,i]是符合条件的,关键是怎么找到这个区间?这时候就要用到hash了,用map映射满足当前异或前缀和的所有下标。用一个vector记录,然后输入的时候,只要查询 i 前面有多少个下标的异或值为 1111111...^xor[1,x], 然后可以二分查询当前的下标距离 i 必须为 A~B 之间,在这个范围内满足的下标的个数就是当前与 i 组合成为区间满足条件的个数,还有一种情况就是[1,i]这个区间满足条件,这种情况直接++即可.
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include <algorithm>
#include <map>
#include <vector>
using namespace std;
typedef long long LL;
char str[80];
const int INF = 999999999;
map<LL,vector<int> > mp;
int n,m,A,B;
LL charToLL(char *str){
    LL ans = 0;
    int p = 1,len = strlen(str);
    for(int i=len-1;i>=0;i--){
        if(str[i]==‘1‘){
            ans+=p;
        }
        p*=2;
    }
    return ans;
}
int binary(LL val,int id){
    vector <int> vec = mp[val];
    int down = 0,up = vec.size()-1,r=-1,l=INF;
    while(down<=up){
        int mid = (down+up)>>1;
        if(id-vec[mid]>=A){
            r = mid;
            down = mid+1;
        }else up = mid-1;
    }
    down = 0,up = vec.size()-1;
    while(down<=up){
        int mid = (down+up)>>1;
        if(id-vec[mid]<=B){
            l = mid;
            up = mid-1;
        }else down = mid+1;
    }
    //printf("%d %d\n",l,r);
    if(l>r) return 0;
    return r-l+1;
}
int main(){
    int t = 1;
    while(scanf("%d%d%d%d",&m,&n,&A,&B)!=EOF){
        mp.clear();
        for(int i=0;i<m;i++){
            str[i] = ‘1‘;
        }
        str[m]=‘\0‘;
        LL k = charToLL(str),xorsum=0,ans = 0;
        for(int i=1;i<=n;i++){
            scanf("%s",str);
            LL x = charToLL(str);
            xorsum^=x;
            if(xorsum==k&&i>=A&&i<=B) ans++;
            ans+=binary(k^xorsum,i);
            mp[xorsum].push_back(i);
        }
        printf("Case %d: %lld\n",t++,ans);
    }
}
				
时间: 2024-08-26 05:27:33

湖南省第十一届大学生程序设计竞赛:Internet of Lights and Switches(HASH+二分+异或前缀和)的相关文章

湖南省第九届大学生程序设计竞赛

I Interesting Calculator CSU 过了 TOJ超时了 先记一下 #include <cstdio> #include <cstring> #include <queue> using namespace std; int a[100]; bool b[100010]; int c[100010]; int d[100010]; int x, y; int cas = 1; struct node { int x, t, tt; node(){}

2018 ACM 国际大学生程序设计竞赛上海大都会赛

传送门:2018 ACM 国际大学生程序设计竞赛上海大都会赛 2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛2018-08-05 12:00:00 至 2018-08-05 17:00:00时长: 5小时 比赛情况 实录 难度差不多介于省赛和区域赛之间吧.开题A是计算几何,有点思路后就先放下去写签到题,B读错题WA一发,K直接套模板,然后就接着看A.之前写过类似题,没注意数据范围就头铁地交了发n3的代码,TE后才发现数据范围是之前那道十多倍,就听学长的先看D.推十分钟公式无果后打算直

湖南省大学生程序设计竞赛系统设计

背景:本人一直学习DBA数据库维护技能,出于同学需要,充当数据库设计开发,第一次与同学一起完成了一套小型管理系统的设计开发.自己充当数据库设计者,记录下来自己作为留念.  (相关的UML图已省略) 一. 引言 1.1项目背景 湖南省每年都要举行大学生程序设计竞赛,每次竞赛时,由组委会发布竞赛要求,各大高校分别对自己学校的队伍进行报名. 传统的以人工方式为主进行该项赛事的报名工作,每年将耗费大量的人力物力,同时还伴随着各种突发问题.如果通过计算机网络将竞赛组委会和各大高校联系在一起,使用网络发布竞

HDU 5925 Coconuts 【离散化+BFS】 (2016CCPC东北地区大学生程序设计竞赛)

Coconuts Time Limit: 9000/4500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 524    Accepted Submission(s): 151 Problem Description TanBig, a friend of Mr. Frog, likes eating very much, so he always has dreams abou

[2012山东省第三届ACM大学生程序设计竞赛]——Fruit Ninja II

Fruit Ninja II 题目:http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2416 Time Limit: 5000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描述 Have you ever played a popular game named "Fruit Ninja"? Fruit Ninja (known as Fruit Ninja

HDU 5961 传递 【图论+拓扑】 (2016年中国大学生程序设计竞赛(合肥))

传递 Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)   Problem Description 我们称一个有向图G是传递的,当且仅当对任意三个不同的顶点a,,若G中有 一条边从a到b且有一条边从b到c ,则G中同样有一条边从a到c.我们称图G是一个竞赛图,当且仅当它是一个有向图且它的基图是完全图.换句 话说,将完全图每条边定向将得到一个竞赛图.下图展示的是一个有4个顶点的竞

HDU 5924 Mr. Frog’s Problem 【模拟】 (2016CCPC东北地区大学生程序设计竞赛)

Mr. Frog's Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 312    Accepted Submission(s): 219 Problem Description One day, you, a clever boy, feel bored in your math class, and then fall

HDU 5927 Auxiliary Set 【DFS+树】(2016CCPC东北地区大学生程序设计竞赛)

Auxiliary Set Time Limit: 9000/4500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 873    Accepted Submission(s): 271 Problem Description Given a rooted tree with n vertices, some of the vertices are important. An a

Alice and Bob(2013年山东省第四届ACM大学生程序设计竞赛)

Alice and Bob Time Limit: 1000ms   Memory limit: 65536K 题目描述 Alice and Bob like playing games very much.Today, they introduce a new game. There is a polynomial like this: (a0*x^(2^0)+1) * (a1 * x^(2^1)+1)*.......*(an-1 * x^(2^(n-1))+1). Then Alice as