Gold Balanced Lineup

Time Limit: 2000MS Memory Limit: 65536K

Total Submissions: 13215 Accepted: 3873

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

Source

USACO 2007 March Gold

题意:有n头牛,每头牛都有不同的属性,这些属性可以通过数字转化的二进制表示,问最大的连续的区间长度使的这个区间的牛的各种属性的和相等.

就是保持平衡

方法:假设有一个数组sum[][]记录了第1头牛到第i头牛各种属性的和,在区间[j,i],判断是不是平衡就是判断sum[i][0]-sum[j][0]=sum[i][1]-sum[j][1]……=sum[i][k]-sum[j][k];

转化一下就是判断

sum[i][1]-sum[i][0]=sum[j][1]=sum[j][0],

sum[i][2]-sum[i][0]=sum[j][2]=sum[j][0],

sum[i][3]-sum[i][0]=sum[j][3]=sum[j][0],

.

.

.

sum[i][k]-sum[i][0]=sum[j][k]=sum[j][0];

所以我们只需要记录cmp[i][j]=sum[i][j]-sum[i][0];

如果cmp[i][]=cmp[j][],则说明[j,i]区间是平衡的,记录i-j的最大值.

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <string>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define eps 1e-9
#define LL long long
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define CRR fclose(stdin)
#define CWW fclose(stdout)
#define RR freopen("input.txt","r",stdin)
#define WW freopen("output.txt","w",stdout) const int MAX = 100010; const int Mod = 1001007; int Hash[Mod+100]; int sum[MAX][40],cmp[MAX][40];
int n,k;
int has(int *s)//哈希值转化,题解看的,不明白
{
int p=0;
for(int i=0; i<k; i++)
{
p = ((p<<2)+(s[i]>>4))^(s[i]<<10);
}
p%=Mod;
if(p<0)
{
p+=Mod;
}
return p;
} int main()
{ int data;
int Max = 0;
scanf("%d %d",&n,&k);
memset(Hash,-1,sizeof(Hash));
Hash[has(cmp[0])]=0;
for(int i=1; i<=n; i++)
{
scanf("%d",&data);
for(int j=0; j<k; j++)
{
sum[i][j]=data&1;
data=data>>1;
sum[i][j]+=sum[i-1][j];
cmp[i][j]=sum[i][j]-sum[i][0];
}
int ans = has(cmp[i]);
while(Hash[ans]!=-1)
{
int R;
for(R=0; R<k; R++)
{
if(cmp[i][R]!=cmp[Hash[ans]][R])
{
break;
}
}
if(R==k)
{
if(Max<i-Hash[ans])
{
Max=i-Hash[ans];
break;
}
}
ans++;
}
if(Hash[ans]==-1)
{
Hash[ans]=i;
}
}
printf("%d\n",Max);
return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

哈希-Gold Balanced Lineup 分类: POJ 哈希 2015-08-07 09:04 2人阅读 评论(0) 收藏的更多相关文章

  1. windows设置多长时间后自动关机 分类: windows常用小技巧 2014-04-15 09:35 230人阅读 评论(0) 收藏

    分二步: 第一步:点击windows键,在"搜索程序和文件"的文本框输入:cmd 第二步:输入:shutdown -s -t          (设置电脑一小时后自动关机) 备注: ...

  2. 快速查询本机IP 分类: windows常用小技巧 2014-04-15 09:28 138人阅读 评论(0) 收藏

    第一步: 点击windows建(屏幕左下方),在搜索程序和文件文本框内输入:cmd 第二步:      点击Enter建进入. 第三步: 输入:ipconfig即可. 版权声明:本文为博主原创文章,未 ...

  3. 如何将计算机加入域 分类: AD域 Windows服务 2015-06-10 11:04 63人阅读 评论(0) 收藏

    在上一篇博客中我已经实现了windows server 2008 R2域中的DC部署,那么如何将计算机加入到我们部署的域环境中呢? (初级教程,step by step,不足之处欢迎批评指正!) 将计 ...

  4. sscanf 函数 分类: POJ 2015-08-04 09:19 4人阅读 评论(0) 收藏

    sscanf 其实很强大 分类: 纯C技术 技术笔记 2010-03-05 16:00 12133人阅读 评论(4) 收藏 举报 正则表达式stringbuffercurlgoogle 最近在做日志分 ...

  5. 哈希-4 Values whose Sum is 0 分类: POJ 哈希 2015-08-07 09:51 3人阅读 评论(0) 收藏

    4 Values whose Sum is 0 Time Limit: 15000MS Memory Limit: 228000K Total Submissions: 17875 Accepted: ...

  6. Gold Coins 分类: POJ 2015-06-10 15:04 16人阅读 评论(0) 收藏

    Gold Coins Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 21767   Accepted: 13641 Desc ...

  7. Ombrophobic Bovines 分类: POJ 图论 最短路 查找 2015-08-10 20:32 2人阅读 评论(0) 收藏

    Ombrophobic Bovines Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16539 Accepted: 3605 ...

  8. Babelfish 分类: 哈希 2015-08-04 09:25 2人阅读 评论(0) 收藏

    Babelfish Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 36398 Accepted: 15554 Descripti ...

  9. PIGS 分类: POJ 图论 2015-08-10 09:15 3人阅读 评论(0) 收藏

    PIGS Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 18209 Accepted: 8277 Description Mir ...

随机推荐

  1. 数据库调优过程(二):找到IO不存在问题,而是sqlserver单表写入IO瓶颈

    物理机上测试IO是否为瓶颈: 使用一个死循环insert into测试数据库最大写入速度: use [iTest]; declare @index int; ; begin ; INSERT into ...

  2. CSS的叠加

    CSS中的叠加分为以下三种: 1.上下叠加 CSS部分: #div1{ width:200px; height:50px; margin-bottom:30px; background:#ffff00 ...

  3. PostgreSQL Monitor pg_activity

    PostgreSQL Monitor pg_activity Command line tool for PostgreSQL server activity monitoring. https:// ...

  4. sdutoj 2154 Shopping

    http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2154 Shopping Time Limit: ...

  5. 使用PHP发送邮件

    使用封装SMTP协议的邮件类 使用PEAR扩展中的Mail类,功能强大:可以支持纯文本.HTML格式的邮件:各字段都可设置编码,正确配置不会出现中文乱码情况:可以支持附件等等. 在服务器可以使用 pe ...

  6. oracle的簇的创建

    簇其实就是一组表,由一组共享相同数据块的多个表组成,将经常一起使用的表组合在一起成簇可以提高处理效率:在一个簇中的表就叫做簇表. 建立顺序是:簇→簇表→簇索引→数据 创建簇的格式 CREATE CLU ...

  7. IT职业选择与定位

           (一)  位置有很多,最适合你的是哪个? 有的人在电子技术的层面工作,开发出性能强劲的芯片和硬件产品:有的人在别人开发的芯片和硬件上开发各种操作系统和驱动程序:有的人在各种操作系统或设备 ...

  8. 【python cookbook】【字符串与文本】6.以不区分大小写的方式对文本做查找和替换

    问题:以不区分大小写的方式对文本做查找和替换 解决方法:使用re模块,并对各种操作都添加上re.IGNORECASE标记 text='UPPER PYTHON,lower python,Mixed P ...

  9. 【python cookbook】【数据结构与算法】3.保存最后N个元素

    问题:希望在迭代或是其他形式的处理过程中对最后几项记录做一个有限的历史记录统计 解决方案:选择collections.deque. 如下的代码对一系列文本行做简单的文本匹配操作,当发现有匹配时就输出当 ...

  10. E2PROM的尺寸

    买的E2PROM是128*8bit的, 就是只能存储128个byte, 妈的, 买小了. 实际需要的是10句, 可能加两个特殊句, "新手"跟"故障", 一共1 ...