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 )

转载于:https://www.cnblogs.com/CKboss/p/3350867.html

POJ 3274 Gold Balanced Lineup相关推荐

  1. POJ 3274 Gold Balanced Lineup(哈希)

    题目链接 很难想.会哈希,但是想不出.需要一个转化,本来是求某一段上的二进制上每一位的1数目相等,转化为找两段相等的,换元可推出公式.最后注意特判.. 1 #include <iostream& ...

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

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

  3. POJ3274 Gold Balanced Lineup【Hash函数】

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

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

    1702: [Usaco2007 Mar]Gold Balanced Lineup 平衡的队列 Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 821   ...

  5. 【POJ 3274】Gold Balanced Lineup (stl map )设计hash表,处理碰撞

    题目链接 题目链接 http://poj.org/problem?id=3274 题意 输入每头牛的特征的10进制,若i~j头牛中每个数位的特征相等则满足要求,求所有满足要求的j-i的最大值. 解题思 ...

  6. Gold Balanced Lineup - poj 3274 (hash)

    这题,看到别人的解题报告做出来的,分析: 大概意思就是: 数组sum[i][j]表示从第1到第i头cow属性j的出现次数. 所以题目要求等价为: 求满足 sum[i][0]-sum[j][0]=sum ...

  7. BZOJ1702: [Usaco2007 Mar]Gold Balanced Lineup 平衡的队列

    n<=100000个数表示每头牛在K<=30种物品的选取情况,该数在二进制下某位为0表示不选1表示选,求一个最大的区间使区间内选择每种物品的牛一样多. 数学转化,把不同状态间单变量的关系通 ...

  8. POJ3274Gold Balanced Lineup(哈希)

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

  9. POJ 3264 Balanced Lineup【线段树区间查询求最大值和最小值】

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 53703   Accepted: 25237 ...

  10. POJ 3264 Balanced Lineup

    POJ 3264 Balanced Lineup 题目链接 Description For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,00 ...

最新文章

  1. 新基建来势汹汹,开发者如何捍卫其安全?
  2. 外企面试官们爱提的十个问题
  3. 虚拟内存(VirtualAlloc),堆(HeapAlloc/malloc/new)和Memory Mapped File
  4. VHDL汽车尾灯控制器的设计
  5. Enterprise Library 2.0 Hands On Lab 翻译(14):加密应用程序块(一)
  6. 领域建模——架构设计的第一步(下)
  7. clark变换_电力变换器PWM原理与实践,p43页,空间矢量理解
  8. pyinstaller打包后闪现cmd黑色窗口解决方案
  9. java 加水印_Java添加水印(图片水印,文字水印)
  10. 开发一个类似于 xxx 的应用有多难?
  11. 微信支付系统的单号原来是这样设计的
  12. php acs解密,RSA 加密及php实现
  13. border-radius的一种经典使用(上凸边框)
  14. Race_Condition_Vulnerability
  15. 使用xom实现xml文件数据的查找,删除,修改(转载)
  16. 前端开发(layui框架)
  17. 【报告分享】2020全球时尚IP白皮书-CBNData.(附下载)
  18. 搞机:AS自带模拟器AVD Root 和 Xposed安装
  19. C编译问题: declaration is incompatible with previous FuncName (declared at line XXX)
  20. 基于Arm Cortex内核的32位MCU和MPU(M0、M0+、M3、M4、M33、M7、A7)

热门文章

  1. 控制台——EventLog实现事件日志操作
  2. pyhthon Opencv截取视频中的图片
  3. 【知识笔记】WebForm
  4. React基础----ReactJS的使用(一)
  5. less--使用less.js将less文件转换成css
  6. 导航菜单点击后高亮显示
  7. Phaser中的组对象group
  8. 树上10只鸟,开枪打死1只,还剩几只?
  9. 在Mac OS X中使用VIM开发STM32(2)
  10. Bitmap Style Designer非官方说明