POJ_3274 Gold Balanced Lineup 解题报告

原创 2012年03月29日 19:05:55
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;
}



POJ3274-Gold Balanced Lineup

  • 2011年07月31日 00:07
  • 90KB
  • 下载

B - Gold Balanced Lineup解题报告(张浩盛伦)

B - Gold Balanced Lineup Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %...

POJ 3274 Gold Balanced Lineup (前缀和 + 哈希表)

POJ 3274 Gold Balanced Lineup (前缀和 + 哈希表)

POJ 3274 Gold Balanced Lineup hash

Gold Balanced Lineup Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 13416   Acce...

POJ3274-Gold Balanced Lineup

转载请注明出处:優YoU  http://user.qzone.qq.com/289065406/blog/1309220772   大致题意:   解题思路: 经典题,不转化问...

POJ---3274-Gold Balanced Lineup(hash)

Language: Default Gold Balanced Lineup Time Limit: 2000MS   Memory Limit: 65536K ...

POJ 3274 GOLD BALANCED LINEUP(HASH)

原文:http://blog.acmj1991.com/?p=616 题意:给你n头牛,同时每头牛不多于m种的特征。然后给你n个数第i个数表示第i头牛的特征,这个数的二进制的第j位表示第i头牛是否有...

POJ_3274_Gold Balanced Lineup_哈希

今天电工实习焊了一下午电路,已中毒。 题意: FJ有N ( 1 Input Line 1: Two space-separated integers, N and ...

【POJ】3274 Gold Balanced Lineup 哈希hash

对于这些英文的题目,我们的首要任务是搞清楚题目的意思。 说实话,我起初拿到这题的时候并不认为这题和哈希有半毛钱关系…… …… 以上都是O(n^2)的正常思路,可惜n的最大值为100000,铁定TLE。...
  • lyfsb
  • lyfsb
  • 2017年02月17日 11:30
  • 487

POJ---3274[Gold Balanced Lineup] 数组的hash

hash函数抄网上的,自己写的死活没过:  /*数组的hash*///思路://(1):sum[i][j]表示前i头牛第j个特征的总和//(2):sum[b][j]-sum[a-1][j];表示[a,...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:POJ_3274 Gold Balanced Lineup 解题报告
举报原因:
原因补充:

(最多只允许输入30个字)