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

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

#include <iostream>
#include <cstring>
#include <cstdio>

using namespace std;

const int MAXHASH=100007;
int n,k,a;
int bit[100007][32];
int head[MAXHASH+10],next[MAXHASH+10];//散列表 拉链法。。。。

int hash(int v[])
{
    int h=0;
    for(int i=0;i<k;i++)
    {
        h=((h<<2)+v>>4)^(v<<10);//牛逼的。。。数组哈希函数(什么折叠法。。。)
    }
    h=h%MAXHASH;
    if(h<0)
        h+=MAXHASH;
    return h;
}

int main()
{
    scanf("%d%d",&n,&k);
    memset(head,-1,sizeof(head));

int ans=0;

for(int i=1;i<=n;i++)
    {
        scanf("%d",&a);
        for(int j=0;j<k;j++)
        {
            bit[j]=a&1;
            a=a>>1;
        }
    }

for(int i=2;i<=n;i++)
    {
        for(int j=0;j<k;j++)
        {
            bit[j]+=bit[i-1][j];
        }
    }

for(int i=0;i<=n;i++)
    {
        int tmp=bit[0];
        for(int j=0;j<k;j++)
        {
            bit[j]-=tmp;
        }
        int h=hash(bit);
        bool Find=false;
        for(int e=head[h];~e;e=next[e])
        {
            if(memcmp(bit[e],bit,sizeof(bit[e]))==0)
            {
                Find=true;
                ans=max(ans,i-e);
                break;
            }
        }
        if(!Find)
        {
            next=head[h];
            head[h]=i;
        }
    }
    printf("%d\n",ans);
    return 0;
}

* This source code was highlighted by YcdoiT. ( style: Codeblocks )

POJ 3274 Gold Balanced Lineup的更多相关文章

  1. poj 3274 Gold Balanced Lineup(哈希 )

    题目:http://poj.org/problem?id=3274 #include <iostream> #include<cstdio> #include<cstri ...

  2. POJ 3274 Gold Balanced Lineup(哈希)

    http://poj.org/problem?id=3274 题意 :农夫约翰的n(1 <= N <= 100000)头奶牛,有很多相同之处,约翰已经将每一头奶牛的不同之处,归纳成了K种特 ...

  3. POJ 3274 Gold Balanced Lineup 哈希,查重 难度:3

    Farmer John's N cows (1 ≤ N ≤ 100,000) share many similarities. In fact, FJ has been able to narrow ...

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

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

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

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

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

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

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

  8. 【POJ】3264 Balanced Lineup ——线段树 区间最值

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 34140   Accepted: 16044 ...

  9. POJ 题目3264 Balanced Lineup(RMQ)

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 39046   Accepted: 18291 ...

随机推荐

  1. 深入探索Java 8 Lambda表达式

    2014年3月,Java 8发布,Lambda表达式作为一项重要的特性随之而来.或许现在你已经在使用Lambda表达式来书写简洁灵活的代码.比如,你可以使用Lambda表达式和新增的流相关的API,完 ...

  2. [USACO2004][poj2373]Dividing the Path(DP+单调队列)

    http://poj.org/problem?id=2373 题意:一条直线分割成N(<=25000)块田,有一群奶牛会在其固定区域吃草,每1把雨伞可以遮住向左右延伸各A到B的区域,一只奶牛吃草 ...

  3. 第三十二课:JSDeferred的性能提速

    大家如果看了前面两课,就知道Deferred的静态方法next(next_default)是用setTimeout实现的(有浏览器最小时钟间隔).但是实现这种异步操作,可以有很多种方法.JSDefer ...

  4. web前端开发教程系列-2 - 前端开发书籍分享

    目录: 前言 一. CSS 二. JavaScript 三. jQuery 四. 后记   前言 前端书籍在每个商城或书架上面都是琳琅满目,很多初学者又不能很好的判断书的质量或层次.因为今天给同学们分 ...

  5. SqlDependency数据库同步+signalr 推送消息

    sqlDependency提供了这样一种能力:当被监测的数据库中的数据发生变化时,SqlDependency会自动触发OnChange事件来通知应用程序,从而达到让系统自动更新数据(或缓存)的目的. ...

  6. PHP时间日期比较

    若要使用PHP来比较日期,最好用DateTime::diff 但是这个是5.3才支持的,如果没有这样的环境,可以使用<.>来比较 如下例子,会输出right $date1=strtotim ...

  7. OpenLayers中地图缩放级别的设置方法

    来源于:http://www.cnblogs.com/sailheart/archive/2011/03/15/1984519.html 一.概述 在OpenLayers中,地图必须具有一个缩放级别的 ...

  8. js-定时任务setInterval,setTimeout,clearInterval,clearTimeout

    setInterval()循环执行相应的方法 <script type="text/javascript"> setInterval("myInterval( ...

  9. Mysql-字段类型

    首先统计所有,以表格查看 数字类型 列类型 需要的存储量 TINYINT 1 字节 SMALLINT 2 个字节 MEDIUMINT 3 个字节 INT 4 个字节 INTEGER 4 个字节 BIG ...

  10. 第六节 JBPM版本控制以及Token对象

    1.JBPM版本 2.Token 3.流程上下文