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
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 3 7 6 7 2 1 4 2
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只牛是否有特征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;
}