[POJ3274]-Gold Balanced Lineup

来源:互联网 发布:s曲线加速算法 博客 编辑:程序博客网 时间:2024/05/21 17:43
Gold Balanced Lineup
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 14483 Accepted: 4196

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 37672142

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
题目描述:
这个题目意思相信有一很大部分人都没看清楚,我的英语也是硬伤,看题目时看了半天一头雾水,弱弱的抱怨一句WTF!,所以默默的去大神的博客去弄懂题目,看了一会,题目终于看清楚了,后来我发现我没看Hint,其实想想没那么难懂,静下心好好看,多揣摩揣摩,题意也就明白了,可能有些童鞋题目遇到我这种问题,我弱弱的说下题目意思吧!剧情就不说了,我说说理解的重点:
每个牛有一个ID,把这个ID用二进制来表示,如果该位为1表示有这种特性,为0表示没有此特性,如果存在一个连续cowID他们每种特性和加起来是相等的,说明该连续cowID是一个平衡区间,要求求得最大的平衡区间。
解题思路:
通过以上描述题意应该明白了吧!
拿这个输入输出分析吧!第一行输入7 代表下面有7个cowID,3代表几种特性。接下来是7个cowID
1 7 ---------->1 1 1
2 6 ---------->1 1 0
3 7 ---------->1 1 1
4 2 ---------->0 1 0
5 1 ---------->0 0 1
6 4 ---------->1 0 0
7 2 ---------->0 1 0
可以清楚的看到3456这几个ID每种特征和相等都等于2,这个特征可以作为我们解题的关键,如果我们将其每种特性都累加起来:
1 7 ---------->1 1 1
2 6 ---------->2 2 1
3 7 ---------->3 3 2
4 2 ---------->3 4 2
5 1 ---------->3 4 3
6 4 ---------->4 4 3
7 2 ---------->4 5 3
然后同时减去第一列的值,
1 7 ---------->0 0 0
2 6 ---------->1 1 0
3 7 ---------->1 1 0
4 2 ---------->1 2 0
5 1 ---------->0 1 0
6 4 ---------->1 1 0
7 2 ---------->1 2 0
我们发现第二行和第六行相等,想想为什么会相等,很容易明白的,因为我们之前分析了3456cowID每种特性和加起来是相等的,那么你加到第六个cowID时相对于第二行来说,是不是每种特性都同时加上了一个random值,这里random值为2,然后你同时都减去最后一种特性,当然第六行每种特性也就等于第二行的每种特性,这样解释应该是比较通熟易懂了。
下面就到了最后的阶段,如何设计算法解题,我是看了一个大神的blog,根据他的思想写的,这里用到了一个对一个数组hash的算法,这个hashcode算法是从Unix下面常用的一个对字符串进行hash处理的方式借鉴过来的,也就是把char* 改为int* ,其余原封不动的搬了过来。
结构体变量cowb[]存储每个cowIDcow[i].b存储的是i cowID编号的二进制。
cnt[].b记录的就是累加后再减去最后一列的得特性值
代码如下
#include <cstdio>#include <iostream>#include <cmath>#include <cstring>#include <string>#include <algorithm>using namespace std;const int maxn = 1000001;const int prime = 1000003;const int INF = -1;struct node{int b[31];//存储每种特性}cowb[maxn],cnt[maxn];int n,k;int HASH[prime];int hashcode(const int *v,int m){int p=0;for(int i = 1;i <= m;++ i)p = ((p<<2)+(v[i]>>4))^(v[i]<<10);p=p%prime;p = p<0? (p+prime):p;return p;}void debug_print(){for(int j = 1;j <= k;++ j){printf("%d ",cnt[i].b[j]);}cout << endl;}int main(){freopen("data3274.in","r",stdin);while(~scanf("%d%d",&n,&k)){memset(cowb,0,n);memset(cnt,0,n);memset(HASH,INF,prime);HASH[hashcode(cnt[0].b,k)]=0;//初始化int res=0;for(int i = 1;i <= n;++ i){int cow;scanf("%d",&cow);for(int j = 1;j <= k;++ j){int b = cow%2;cowb[i].b[j]=b+cowb[i-1].b[j];cnt[i].b[j] = cowb[i].b[j]-cowb[i].b[1];cow/=2;}// debug_print();int p=hashcode(cnt[i].b,k);//每次获得该cowID处理后的hahs值while(HASH[p]!=-1){bool flag=true;//判断每个特性是否相同for(int j = 1;j <= k;++ j)if(cnt[i].b[j]!=cnt[HASH[p]].b[j]){flag=false;//如果有一个特性值不同就为falsebreak;}if(flag){res=max(res,i-HASH[p]);//若果找打了就把res更新break;}//printf("res = %d\n",res);p++;}if(HASH[p]==-1)//没有被赋值的话HASH[p]=i;//将其赋值为i cowID}printf("%d\n",res);}return 0;}

0 0