poj 3368 Frequent values
http://poj.org/problem?id=3368
1 #include <cstdio> 2 #include <cstring> 3 #include <cmath> 4 #include <algorithm> 5 #define maxn 100100 6 using namespace std; 7 8 int a[maxn],f[maxn][30],n,q,x,y,b[maxn]; 9 10 int main() 11 { 12 while(scanf("%d",&n)!=EOF) 13 { 14 if(n==0) break; 15 scanf("%d",&q); 16 memset(f,0,sizeof(f)); 17 for(int i=1; i<=n; i++) 18 { 19 scanf("%d",&a[i]); 20 } 21 memset(b,0,sizeof(b)); 22 for(int i=1; i<=n; i++) 23 { 24 if(i==1) 25 { 26 b[1]=1; 27 continue; 28 } 29 if(a[i]==a[i-1]) 30 { 31 b[i]=b[i-1]+1; 32 } 33 else 34 b[i]=1; 35 } 36 for(int i=1; i<=n; i++) 37 { 38 f[i][0]=b[i]; 39 } 40 int m=(int)(log(n*1.0)/(log(2.0))); 41 for(int j=1; j<=m; j++) 42 { 43 for(int i=1; i<=n; i++) 44 { 45 if(i+(1<<j)-1<=n) 46 { 47 f[i][j]=max(f[i][j-1],f[i+(1<<(j-1))][j-1]); 48 } 49 } 50 } 51 for(int i=0; i<q; i++) 52 { 53 scanf("%d%d",&x,&y); 54 int t=x; 55 while(t<=y&&a[t]==a[t-1]) 56 t++; 57 int k=(int)(log(y-t+1.0)/(log(2.0))); 58 int c=max(f[t][k],f[y-(1<<k)+1][k]); 59 if(t>y) c=0; 60 printf("%d\n",max(t-x,c)); 61 } 62 } 63 return 0; 64 }