Description

N(1<=N<=100000)头牛,一共K(1<=K<=30)种特色,

每头牛有多种特色,用二进制01表示它的特色ID。比如特色ID为13(1101),
则它有第1、3、4种特色。[i,j]段被称为balanced当且仅当K种特色在[i,j]内
拥有次数相同。求最大的[i,j]段长度。

Input

* Line 1: Two space-separated integers, N and K.

* Lines 2..N+1: Line i+1 contains a single K-bit integer specifying the features present in cow i. The least-significant bit of this integer is 1 if the cow exhibits feature #1, and the most-significant bit is 1 if the cow exhibits feature #K.

Output

* Line 1: A single integer giving the size of the largest contiguous balanced group of cows.

Sample Input

7 3
7
6
7
2
1
4
2

INPUT DETAILS:

The line has 7 cows with 3 features; the table below summarizes the
correspondence:
Feature 3: 1 1 1 0 0 1 0
Feature 2: 1 1 1 1 0 0 1
Feature 1: 1 0 1 0 1 0 0
Key: 7 6 7 2 1 4 2
Cow #: 1 2 3 4 5 6 7

Sample Output

4

OUTPUT DETAILS:

In the range from cow #3 to cow #6 (of size 4), each feature appears
in exactly 2 cows in this range:
Feature 3: 1 0 0 1 -> two total
Feature 2: 1 1 0 0 -> two total
Feature 1: 1 0 1 0 -> two total
Key: 7 2 1 4
Cow #: 3 4 5 6

—————————————————————————————
这道题的话 因为颜色一共最多有30种 转换成2进制 每一位代表一种颜色
这样之后 如果存在 a1 b1 c1 d1 == a2 b2 c2 d2
那么容易证明
因为 d2-d1=c2-c1=b2-b1=a1-b1
以d这一颜色为例 d2-d1=a2-a1
所以 d2-a2==d1-a1 这样就转换成了只和自己有关
那么每一位同理处理就可以了
这样如果有两个前缀和 i 和 j 相同的话,那么i+1~j这一段的k种颜色出现次数一样多。
这样处理之后加一波hahs和map 判断这种前缀时候存在以及他的位置就可以了
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#define LL unsigned long long
const int M=2e5+,P=;
int read(){
int ans=,f=,c=getchar();
while(c<''||c>''){if(c=='-') f=-; c=getchar();}
while(c>=''&&c<=''){ans=ans*+(c-''); c=getchar();}
return ans*f;
}
int n,k,ans;
int d[M][];
std::map<LL,int>q;
int find(int x){
LL sum=;
for(int i=;i<=k;i++) sum=sum*P+1LL*d[x][i];
if(!q[sum]&&sum) q[sum]=x;
return q[sum];
}
int main(){
n=read(); k=read();
for(int i=;i<=n;i++){
int now=,x=read();
for(;x;x>>=) d[i][++now]=x&;
for(int j=;j<=k;j++) d[i][j]+=d[i-][j];
}
for(int i=;i<=n;i++){
for(int j=;j<=k;j++) d[i][j]-=d[i][];
ans=std::max(ans,i-find(i));
}printf("%d\n",ans);
return ;
}
 

bzoj 1702: [Usaco2007 Mar]Gold Balanced Lineup 平衡的队列——map+hash+转换的更多相关文章

  1. bzoj 1702: [Usaco2007 Mar]Gold Balanced Lineup 平衡的队列【hash】

    我%&&--&()&%????? 双模hashWA,unsigned long longAC,而且必须判断hash出来的数不能为0???? 我可能学了假的hash 这个 ...

  2. 1702: [Usaco2007 Mar]Gold Balanced Lineup 平衡的队列

    1702: [Usaco2007 Mar]Gold Balanced Lineup 平衡的队列 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 510  S ...

  3. 【BZOJ】1702: [Usaco2007 Mar]Gold Balanced Lineup 平衡的队列

    [题意]给定n头牛,k个特色,给出每头牛拥有哪些特色的二进制对应数字,[i,j]平衡当且仅当第i~j头牛的所有特色数量都相等,求最长区间长度. [算法]平衡树+数学转化 [题解]统计前缀和sum[i] ...

  4. [Usaco2007 Mar]Gold Balanced Lineup 平衡的队列

    Description N(1<=N<=100000)头牛,一共K(1<=K<=30)种特色,每头牛有多种特色,用二进制01表示它的特色ID.比如特色ID为13(1101),则 ...

  5. BZOJ1702: [Usaco2007 Mar]Gold Balanced Lineup 平衡的队列

    n<=100000个数表示每头牛在K<=30种物品的选取情况,该数在二进制下某位为0表示不选1表示选,求一个最大的区间使区间内选择每种物品的牛一样多. 数学转化,把不同状态间单变量的关系通 ...

  6. 哈希-Gold Balanced Lineup 分类: POJ 哈希 2015-08-07 09:04 2人阅读 评论(0) 收藏

    Gold Balanced Lineup Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13215 Accepted: 3873 ...

  7. POJ 3274 Gold Balanced Lineup

    Gold Balanced Lineup Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10924 Accepted: 3244 ...

  8. POJ 3274:Gold Balanced Lineup 做了两个小时的哈希

    Gold Balanced Lineup Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 13540   Accepted:  ...

  9. 洛谷 P1360 [USACO07MAR]Gold Balanced Lineup G (前缀和+思维)

    P1360 [USACO07MAR]Gold Balanced Lineup G (前缀和+思维) 前言 题目链接 本题作为一道Stl练习题来说,还是非常不错的,解决的思维比较巧妙 算是一道不错的题 ...

随机推荐

  1. JS设置cookie,删除cookie(引)

    JS设置cookie,删除cookie(引) js设置cookie有很多种方法. 第一种:(这个是w3c官网的代码) <script> //设置cookie function setCoo ...

  2. Alpha 冲刺报告(3/10)

    Alpha 冲刺报告 队名:洛基小队 峻雄(组长) 已完成:开始编写角色的移动脚本 明日计划:继续学习并进行脚本编写 剩余任务:物品背包交互代码 困难:如何把各个模块的脚本整合起来 --------- ...

  3. 使用Quartz.Net同时执行多个任务

    在Quartz.Net中可能我们需要在某一时刻执行多个任务操作,而又不想创建多个任务.Quartz.Net为我们提供了多个ScheduleJob的重载来实现多个一次执行多个任务. // 创建一个组任务 ...

  4. thinkphp5学习记录一

    1 使用composer安装 composer create-project topthink/think=5.0.* tpblog --prefer-dist 2 配置环境vim /usr/loca ...

  5. C语言宏中"#"和"##"的用法

    转自:https://www.cnblogs.com/hnrainll/archive/2012/08/15/2640558.html 在查看linux内核源码的过程中,遇到了许多宏,这里面有许多都涉 ...

  6. c++移动文件夹

    bool Files::MoveSampleFolder(string src_path,string dst_path) { int index = src_path.find_last_of(&q ...

  7. 【Python】python之set

    阅读目录 一.set集合介绍 二.集合的方法 1.s.add()添加元素 3.s.copy()浅拷贝 4.s.difference(b) 5.s.difference_update(b) 6.s.di ...

  8. vs code 自动补全效果不理想的问题

    之前一直用webstorm,最近换换口味,改用了VS Code,发现VS Code 智能提示得到的都不是我想要的 就比如  ! + tab ,HTML结构都出不来.经过一番搜索,发现是 VS Code ...

  9. linux系统环境代理设置

    系统上网代理设置: 1.编辑文件/etc/profile,增加如下两行 export http_proxy=http://ip:port export https_proxy=http://ip:po ...

  10. BZOJ4871 Shoi2017摧毁“树状图”(树形dp)

    设f[i][0/1/2/3/4/5]表示i子树中选一条链不包含根/i子树中选一条链包含根但不能继续向上延伸/i子树中选一条链可以继续向上延伸/选两条链不包含根/选两条链包含根但不能继续向上延伸/选两条 ...