POJ_3274 Gold Balanced Lineup 解题报告

318人阅读 评论(0)
Gold Balanced Lineup
 Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 8040 Accepted: 2379

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.

Sample Input

7 3
7
6
7
2
1
4
2

Sample Output

4

sum[b][1] - sum[a][1] = sum[b][2] - sum[a][2] = sum[b][j] - sum[a][j]   (1<= j <= K)

sum[b][1] - sum[b][2] = sum[a][1] - sum[a][2]
sum[b][1] - sum[b][j] = sum[a][1] - sum[a][j]   (1<= j <= K)

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAXN 100010
#define HASH 99991
int s[MAXN][30],c[MAXN][30];
int N,K;
int count(int a,int b)
{
for(int j=0;j<K;j++)
{
if(c[a][j]!=c[b][j])
return 0;
}
return b-a;
}
struct COW
{
struct COW(){next=NULL;};  //构造函数；
int num;
COW *next;     //链表；
}hash[HASH];
int main()
{
int i,j,a,sumc,max;
while(scanf("%d %d",&N,&K)!=EOF)
{
max=0;
memset(s,0,sizeof(s));
memset(hash,0,sizeof(hash));
COW *p=(COW*)malloc(sizeof(COW));
hash[0].num=0,hash[0].next=p,p->next=NULL;
for(i=1;i<=N;i++)
{
scanf("%d",&a);
for(j=0,sumc=0;j<K;j++)
{
s[i][j]=a%2+s[i-1][j];
a=a/2;
c[i][j]=s[i][j]-s[i][0];
sumc+=c[i][j];
}
int t=sumc%HASH;
if(t<0)t=-t;
COW *p=&hash[t];
for(;p->next!=NULL;p=p->next)
{
int ok=count(p->num,i);
if(ok!=0 && ok>max)max=ok;
}
COW *q=(COW*)malloc(sizeof(COW));
p->num=i;
p->next=q;
q->next=NULL;
}
printf("%d\n",max);
}
return 0;
}


3
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：12919次
• 积分：344
• 等级：
• 排名：千里之外
• 原创：18篇
• 转载：7篇
• 译文：0篇
• 评论：7条
文章存档
阅读排行
最新评论