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
C[i][]==C[j][] 即二维数组C[][]第i行与第j行对应列的值相等,那么原题就转化为求C数组中 相等且相隔最远的两行的距离i-j。
以样例为例
7 3
7
6
7
2
1
4
2
先把7个十进制特征数转换为二进制,其逆序为:
7 à 1 1 1
6 à 0 1 1
7 à 1 1 1
2 à 0 1 0
1 à 1 0 0
4 à 0 0 1
2 à 0 1 0
(行数为cow编号,自上而下从1开始;列数为特征编号,自左到右从0开始)
再求sum数组,逐行累加得,sum数组为
1 1 1
1 2 2
2 3 3
2 4 3
3 4 3
3 4 4
3 5 4
再利用C[i][y]=sum[i][y]-sum[i][0]求C数组,即所有列都减去第一列
注意C数组有第0行,为全0
0 0 0 à 第0行
0 0 0
0 1 1
0 1 1
0 2 1
0 1 0
0 1 1
0 2 1
显然第2行与第6行相等,均为011,且距离最远,距离为6-2=4,这就是所求。
但是最大数据有10W个,即10W行,因此不能直接枚举找最大距离,必须用Hash查找相同行,找到相同行再比较最大距离。
注意C数组的值可能为负数,因此生成key值时要注意保证key为非负数。
Sample Input
7 3 7 6 7 2 1 4 2
Sample Output
4
Hint
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#define MAX 100005
#define mod 99991
using namespace std;
typedef struct node
{
int i,next; //i记录当前牛的编号,next记录相同key值的下一个牛的下标
} Node;
Node node[MAX];
int sum[MAX][31];
int c[MAX][31],n,k,maxlen;
int hsh[mod],cnt;
void init() //初始化
{
memset(hsh,-1,sizeof(hsh));
cnt=0;
maxlen=0;
}
void add(int key,int a) //添加键值为key,编号为a的牛
{
node[cnt].i=a;
node[cnt].next=hsh[key];
hsh[key]=cnt;
cnt++;
}
void serch(int key,int a)
{
int next,j;
if(hsh[key]==-1)
add(key,a); //如果不存在直接添加
else
{
for(next=hsh[key]; next!=-1; next=node[next].next) //如果存在相同键值的牛就开始遍历相同键值的牛,如果符合要求就求两牛之间的区间长度
{
for( j=0; j<k; j++)
{
if(c[a][j]!=c[node[next].i][j])
break;
}
if(j>=k)
{
maxlen=max(maxlen,a-node[next].i);
}
}
add(key,a); //然后再将此时的牛加入到键值为key的哈希表中
}
return ;
}
int main()
{
//freopen("lalala.text","r",stdin);
int key;
while(~scanf("%d %d",&n,&k))
{
init();
for(int i=0; i<k; i++) //初始化sum[0],c[0];
{
sum[0][k]=0;
c[0][k]=0;
}
key=0;
for(int j=1;j<k;j++)
key+=c[0][j]*j;
key=abs(key)%mod;
add(key,0); //要记得把第0头牛也要加到哈希表中
for(int i=1; i<=n; i++)
{
int x;
scanf("%d",&x);
key=0;
for(int j=0; j<k; j++)
{
sum[i][j]=sum[i-1][j]+x%2;
x=x/2;
c[i][j]=sum[i][j]-sum[i][0];
}
//for(int j=0;j<k;j++)
//printf("sum= %d c = %d\n",sum[i][j],c[i][j]);
for(int j=1; j<k; j++)
key+=c[i][j]*j;
key=abs(key)%mod; //尽量减少地址冲突
serch(key,i); //边输入边查找
}
printf("%d\n",maxlen);
}
return 0;
}