• poj3274 Gold Balanced Lineup(HASH)


    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[100010];
    int f[100010][40],sum[100010][40],c[100010][40];
    int n,k,ans=0;
    
    int check(int a,int b)
    {
        for(int w=2;w<=k;w++)
        {
            if(c[a][w]!=c[b][w])
            {
                return 0;
            }
        }
        return 1;
    }
    
    int main()
    {
        scanf("%d%d",&n,&k);
        for(int i=1;i<=n;i++)
        {
            int tmp;
            scanf("%d",&tmp);
            for(int j=1;j<=k;j++)
            {
                f[i][j]=tmp%2;
                tmp>>=1;
            }
        }
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=k;j++)
            {
                sum[i][j]=sum[i-1][j]+f[i][j];
            }
        }
        for(int i=0;i<=n;i++)
        {
            int key=0;
            for(int j=2;j<=k;j++)
            {
                c[i][j]=sum[i][j]-sum[i][1];
            }
            for(int j=2;j<=k;j++)
            {
                key+=c[i][j];
            }
            key=(key+99991)%99991;
            if(g[key].size())
            {
                for(int h=0;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
    ",ans);
        return 0;
    }
     
     
     
     
     
     
     
     
     
  • 相关阅读:
    二叉树
    消息
    线性表 及Java实现 顺序表、链表、栈、队列
    Memcache简介
    redis例子
    redis简介
    Android客户端采用Http 协议Post方式请求与服务端进行数据交互(转)
    jQueryValidate实现重复性验证
    mybatis中${}和#{}的区别
    List转换为数组Array的方法
  • 原文地址:https://www.cnblogs.com/stxy-ferryman/p/8464904.html
Copyright © 2020-2023  润新知