[Usaco2007 Mar]Gold Balanced Lineup 平衡的队列
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
第一行给出数字N,K
下面N行每行给出一个数字,代表这头牛的特征值
Output
求出一个区间值,在这个区间中,所有牛的这K种特征值的总和是相等的.
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
这题我们推推柿子,我们首先记录一下前缀和 sum[i][k],表示到第i头牛,k的特征值前缀和为k。如果说某段区间满足条件,那么肯定有\(sum[r][i]-sum[l-1][i](i\in[1,k])\)都相同,我们单独拎出两项\(sum[r][i]-sum[l-1][i]=sum[r][i-1]-sum[l-1][i-1]\),移项得到\(sum[r][i]-sum[r][i-1]=sum[l-1][i]-sum[l-1][i-1]\),因此,我们只需要对每个点记录一下\(sum[x][i]-sum[x][i-1]\),而且由于我们按顺序枚举,那么最先出现的做左端点必定更优,这样我们就得到了判断第i个和第i-1个特征值相同的情况的办法了。判多个也只需要多次差分即可
然后记录位置可以用map,当然,这题并不需要排序,因此可以用unordered_map,不过记得手写hash
/*program from Wolfycz*/
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<unordered_map>
#define inf 0x7f7f7f7f
using namespace std;
typedef long long ll;
typedef unsigned int ui;
typedef unsigned long long ull;
inline int read(){
int x=0,f=1;char ch=getchar();
for (;ch<'0'||ch>'9';ch=getchar()) if (ch=='-') f=-1;
for (;ch>='0'&&ch<='9';ch=getchar()) x=(x<<1)+(x<<3)+ch-'0';
return x*f;
}
inline void print(int x){
if (x>=10) print(x/10);
putchar(x%10+'0');
}
const int N=1e5,limit=2333;
int n,k,Ans;
struct S1{
int v[30];
S1(){memset(v,0,sizeof(v));}
bool operator ==(const S1 &x)const{
for (int i=1;i<k;i++) if (v[i]!=x.v[i]) return 0;
return 1;
}
}tmp;
struct myHash{
size_t operator ()(const S1 &x)const{
ui res=0;
for (int i=1;i<k;i++) res=res*limit+x.v[i];
return res;
}
};
unordered_map<S1,int,myHash>mp;
int cnt[30];
void Extract(int x){for (int i=0;i<k;i++) cnt[i]+=x&1,x>>=1;}
int main(){
n=read(),k=read(),mp[tmp]=0;
for (int i=1;i<=n;i++){
Extract(read());
for (int i=1;i<k;i++) tmp.v[i]=cnt[i]-cnt[i-1];
if (mp.count(tmp)) Ans=max(Ans,i-mp[tmp]);
else mp[tmp]=i;
}
printf("%d\n",Ans);
return 0;
}
[Usaco2007 Mar]Gold Balanced Lineup 平衡的队列的更多相关文章
- 1702: [Usaco2007 Mar]Gold Balanced Lineup 平衡的队列
1702: [Usaco2007 Mar]Gold Balanced Lineup 平衡的队列 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 510 S ...
- bzoj 1702: [Usaco2007 Mar]Gold Balanced Lineup 平衡的队列——map+hash+转换
Description N(1<=N<=100000)头牛,一共K(1<=K<=30)种特色, 每头牛有多种特色,用二进制01表示它的特色ID.比如特色ID为13(1101), ...
- 【BZOJ】1702: [Usaco2007 Mar]Gold Balanced Lineup 平衡的队列
[题意]给定n头牛,k个特色,给出每头牛拥有哪些特色的二进制对应数字,[i,j]平衡当且仅当第i~j头牛的所有特色数量都相等,求最长区间长度. [算法]平衡树+数学转化 [题解]统计前缀和sum[i] ...
- BZOJ1702: [Usaco2007 Mar]Gold Balanced Lineup 平衡的队列
n<=100000个数表示每头牛在K<=30种物品的选取情况,该数在二进制下某位为0表示不选1表示选,求一个最大的区间使区间内选择每种物品的牛一样多. 数学转化,把不同状态间单变量的关系通 ...
- bzoj 1702: [Usaco2007 Mar]Gold Balanced Lineup 平衡的队列【hash】
我%&&--&()&%????? 双模hashWA,unsigned long longAC,而且必须判断hash出来的数不能为0???? 我可能学了假的hash 这个 ...
- 哈希-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 ...
- POJ 3274 Gold Balanced Lineup
Gold Balanced Lineup Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10924 Accepted: 3244 ...
- POJ 3274:Gold Balanced Lineup 做了两个小时的哈希
Gold Balanced Lineup Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13540 Accepted: ...
- 洛谷 P1360 [USACO07MAR]Gold Balanced Lineup G (前缀和+思维)
P1360 [USACO07MAR]Gold Balanced Lineup G (前缀和+思维) 前言 题目链接 本题作为一道Stl练习题来说,还是非常不错的,解决的思维比较巧妙 算是一道不错的题 ...
随机推荐
- [K3Cloud] QueryService使用注意事项
QueryServlice是目前查询数据非常好用的服务,但目前在使用过程中由于使用不当产生不少问题,下面将一一解答: 1.在查询一些实体关键字段如实体主键.分录序号时,条件中的别名怎么会变来变去? ...
- spring boot项目自定义数据源,mybatisplus分页、逻辑删除无效解决方法
Spring Boot项目中数据源的配置可以通过两种方式实现: 1.application.yml或者application.properties配置 2.注入DataSource及SqlSessio ...
- 基于Token的身份验证——JWT(转)
本文转自:http://www.cnblogs.com/zjutzz/p/5790180.html 感谢作者 初次了解JWT,很基础,高手勿喷.基于Token的身份验证用来替代传统的cookie+se ...
- 在Windows下搭建RocketMQ
原文:http://blog.csdn.net/u014134180/article/details/51790988 目录 目录 一 准备工作 1 RocketMQ部署架构1 2 环境配置 二 安装 ...
- cakephp2.3.8中何为component
大胆假设,小心求证 记得这句话是以前读高三的时候,一个数学老师(圆头,有点胖胖的老师,不是很记得),经常挂在嘴边的一句话, 对于我们不理解,或者说是无法确定的东西,我们不妨先大胆地去假 ...
- Linux 中浏览网页的命令行
Linux系统环境的WEB网站浏览器工具,常用的有w3m.Links.Lynx三个工具 第一.w3m w3m文本浏览器是基于GPL协议发布的且支持表格.颜色.SSL连接以及内链图像,因速度快而著称. ...
- 【Python】python扩展
当python的基本功能无法满足要求.或者是为了保密源码(.py).遇到性能瓶颈时,我们经常要扩展python,扩展语言能够是C/C++.Java.C#等. 为python创建扩展须要三个基本的步骤: ...
- Android 5.1 Settings源代码简要分析
转载请注明出处,谢谢~http://blog.csdn.net/u011974987/article/details/51004854. 概述: 先声明:本人工作快两年了,仍是菜鸟级别的.羞愧啊!曾经 ...
- Wps 2013 拼音标注两种方式分析
Wps 2013 拼音标注两种方式分析 太阳火神的漂亮人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名-非商业用途-保持一致"创作公用协议 转 ...
- Lvs 负载均衡 (VS/NAT模式)
一.LVS简介 LVS是 Linux Virtual Server 的简称,也就是Linux虚拟服务器.这是一个由章文嵩博士发起的一个开源项目,它的官方网站是 http://www.linuxvirt ...