POJ 3274 Gold Balanced Lineup
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 10516 | Accepted: 3145 |
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 Kdifferent 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
Output
Sample Input
7 3 7 6 7 2 1 4 2
Sample Output
4
Hint
1 #include<iostream> 2 #include<cstdio> 3 #include<cmath> 4 #include<cstring> 5 6 using namespace std; 7 8 const long mod=99991; 9 10 typedef class node 11 { 12 public: 13 long pi; 14 class node *next; 15 node() 16 { 17 next=NULL; 18 } 19 } HashTable; 20 21 HashTable *cow[mod]; 22 23 int feature[100001][30]; 24 int sum[100001][30]; 25 int c[100001][30]; 26 int k; 27 long n,maxlen; 28 29 bool cmp(long ai,long bi) 30 { 31 for(int j=0;j<k;j++) 32 if(c[ai][j]!=c[bi][j]) 33 return false; 34 return true; 35 } 36 37 void Hash(long ci) 38 { 39 long key=0; 40 for(int j=1;j<k;j++) 41 key+=c[ci][j]*j; 42 key=((long)fabs((double)key))%mod; 43 if(!cow[key]) 44 { 45 HashTable *temp=new HashTable; 46 temp->pi=ci; 47 cow[key]=temp; 48 } 49 else 50 { 51 if(cmp(cow[key]->pi,ci)) 52 { 53 long dist=ci-(cow[key]->pi); 54 if(dist>maxlen) 55 maxlen=dist; 56 return; 57 } 58 else 59 { 60 HashTable *p=cow[key]; 61 while(p->next) 62 { 63 if(cmp(p->next->pi,ci)) 64 { 65 long dist=ci-(p->next->pi); 66 if(dist>maxlen) 67 maxlen=dist; 68 return; 69 } 70 p=p->next; 71 } 72 HashTable *temp=new HashTable; 73 temp->pi=ci; 74 p->next=temp; 75 } 76 } 77 } 78 79 int main() 80 { 81 long temp; 82 83 while(scanf("%ld %d",&n,&k)==2) 84 { 85 for(int i=0;i<k;i++) 86 { 87 c[0][i]=0; 88 sum[0][i]=0; 89 } 90 memset(cow,0,sizeof(cow)); 91 Hash(0); 92 maxlen=0; 93 94 for(int i=1;i<=n;i++) 95 { 96 scanf("%ld",&temp); 97 for(int j=0;j<k;j++) 98 { 99 feature[i][j]=temp&1; 100 temp>>=1; 101 sum[i][j]=sum[i-1][j]+feature[i][j]; 102 c[i][j]=sum[i][j]-sum[i][0]; 103 } 104 Hash(i); 105 } 106 107 printf("%ld\n",maxlen); 108 } 109 110 return 0; 111 }