POJ Gold Balanced Lineup (哈希排序)
来源:互联网 发布:mac 如何加载字幕 编辑:程序博客网 时间:2024/06/05 17:14
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
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
Sample Input
7 37672142
Sample Output
4
Hint
Source
点击打开链接
点击打开链接
//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;}
- POJ Gold Balanced Lineup (哈希排序)
- POJ Gold Balanced Lineup
- Gold Balanced Lineup POJ
- POJ 3274 Gold Balanced Lineup (hash)
- POJ 3274 Gold Balanced Lineup (hash)
- POJ---3274-Gold Balanced Lineup(hash)
- POJ 3274 Gold Balanced Lineup
- POJ 3274 Gold Balanced Lineup
- POJ 3274 Gold Balanced Lineup
- POJ 3274 Gold Balanced Lineup
- POJ 3274 Gold Balanced Lineup
- poj 3274 Gold Balanced Lineup
- POJ 3274 Gold Balanced Lineup
- POJ 3274 Gold Balanced Lineup
- poj 3274 Gold Balanced Lineup
- poj 3274 Gold Balanced Lineup
- Gold Balanced Lineup(POJ--3274
- POJ 3274 Gold Balanced Lineup
- StackOverflowError栈满--无线递归与OutOfMemoryError内存溢出
- 【草稿】2017-8-29速记
- 好记性,不如烂笔头(sql篇)
- Hive分区
- 如何通过思维导图分析问题的6款优秀模板鉴赏
- POJ Gold Balanced Lineup (哈希排序)
- 装包裹问题
- 完美数列(暴力枚举)
- MySQL 触发器简单实例
- 暑末 Day2 T3
- UVA 100
- 1138- 数据结构上机测试2-1:单链表操作A
- eclipse 提示
- Java8源码-Set整体架构