Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 14483 | Accepted: 4196 |
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
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
#include <cstdio>
#include <iostream>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
using namespace std;
const int maxn = 1000001;
const int prime = 1000003;
const int INF = -1;
struct node
{
int b[31];//存储每种特性
}cowb[maxn],cnt[maxn];
int n,k;
int HASH[prime];
int hashcode(const int *v,int m)
{
int p=0;
for(int i = 1;i <= m;++ i)
p = ((p<<2)+(v[i]>>4))^(v[i]<<10);
p=p%prime;
p = p<0? (p+prime):p;
return p;
}
void debug_print()
{
for(int j = 1;j <= k;++ j)
{
printf("%d ",cnt[i].b[j]);
}
cout << endl;
}
int main()
{
freopen("data3274.in","r",stdin);
while(~scanf("%d%d",&n,&k))
{
memset(cowb,0,n);
memset(cnt,0,n);
memset(HASH,INF,prime);
HASH[hashcode(cnt[0].b,k)]=0;//初始化
int res=0;
for(int i = 1;i <= n;++ i)
{
int cow;
scanf("%d",&cow);
for(int j = 1;j <= k;++ j)
{
int b = cow%2;
cowb[i].b[j]=b+cowb[i-1].b[j];
cnt[i].b[j] = cowb[i].b[j]-cowb[i].b[1];
cow/=2;
}
// debug_print();
int p=hashcode(cnt[i].b,k);//每次获得该cowID处理后的hahs值
while(HASH[p]!=-1)
{
bool flag=true;//判断每个特性是否相同
for(int j = 1;j <= k;++ j)
if(cnt[i].b[j]!=cnt[HASH[p]].b[j])
{
flag=false;//如果有一个特性值不同就为false
break;
}
if(flag)
{
res=max(res,i-HASH[p]);//若果找打了就把res更新
break;
}
//printf("res = %d\n",res);
p++;
}
if(HASH[p]==-1)//没有被赋值的话
HASH[p]=i;//将其赋值为i cowID
}
printf("%d\n",res);
}
return 0;
}