poj 3274 Gold Balanced Lineup

Gold Balanced Lineup
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 14442   Accepted: 4184

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

提示

题意以及思路:
请参照这片 博客,上面讲的十分详细,我给出的是静态内存的做法。(实际上没有什么本质的区别)

示例程序

Source Code

Problem: 3274		Code Length: 1174B
Memory: 39532K		Time: 344MS
Language: GCC		Result: Accepted
#include 
#include 
struct
{
    int id,bug;				//bug记录是否有数据占据
}hash[10000][55];
int sum[100001][30],c[100001][30],f[100001][30],max=0;
int judge(int t,int t1,int k)
{
    int i;
    for(i=0;k>i;i++)
    {
        if(c[t][i]!=c[t1][i])
        {
            return 0;
        }
    }
    return 1;
}
void insert(int t,int k)
{
    int i,key=0;
    for(i=0;k>i;i++)
    {
        key=key+c[t][i]*i;			//这里选取*i比较好,至于为什么,我测试了很多只有它可以过
    }
    key=abs(key)%10000;				//key值会有负数的情况
    for(i=0;hash[key][i].bug==1;i++)		//进行比较
    {
        if(judge(hash[key][i].id,t,k)==1&&t-hash[key][i].id>max)
        {
            max=t-hash[key][i].id;
            return;				//有符合条件的跳出吧
        }
    }
    hash[key][i].bug=1;				//没有就做记录
    hash[key][i].id=t;
}
int main()
{
    int n,k,i,i1,x;
    memset(hash,0,sizeof(hash));
    scanf("%d %d",&n,&k);
    for(i=0;k>i;i++)
    {
        sum[0][i]=0;				//把它看做源头
        f[0][i]=0;
    }
    hash[0][0].bug=1;				//记录到哈希表中
    hash[0][0].id=0;
    for(i=1;n>=i;i++)
    {
        scanf("%d",&x);				//特征的数值
        for(i1=0;k>i1;i1++)
        {
            f[i][i1]=x%2;
            x=x/2;
            sum[i][i1]=sum[i-1][i1]+f[i][i1];
            c[i][i1]=sum[i][i1]-sum[i][0];
        }
        insert(i,k);
    }
    printf("%d",max);
    return 0;
}

你可能感兴趣的:(POJ,Hash表,---Primary)