题目传送门:http://poj.org/problem?id=3368
题解传送门:http://www.cnblogs.com/frog112111/p/3306280.html
这道题我又看了题解(捂脸),题解大家看链接的吧。还有就是我的代码较拙,跑了1s多。。。。。。
#include<cstdio> #include<cstring> #include<map> #include<iostream> using namespace std; const int maxn=100010; map<int,int> c; int n,q,f[maxn][20],num[maxn]; void init() { int i,j; for (j=1;(1<<j)<=n;j++) for (i=1;i+(1<<j)-1<=n;i++) f[i][j]=max(f[i][j-1],f[i+(1<<(j-1))][j-1]); } int query(int l,int r) { int k=0; if (l>r)return 0; while ((1<<(k+1))<=(r-l+1))k++; return max(f[l][k],f[r-(1<<k)+1][k]); } int main() { while (scanf("%d",&n) && n) { int i,j,l,k,ans; scanf("%d",&q); c.clear(); memset(f,0,sizeof(f)); memset(num,0,sizeof(num)); for (i=1;i<=n;i++) { scanf("%d",&num[i]); c[num[i]]++; f[i][0]=c[num[i]]; } init(); for (i=1;i<=q;i++) { scanf("%d%d",&j,&l); k=j; while (num[k]==num[k-1] && k<=l)k++; ans=query(k,l); if (k!=j)ans=max(k-j,ans); printf("%d ",ans); } } return 0; }