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. Eclipse里git提交冲突rejected – non-fast-forward

    Eclipse里commit代码,其实只是提交到本地仓库,需要push才会提交到远程的git仓库,这时是一个本地仓库到远程仓库的同步过程.Git是分布式的,每个人在本地仓库维护本地的自己的那一份代码, ...

  2. MyBatis缓存结构

    Mybatis Cache结构图: CacheKey(statementId, sql, sqlParams,other). 上图展示了Mybatis Cache的结构: 1)每个Mapper对应一块 ...

  3. Oracle sqlldr数据加载

    1 sqlldr 传统路径:sqlldr会利用sql插入为我们加载数据 直接路径加载:sqlldr不适用sql,直接格式化数据块,绕开undo,避开redo,最快的方法就是并行直接路径加载 sqlld ...

  4. maven学习5 构建MyBatis项目

    2. 修改pom.xml,添加MyBatis依赖 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi=& ...

  5. maven学习1

     1.Maven的约定 src/main/java: 存放项目的java文件. src/main/resources: 存放项目的资源文件,如spring,hibernate的配置文件. src/te ...

  6. ILMerge最佳实践

    背景 为了生成的代码更加简捷,复制方便,常常会把多个可执行文件合并成一个. 方案 Project=>Properties=>Build Events=>Edit Post-build ...

  7. 如何规避javascript多人开发函数重名问题

    命名空间 封闭空间 js模块化mvc(数据层.表现层.控制层) seajs(如果了解的呢,可以说) 变量转换成对象的属性 对象化

  8. div+css显示两行或三行文字

    display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: ; //需要控制的文本行数 overflow: hidd ...

  9. html 之表单,div标签等

    一 , 表单 功能 : 表单用于向服务器传输数据, 从而实现用户与Web服务器的交互 表单能够包含input系列标签,比如文本字段,复选框 , 单选框 , 提交按钮等等. 表单还可以包含textare ...

  10. 搭建J2EE开发平台-Eclipse+MySql+tomcat

    搭建J2EE开发平台-Eclipse+MySql+tomcat 分类: ·Java 2010-10-10 15:45 2596人阅读 评论(3) 收藏 举报 mysql平台eclipsetomcatj ...