POJ Gold Balanced Lineup (哈希排序)

来源:互联网 发布:mac 如何加载字幕 编辑:程序博客网 时间:2024/06/05 17:14

Gold Balanced Lineup
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 15885 Accepted: 4552

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..jis 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 37672142

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

点击打开链接

点击打开链接

//Memory  Time//44152K 1141MS#include<iostream>#include<cmath>#include<stdlib.h>#include<string.h>using namespace std;const int size=100001;const int mod=99991;int feature[size][30];  //feature[i][j]标记第i只牛是否有特征jint sum[size][30];  //从第1到第i只牛,特征j总共出现了sum[i][j]次int c[size][30];  //c[i][j]=sum[i][j]-sum[i][0] , 即所有列都减去第一列后,值保存在c[][]int N;  //牛数量int K;  //特征数int MaxLen;  //最大距离typedef class HASH{    public:        int pi;  //保存c[i][j]的行地址c[i]的下标i        class HASH* next;        HASH()        {            next=0;        }}HashTable;HashTable* hash[mod];/*检查c[ai][]与c[bi][]是否对应列相等*/bool cmp(int ai,int bi){    for(int j=0;j<K;j++)        if(c[ai][j]!=c[bi][j])            return false;    return true;}void Hash(int ci)//把第ci只牛先放入哈希表{    int key=0;   //生成关键字    for(int j=1;j<K;j++)        key+=c[ci][j]*j;    key=abs(key)%mod;  //由于c[][]有正有负,因此key值可能为负数    if(!hash[key])  //新key,建立    {        HashTable* pn=new HashTable;        pn->pi=ci;        hash[key]=pn;    }    else  //key值冲突,已有key。则把两行行数相减    {        HashTable* pn=hash[key];        if(cmp(pn->pi,ci))        {            int dist=ci-(pn->pi);            if(MaxLen<dist)                MaxLen=dist;            return;  //由于pi与ci对应列数字相等,且pi地址必定比ci小        }            //而要求的是最大距离,因此不需要保存ci,判断距离后直接返回        else        {            while(pn->next)//往后搜索            {                if(cmp(pn->next->pi,ci))                {                    int dist=ci-(pn->next->pi);                    if(MaxLen<dist)                        MaxLen=dist;                    return;                }                pn=pn->next;            }            //地址冲突但c[][]各列的值不完全相同            HashTable* temp=new HashTable;//建个新的            temp->pi=ci;            pn->next=temp;        }    }    return;}int main(void){    //freopen("in.txt","r",stdin);    while(cin>>N>>K)    {        /*Initial*/        for(int p=0;p<K;p++)        {            c[0][p]=0; //第0只牛的特征默认为全0            sum[0][p]=0;        }        memset(hash,0,sizeof(hash));        Hash(0);  //把第0只牛先放入哈希表        MaxLen=0;        /*Input*/        for(int i=1;i<=N;i++)        {            int Dexf;   //十进制特征数            cin>>Dexf;            for(int j=0;j<K;j++)            {                feature[i][j]=Dexf%2;  //Dexf转换为逆序二进制                Dexf/=2;                sum[i][j]=sum[i-1][j]+feature[i][j];                c[i][j]=sum[i][j]-sum[i][0];            }            Hash(i);//把第i只牛先放入哈希表        }        /*Output*/        cout<<MaxLen<<endl;    }    return 0;}


  1. #include <iostream>  
  2. #include <vector>  
  3. #include <stdio.h>  
  4. #include <cstring>  
  5. #define inf 999983  
  6.   
  7. using namespace std;  
  8. int n,k;  
  9. int num[100010];  
  10. long long bit[100010][32];  
  11. vector<vector<int> > vec;  
  12.   
  13. bool judge(int i,int j)  
  14. {  
  15.     int tool=-1;  
  16.     for(int p=0;p<k;p++)  
  17.     {  
  18.         if(tool==-1||bit[i][p]-bit[j][p]==tool)  
  19.             tool=bit[i][p]-bit[j][p];  
  20.         else  
  21.         {  
  22.             return false;  
  23.         }  
  24.     }  
  25.     return true;  
  26. }  
  27.   
  28. int main()  
  29. {  
  30.    while(scanf("%d%d",&n,&k)==2)  
  31.    {  
  32.        vec.clear();  
  33.        memset(num,0,sizeof num);  
  34.        memset(bit,0,sizeof bit);  
  35.         vec.resize(1000000);  
  36.     for(int i=0;i<n;i++)  
  37.     {  
  38.         scanf("%d",num+i);  
  39.     }  
  40.     for(int i=1;i<=n;i++)  
  41.     {  
  42.         for(int j=0;j<k;j++)  
  43.         {  
  44.             bit[i][j]=bit[i-1][j];  
  45.             if(num[i-1]&(1<<j))  
  46.                 bit[i][j]++;  
  47.         }  
  48.     }  
  49.       int ans=0;  
  50.       for(int i=0;i<=n;i++)  
  51.       {  
  52.           long long mi=1000000000;  
  53.           long long tool=0;  
  54.           for(int j=0;j<k;j++)  
  55.             mi=min(mi,bit[i][j]);  
  56.           for(int j=0;j<k;j++)  
  57.            tool+=(long long)(1<<(j%15))*(bit[i][j]-mi);  
  58.            tool%=inf;  
  59.         for(int j=0;j<vec[tool].size();j++)  
  60.         {  
  61.             if(judge(i,vec[tool][j]))  
  62.                ans=max(ans,i-vec[tool][j]);//,cout<<i<<" "<<vec[tool][j]<<endl;  
  63.         }  
  64.           //cout<<ans<<endl;  
  65.         vec[tool].push_back(i);  
  66.       }  
  67.       printf("%d\n",ans);  
  68.    }  
  69.     return 0;  
  70. }  




原创粉丝点击