Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 13958 | Accepted: 4038 |
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 onlyK 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 featurei.
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 cowsi..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
Source
分析:对前i个奶牛的特长求前缀和,然后hash判断之前有没有出现过相对差一样的串,注意一开始的没有奶牛时也要插入hash。
#include <cstdio> #include <iostream> using namespace std; const int Func[31]={10000,1000,100,10,1,10,100,1000,1000,1000,100,10,100,1000,1000,1000,10,10,11,12,13,15,100,15,16,17,18,16,100,1000}; bool jud[400001]; int n,k,num,ans,val[400001],f[400001][31],now[200001],F[200001]; bool JUD(int s) { for(int i=1;i <= k;i++) if(f[s][i] != now[i]) return false; return true; } int Find() { int s=0; for(int i=1;i <= k;i++) s+=now[i]*Func[i]; s=s%230007; while(jud[s] && !JUD(s)) s++; if(jud[s]) return val[s]; return -1; } void insert(int x) { int s=0; for(int i=1;i <= k;i++) s+=now[i]*Func[i]; s=s%230007; while(jud[s]) s++; for(int i=1;i <= k;i++) f[s][i]=now[i]; jud[s]=true; val[s]=x; } int main() { scanf("%d %d",&n,&k); insert(0); for(int i=1;i <= n;i++) { scanf("%d",&num); int Min=2147483647; for(int j=1;j <= k;j++) { if(num & (1<<(j-1))) F[j]++; Min=min(Min,F[j]); } for(int j=1;j <= k;j++) now[j]=F[j]-Min; int pos=Find(); if(pos >= 0) ans=max(ans,i-pos); else insert(i); } cout<<ans<<endl; }