Description

Farmer John's N cows (1 ≤ N ≤ 100,000) share many similarities. In fact, FJ has been able to narrow down the list of features shared by his cows to a list of only K different features (1 ≤ K ≤ 30). For example, cows exhibiting feature #1 might have spots, cows exhibiting feature #2 might prefer C to Pascal, and so on.

FJ has even devised a concise way to describe each cow in terms of its "feature ID", a single K-bit integer whose binary representation tells us the set of features exhibited by the cow. As an example, suppose a cow has feature ID = 13. Since 13 written in binary is 1101, this means our cow exhibits features 1, 3, and 4 (reading right to left), but not feature 2. More generally, we find a 1 in the 2^(i-1) place if a cow exhibits feature i.

Always the sensitive fellow, FJ lined up cows 1..N in a long row and noticed that certain ranges of cows are somewhat "balanced" in terms of the features the exhibit. A contiguous range of cows i..j is balanced if each of the K possible features is exhibited by the same number of cows in the range. FJ is curious as to the size of the largest balanced range of cows. See if you can determine it.

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

Sample Output

4

Hint

In the range from cow #3 to cow #6 (of size 4), each feature appears in exactly 2 cows in this range
搬下vjudge上q234rty的中文题意:
N(1<=N<=100000)头牛,一共K(1<=K<=30)种特色, 每头牛有多种特色,用二进制01表示它的特色ID。比如特色ID为13(1101), 则它有第1、3、4种特色。[i,j]段被称为balanced当且仅当K种特色在[i,j]内 拥有次数相同。求最大的[i,j]段长度。
 
题解:这道题hash倒不是难点,难点是如何处理,如果百度一发题解会发现,只需要求出前缀和然后sum[i][2~k]均减去sum[i][1]然后判断sum[i][2~k]与sum[j][2~k]是否分别相等即可.
所以为什么呢?
如果sum[j]为(a,b,c)
若需满足条件则sum[i]=(a+x,b+x,c+x)
那么减一减之后
c[j]=(0,b-a,c-a)
c[i]=(0,b+x-a-x,c+x-a-x)
     =(0,b-a,c-a)
可见这是符合条件的
这种思路非常好值得借鉴,尤其是遇到多个数增长相同大小的时候
 
然后需要注意的有两点
1.可能到最后一个时刚好所有数的个数都相等如:
4 4
1
2
4
8
所以遍历应该从0开始
2.b-a之类的可能有负数
所以hash的时候要注意
有些hash函数要加模数后取模
 
代码如下:
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define hi puts("hi!");
using namespace std; vector<int> g[];
int f[][],sum[][],c[][];
int n,k,ans=; int check(int a,int b)
{
for(int w=;w<=k;w++)
{
if(c[a][w]!=c[b][w])
{
return ;
}
}
return ;
} int main()
{
scanf("%d%d",&n,&k);
for(int i=;i<=n;i++)
{
int tmp;
scanf("%d",&tmp);
for(int j=;j<=k;j++)
{
f[i][j]=tmp%;
tmp>>=;
}
}
for(int i=;i<=n;i++)
{
for(int j=;j<=k;j++)
{
sum[i][j]=sum[i-][j]+f[i][j];
}
}
for(int i=;i<=n;i++)
{
int key=;
for(int j=;j<=k;j++)
{
c[i][j]=sum[i][j]-sum[i][];
}
for(int j=;j<=k;j++)
{
key+=c[i][j];
}
key=(key+)%;
if(g[key].size())
{
for(int h=;h<g[key].size();h++)
{
if(check(i,g[key][h]))
{
ans=max(ans,i-g[key][h]);
break;
}
}
}
g[key].push_back(i);
}
printf("%d\n",ans);
return ;
}
 
 
 
 
 
 
 
 
 

poj3274 Gold Balanced Lineup(HASH)的更多相关文章

  1. Gold Balanced Lineup(hash)

    http://poj.org/problem?id=3274 ***** #include <stdio.h> #include <iostream> #include < ...

  2. POJ 3274 Gold Balanced Lineup

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

  3. 哈希-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 ...

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

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

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

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

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

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

  7. Gold Balanced Lineup - poj 3274 (hash)

    这题,看到别人的解题报告做出来的,分析: 大概意思就是: 数组sum[i][j]表示从第1到第i头cow属性j的出现次数. 所以题目要求等价为: 求满足 sum[i][0]-sum[j][0]=sum ...

  8. bzoj 1702: [Usaco2007 Mar]Gold Balanced Lineup 平衡的队列——map+hash+转换

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

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

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

随机推荐

  1. 开启mac terminal 命令/路径自动补全功能

    用惯了windows命令行工具的按Tab自动补全路径功能后,在mac terminal上敲命令很不习惯.其实mac terminal也有这个功能. 在命令行输入nano .inputrc 进入.inp ...

  2. Restore Nexus 5 to Stock and Flash Factory Images

    1.This is the website to download Factory Images for Nexus Devices https://developers.google.com/and ...

  3. CAN总线标准帧

    CAN总线是一种串行数据通信协议,其通信接口中集成了CAN协议的物理层和数据链路层功能,可完成对通信数据的成帧处理,包括位填充.数据块编码.循环冗余检验.优先级判别等项工作. CAN总线结构 CAN总 ...

  4. VisualGDB:使用VS创建CMake Linux项目

    转载地址:点击打开链接 根据VisualGDB官网(https://visualgdb.com)的帮助文档大致翻译而成.主要是作为个人学习记录.有错误的地方,Robin欢迎大家指正. 本文介绍如何使用 ...

  5. selenium新的定位方法,更简洁很方便

    亲测是可以的 self.driver.find_element('id','kw').send_keys(u"凯宾斯基")

  6. ARM-Linux移植之(四)——根文件系统构建

    相关工具版本: busybox-1.7.0 arm-linux-4.3.2 linux-2.6.22   1.配置busybox并安装. 在我们的根文件系统中的/bin和/sbin目录下有各种命令的应 ...

  7. 第十二章 MySQL触发器(待续)

    ······

  8. Centos7 SSH公钥生成及配置

    1.你可以按如下命令来生成 sshkey: ssh-keygen -t rsa -C "xxxxx@xxxxx.com" 2.按照提示完成三次回车,即可生成 ssh key.通过查 ...

  9. Python 面向对象 (补充) , 反射 , 内置函数

    面向对象中内置函数 issubclass方法: 检查第一个参数是否是第二个参数的子子孙孙类     返回  :   是一个布尔值 class Base(object): pass class Foo( ...

  10. Python中常见的异常处理

    异常和错误 part1:程序中难免出现错误,而错误分成两种 1. 语法错误(这种错误,根本过不了Python解释器的语法检测,必须在程序执行前就改正) # 语法错误示范一 if # 语法错误示范二 d ...