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. 时间js

    function DateUtil(){ this.url = ""; this.op={ partten:{mdy:"m/d/y",ymd:"y/m ...

  2. 如何设计并使用FireMonkeyStyle

    如何设计并使用FireMonkeyStyle FireMonkey使用Style来控制控件的显示方式. 每个控件都有一个StyleLookup属性,FireMonkey就是通过控件的这个属性来在当前窗 ...

  3. new JSONObject()报错

    如果缺少下列依赖包会报错: 1.commons-beanutils-1.7.0.jar 2.commons-collections-3.2.1.jar 3.commons-lang-2.3.jar 4 ...

  4. 【转】学习使用Jmeter做压力测试(二)--压力测试的实施

    JMeter测试步骤: 1.建立测试计划 2.添加线程组 3.添加HTTP请求 4.增加监听器 5.执行测试计划 6.根据JMeter提供的报告分析结果 一.目标 测试访问目标服务器网站首页的每秒查询 ...

  5. 求 s=a+aa+ aaa+ aaaa +aaaaa+........的值,a是从键盘输入的,项数也为键盘输入

    总结:这道题目.主要是那个位数,需要*10, while(i<f){ x+=y;//决定位数上的那个数 sum+=x//求和 y*10=y;//决定位数 } package com.b; imp ...

  6. 杂项:UN-APP

    ylbtech-杂项:APP 1.返回顶部   2.返回顶部   3.返回顶部   4.返回顶部   5.返回顶部     6.返回顶部   7.返回顶部   8.返回顶部   9.返回顶部   10 ...

  7. 1124 Raffle for Weibo Followers

    题意:水题,直接贴代码了.(为什么我第一遍做的时候代码写的那么烦?) 代码: #include <iostream> #include <string> #include &l ...

  8. thinkphp遇到的小问题,js文件中U方法不被解析

    我想在js文件中写ajax, 写完发现异常, 本以为是js文件中不支持ajax 后来发现时地址解析错误. 也就是U方法在js文件中不被解析. 貌似thinkphp解析,tpl文件中的一些元素. js文 ...

  9. python's ninth day for me

    函数 函数的定义与调用: #def  关键字  定义一个函数. # my_len  函数名, 函数名的书写规则与变量的命名一致. # def  与函数名中间一个空格. # 函数名() :  加上冒号. ...

  10. vs中的强大的代码段管理

    vs中的代码段管理可以实现大段固定文本的快捷输入,方法: 首先编写.snippet文件如: <?xml version="1.0" encoding="utf-8& ...