标签:
题意:给出n个数和Q个询问(l,r),对于每个询问求出(l,r)之间连续出现次数最多的次数。
1 #include<cstdio> 2 #include<iostream> 3 #include<algorithm> 4 #include<cstring> 5 #include<cmath> 6 #include<queue> 7 #include<map> 8 using namespace std; 9 #define MOD 1000000007 10 const int INF=0x3f3f3f3f; 11 const double eps=1e-5; 12 typedef long long ll; 13 #define cl(a) memset(a,0,sizeof(a)) 14 #define ts printf("*****\n"); 15 int n,m,tt; 16 const int MAXN = 100005; 17 int dp[MAXN][20]; 18 int mm[MAXN]; 19 //初始化RMQ, b数组下标从1开始,从0开始简单修改 20 void initRMQ(int n,int b[]) 21 { 22 mm[0] = -1; 23 for(int i = 1; i <= n;i++) 24 { 25 mm[i] = ((i&(i-1)) == 0)?mm[i-1]+1:mm[i-1]; 26 dp[i][0] = b[i]; 27 } 28 for(int j = 1; j <= mm[n];j++) 29 for(int i = 1;i + (1<<j) -1 <= n;i++) 30 dp[i][j] = max(dp[i][j-1],dp[i+(1<<(j-1))][j-1]); 31 } 32 //查询最大值 33 int rmq(int x,int y) 34 { 35 int k = mm[y-x+1]; 36 return max(dp[x][k],dp[y-(1<<k)+1][k]); 37 } 38 int a[MAXN],f[MAXN]; 39 int main() 40 { 41 int i,j,k; 42 #ifndef ONLINE_JUDGE 43 freopen("1.in","r",stdin); 44 #endif 45 while(scanf("%d",&n)!=EOF) 46 { 47 if(n==0) break; 48 scanf("%d",&m); 49 for(i=1;i<=n;i++) 50 { 51 scanf("%d",&a[i]); 52 f[1]=1; 53 if(i!=1) 54 { 55 if(a[i]==a[i-1]) 56 { 57 f[i]=f[i-1]+1; 58 } 59 else 60 f[i]=1; 61 } 62 } 63 /*for(i=1;i<=n;i++) 64 { 65 printf("%d ",f[i]); 66 } 67 printf("\n");*/ 68 initRMQ(n,f); 69 while(m--) 70 { 71 int uu,v; 72 scanf("%d%d",&uu,&v); 73 int u=uu; 74 while(u<=v&&a[u]==a[u-1]) 75 { 76 u++; 77 } 78 int ans=rmq(u,v); 79 ans=max(u-uu,ans); 80 printf("%d\n",ans); 81 } 82 } 83 }
标签:
原文地址:http://www.cnblogs.com/cnblogs321114287/p/4620394.html