poj3274Gold Balanced Lineup(哈希表)

poj3274Gold Balanced Lineup
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 15721 Accepted: 4511
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

/*
题意:有最多十万头牛,每头牛特征最多为30个,特征用二进位表示,求出距离最远且特征相同的牛的距离
即sum[i][0]-sum[j][0]=sum[i][1]-sum[j][1]=.........
求出最大的i-j的值
上式可继续推:
sum[i][1]-sum[i][0]=sum[j][1]-sum[j][0]=...........

可设:c[i][j]=sum[i][j]-sum[i][0](0<j<k);

则问题变为求c[i][]=c[j][]中i-j的最大值
也就是求c[][]数组中相同值时距离最远的距离

用sum[i][j]表示在前i头牛身上的第j个特征出现次数,f[i][j]为第i头牛,特征的二进制表示,c[i][j]为。
推出
sum[i][j]=sum[i-1][j]+f[i][j];


c[i][j]=sum[i][j]-sum[i][0];

*/
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <queue>
#include <cmath>
using namespace std;
#define mod 10000
#define N 10010
struct node
{
    int id,is;
}hash[N][55];
int sum[N*10][30],f[N*10][30],c[N*10][30];
int n,k;
int maxn=0;
int is_ok(int t1,int t2,int k)///对两个牛的特征比较
{
    for(int i=0;i<k;i++)
    {
        if(c[t1][i]!=c[t2][i])
        return 0;
    }
    return 1;
}

void inset(int t,int k)
{
    int i,key=0;
    for(i=0;i<k;i++)
    {
        key+=c[t][i]*i;///找出合适的key
    }
    key=abs(key)%mod;

    for(i=0;hash[key][i].is==1;i++)
    {
        if(is_ok(hash[key][i].id,t,k)==1&&t-hash[key][i].id>maxn)
        {
            maxn=t-hash[key][i].id;///求出距离最大
            return;
        }
    }
    hash[key][i].is=1;
    hash[key][i].id=t;
}

int main()
{
    int x;
    memset(hash,0,sizeof(hash));
    scanf("%d %d",&n,&k);
    for(int i=0;i<k;i++)
    {
        sum[0][i]=0;
        f[0][i]=0;
    }
    hash[0][0].id=0;
    hash[0][0].is=1;
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&x);
        for(int j=0;j<k;j++)
        {
            f[i][j]=x%2;///计算位
            x/=2;
            sum[i][j]=sum[i-1][j]+f[i][j];
            c[i][j]=sum[i][j]-sum[i][0];
        }
        inset(i,k);
    }
    cout<<maxn<<endl;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值