关闭

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

题目链接:http://poj.org/problem?id=3274

算法类型:HASH;

解题思路:对于每头奶牛都有一个"feature ID",用一个整数表示,把这个数表示成K位二进制,对应位为1表示具有相应的特征,这样n头奶牛排成一排,求最长的连续区间,使得里面奶牛的全部K个特征个数达到平衡,即:

如果用sum[i][j] 表示前 i 头奶牛的第 j 个特征个数总和,则对于区间[a + 1, b],有
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)
如果令c[i][j] = sum[i][j] - sum[i][1] (1<= j <= K)
则条件可以转换为:c[a] == c[b]
这样我们就可以在求的 c[i][j] (1<= i <= N, 1 <= j <= K)的基础上,对数组c[i](1 <= i <= N) hash,用开散列法(拉链法)求解即可。
算法实现:
#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条
    文章存档
    最新评论