您的位置:首页 > 编程语言 > Go语言

POJ Gold Balanced Lineup (哈希排序)

2017-08-29 17:27 155 查看
Gold Balanced Lineup

Time Limit: 2000MS Memory Limit: 65536K
Total 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 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

点击打开链接

点击打开链接

//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只牛是否有特征j
int 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;
}


#include <iostream>  
#include <vector>  
#include <stdio.h>  
#include <cstring>  
#define inf 999983  
  
using namespace std;  
int n,k;  
int num[100010];  
long long bit[100010][32];  
vector<vector<int> > vec;  
  
bool judge(int i,int j)  
{  
    int tool=-1;  
    for(int p=0;p<k;p++)  
    {  
        if(tool==-1||bit[i][p]-bit[j][p]==tool)  
            tool=bit[i][p]-bit[j][p];  
        else  
        {  
            return false;  
        }  
    }  
    return true;  
}  
  
int main()  
{  
   while(scanf("%d%d",&n,&k)==2)  
   {  
       vec.clear();  
       memset(num,0,sizeof num);  
       memset(bit,0,sizeof bit);  
        vec.resize(1000000);  
    for(int i=0;i<n;i++)  
    {  
        scanf("%d",num+i);  
    }  
    for(int i=1;i<=n;i++)  
    {  
        for(int j=0;j<k;j++)  
        {  
            bit[i][j]=bit[i-1][j];  
            if(num[i-1]&(1<<j))  
                bit[i][j]++;  
        }  
    }  
      int ans=0;  
      for(int i=0;i<=n;i++)  
      {  
          long long mi=1000000000;  
          long long tool=0;  
          for(int j=0;j<k;j++)  
            mi=min(mi,bit[i][j]);  
          for(int j=0;j<k;j++)  
           tool+=(long long)(1<<(j%15))*(bit[i][j]-mi);  
           tool%=inf;  
        for(int j=0;j<vec[tool].size();j++)  
        {  
            if(judge(i,vec[tool][j]))  
               ans=max(ans,i-vec[tool][j]);//,cout<<i<<" "<<vec[tool][j]<<endl;  
        }  
          //cout<<ans<<endl;  
        vec[tool].push_back(i);  
      }  
      printf("%d\n",ans);  
   }  
    return 0;  
}  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  哈希