Gold Balanced Lineup(POJ--3274

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

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.
题意:每组输入n和k分别代表有n头牛,牛有k个特征,接下来输入n个数,第i个数转换成二进制数之后从右向左只要出现1的位置就表示第i头牛含有第j个特征,求最远的两行间各个特征出现次数相等的区间长度
思路:把“求最远的两行间各个特征出现次数相等”转化为“求最远的相同两行”,再用Hash查找。数组sum[i][j]表示从第1到第i头牛特征j的出现次数。所以题目要求等价为:求满足sum[i][0]-sum[j][0]=sum[i][1]-sum[j][1]=.....=sum[i][k-1]-sum[j][k-1] (j<i)中最大的i-j。将上式变换可得到sum[i][k-1]-sum[i][0] = sum[j][k-1]-sum[j][0]令C[i][y]=sum[i][y]-sum[i][0] (0<y<k)初始条件C[0][0~k-1]=0。所以只需求满足C[i][]==C[j][] 中最大的i-j,其中0<=j<i<=n。

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

In the range from cow #3 to cow #6 (of size 4), each feature appears in exactly 2 cows in this range

#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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值