poj3274Gold Balanced Lineup(哈希表)

来源:互联网 发布:java 泛型和通配符 编辑:程序博客网 时间:2024/06/08 00:43

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];哈希表处理数据,线性探测找出合适的key=(∑c[id][i]*i)%10000然后对表数据进行边输入边插入,比对然后求出maxn*/#include <cstdio>#include <cstring>#include <algorithm>#include <iostream>#include <queue>#include <cmath>using namespace std;#define mod 10000#define N 10010struct 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;}
原创粉丝点击