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内存优化牛刀小试

    小猿做了两年的c++,上个月竟然被调到java项目,于是第一篇随笔就想八一八java的内存优化. 首先优化这种事,肯定是应该放到最后去做的,不过在写代码的过程中养成良好的习惯也是很重要的.在这里先推荐 ...

  2. express的session函数

    key:这个表示session返回来的cookie的键值, 我们整理一下哈: 这个是我们没有清缓存然后刷新了一下哈,对比的结果,发现session保存的数据中,只是expires这个改变了 { &qu ...

  3. [C#]Attribute特性

    简介 特性提供功能强大的方法,用以将元数据或声明信息与代码(程序集.类型.方法.属性等)相关联. 特性与程序实体关联后,即可在运行时使用名为“反射”的技术查询特性. 特性具有以下属性: 特性可向程序中 ...

  4. VMware v12.1.1 专业版以及永久密钥

    热门虚拟机软件VMware Workstation 现已更新至v12.1.1 专业版!12.0属于大型更新,专门为Win10的安装和使用做了优化,支持DX10.4K高分辨率显示屏.OpenGL 3.3 ...

  5. ASP.NET Web API实现POST报文的构造与推送

    毕设和OAuth协议相关,而要理解OAuth协议就必须理解HTTP GET/POST方法.因此研究了一下如何使用Web API或MVC构造POST报文并实现客户端与服务器端的交互. 我使用的工具是Vi ...

  6. 读JS高级——第五章-引用类型 _记录

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  7. hdu1231 最大连续子序列

    #include<stdio.h> #include<string.h> #define maxn 10010 int a[maxn],dp[maxn]; int main() ...

  8. 41.Android之图片放大缩小学习

    生活中经常会用到图片放大和缩小,今天简单学习下. 思路:1.添加一个操作图片放大和缩小类;  2. 布局文件中引用这个自定义控件;  3. 主Activity一些修改. 代码如下: 增加图片操作类: ...

  9. 37.Activity之间的转换以及数据的传递(Intent)学习

      Intent简介:                                                                                在一个Androi ...

  10. BZOJ-1067 降雨量 线段树+分类讨论

    这道B题,刚的不行,各种碎点及其容易忽略,受不鸟了直接 1067: [SCOI2007]降雨量 Time Limit: 1 Sec Memory Limit: 162 MB Submit: 2859 ...