poj 3368 Frequent values(RMQ)
2016-02-08 23:37
471 查看
也算是一道模版题吧。
初始化为连续相同的数的个数,最后查询之前先从和区间第一个数不同的数开始查询,然后和区间内和第一个数相同的数的个数进行比较,输出最大值就好了。
初始化为连续相同的数的个数,最后查询之前先从和区间第一个数不同的数开始查询,然后和区间内和第一个数相同的数的个数进行比较,输出最大值就好了。
#include<iostream> #include<cstdio> #include<cstring> #include<math.h> #include<algorithm> using namespace std; const int INF=0x3f3f3f3f; const int maxn=100005; int n,m; int a[maxn]; int maxNum[maxn][32]; void RMQ(){ for(int j=1;j<=20;j++){ for(int i=1;i<=n;i++){ if(i+(1<<j)-1<=n){ maxNum[i][j]=max(maxNum[i][j-1],maxNum[i+(1<<(j-1))][j-1]); } } } } int main() { #ifndef ONLINE_JUDGE freopen("test.in","r",stdin); freopen("test.out","w",stdout); #endif while(~scanf("%d",&n)&&n){ scanf("%d",&m); for(int i=1;i<=n;i++) scanf("%d",&a[i]); maxNum[1][0]=1,a[0]=-INF; for(int i=2;i<=n;i++){ if(a[i]==a[i-1]) maxNum[i][0]=maxNum[i-1][0]+1; else maxNum[i][0]=1; } RMQ(); int p,q; while(m--){ scanf("%d%d",&p,&q); int tmp=p; while(tmp<=q&&(a[tmp]==a[tmp-1])) tmp++; int intervalMax; if(tmp>q) intervalMax=0; else{ int k=(int)log2(q-tmp+1); intervalMax=max(maxNum[tmp][k],maxNum[q-(1<<k)+1][k]); } printf("%d\n",max(intervalMax,tmp-p)); } } return 0; }
相关文章推荐
- 【BZOJ1367】sequence
- UIDynamic物理引擎
- LeetCode 48. Rotate Image My Submissions Question (矩阵旋转)
- HDU 3397 Sequence operation(线段树)
- CQRS, Task Based UIs, Event Sourcing agh!
- 在Qt中如何使用QtDesigner创建的UI文件
- Qt中Ui名字空间以及setupUi函数的原理和实现
- volley中多级别取消请求Request
- break和continue跳出指定for循环(for一些特点)
- continue break return的区别
- Codeforces Round #342 (Div. 2) A - Guest From the Past 数学
- continue break return的区别
- Android 异步查询框架AsyncQueryHandler的使用
- 关于RHEL6.x上avahi进程与Dell的Fluid Cache
- hdu4296 Buildings(贪心)
- 从交互设计角度,聊聊WEB网站和移动APP的六大差异
- How to Acquire or Improve Debugging Skills
- Codeforces Round #342 (Div. 2)--A. Guest From the Past
- POJ 3498 March of the Penguins(枚举+最大流)
- 获取通讯录(AddressBookUI框架)