Gold Balanced Lineup
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 13606 Accepted: 3956
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
题意:
有N头牛,这些牛各有一些特性。有一个区间,这个区间里的牛的每一个特性的总数是一样的。问这个区间最长有多长。
题解:
很好的一道题目,首先我们考虑G[i][j]为前i头牛有j特性的总数。那么如果区间[i,j]是一个满足题意得区间,则必有G[i][k] - G[j][k]对每一个特性都是一样大小的。于是可能会想到在i时向前遍历所有的j,显然这么做会超时的。我们来变形一下,当i,j为满足条件的两个位置时。有G[i][0] - G[j][0] = G[i][1] - G[j][1] = G[i][2] - G[j][2]…即为G[i][1] - G[i][0] = G[j][1] - G[j][0],G[i][2] - G[i][0] = G[j][2] - G[j][0]…那么我们可以用G[i][j]存储G[i][j] - G[i][0],然后只需要查找有没有和G[i][j]一样的就可以了。关键点是我们把这样一个两个状态的对应关系变成了一个状态的内在属性,那么我们只需要找另一个状态是否有这样的属性就可以。然后就可以用上查找技术。
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cmath>
using namespace std;
#define f(i,a,b) for(i = a;i<=b;i++)
#define fi(i,a,b) for(i = a;a>=b;i--)
#define M 100007
struct Po{
int v;
Po* next;
};
int N,K;
Po* Hash[M+5];
int G[M+5][40];
void init(void){
int i;
f(i,0,M+3)
Hash[i] = NULL;
Po* tem = (Po*)malloc(sizeof(Po));
tem->v = 0;
tem->next = NULL;
Hash[0] = tem;
}
void Insert(int a){
int key = 0;
int i;
f(i,1,K-1){
long long ai = G[a][i];
key+=(ai*ai)%M;
}
key%=M;
Po* tem = (Po*)malloc(sizeof(Po));
tem->next = NULL;
tem->v = a;
if(Hash[key] == NULL)
Hash[key] = tem;
else{
tem->next = Hash[key];
Hash[key] = tem;
}
}
bool IsTrue(int k,int v){
int i;
f(i,1,K-1)
if(G[k][i]!=G[v][i])
return false;
return true;
}
int Find(int k){
int key = 0;
int i,Max = 0;
f(i,1,K-1){
long long ai = G[k][i];
key+=(ai*ai)%M;
}
key%=M;
if(Hash[key]==NULL)
return 0;
else{
Po* p = Hash[key];
while(p!=NULL){
if(IsTrue(k,p->v)){
if(abs(k-p->v)>Max)
Max = abs(k-p->v);
}
p = p->next;
}
}
return Max;
}
int main()
{
while(~scanf("%d%d",&N,&K)){
memset(G,0,sizeof(G));
int i,j;
f(i,1,N){
int key;
scanf("%d",&key);
int cnt = 0;
for(j = 1;j<1<<K;j<<=1){
if(key&j){
G[i][cnt] = G[i-1][cnt]+1;
}else
G[i][cnt] = G[i-1][cnt];
cnt++;
}
}
init();
f(i,1,N){
f(j,1,K-1)
G[i][j] -= G[i][0];
Insert(i);
}
int Max = 0;
f(i,1,N){
int k = Find(i);
if(k>Max)
Max = k;
}
printf("%d\n",Max);
}
return 0;
}