POJ3368Frequent values[RMQ 游程编码]
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 17581 | Accepted: 6346 |
Description
You are given a sequence of n integers a1 , a2 , ... , an in non-decreasing order. In addition to that, you are given several queries consisting of indices i and j (1 ≤ i ≤ j ≤ n). For each query, determine the most frequent value among the integers ai , ... , aj.
Input
The input consists of several test cases. Each test case starts with a line containing two integers n and q (1 ≤ n, q ≤ 100000). The next line contains n integers a1 , ... , an (-100000 ≤ ai ≤ 100000, for each i ∈ {1, ..., n}) separated by spaces. You can assume that for each i ∈ {1, ..., n-1}: ai ≤ ai+1. The following q lines contain one query each, consisting of two integers i and j (1 ≤ i ≤ j ≤ n), which indicate the boundary indices for the
query.
The last test case is followed by a line containing a single 0.
Output
For each query, print one line with one integer: The number of occurrences of the most frequent value within the given range.
Sample Input
10 3
-1 -1 1 1 1 1 3 10 10 10
2 3
1 10
5 10
0
Sample Output
1
4
3
Source
//
// main.cpp
// poj3368
//
// Created by Candy on 10/8/16.
// Copyright © 2016 Candy. All rights reserved.
// #include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
const int N=1e5+,INF=1e9;
inline int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x*f;
}
int n,q,l,r;
int x,last,cnt=,a[N],v[N],c[N],id[N],left[N],right[N];
int st[N][];
void initRMQ(){
memset(st,,sizeof(st));
for(int i=;i<=cnt;i++) st[i][]=c[i];
for(int j=;(<<j)<=cnt;j++)
for(int i=;i+(<<j)-<=cnt;i++)
st[i][j]=max(st[i][j-],st[i+(<<(j-))][j-]);
}
int rmq(int l,int r){
if(l>r) return ;
int k=log(r-l+)/log();
return max(st[l][k],st[r-(<<k)+][k]);
}
int main(int argc, const char * argv[]) {
while((n=read())){
q=read();
memset(c,,sizeof(c));
memset(left,,sizeof(left));
memset(right,,sizeof(right));
v[]=v[n+]=INF;
for(int i=;i<=n;i++){
v[i]=read();
if(v[i]==v[i-]){
c[cnt]++;
right[cnt]=i;
id[i]=cnt;
}else{
cnt++;
a[cnt]=v[i];
c[cnt]++;
left[cnt]=right[cnt]=i;
id[i]=cnt;
}
}
initRMQ();
//for(int i=1;i<=cnt;i++) printf("init %d %d %d %d\n",a[i],c[i],left[i],right[i]);
for(int i=;i<=q;i++){
l=read();r=read();
int ans=;
if(id[l]==id[r]) ans=r-l+;
else{
ans=max(right[id[l]]-l+,r-left[id[r]]+);
ans=max(ans,rmq(id[l]+,id[r]-));
}
printf("%d\n",ans);
}
}
//printf("\n\n\n%d %d %d %d",id[1]+1,c[2],id[10]-1,rmq(id[1]+1,id[10]-1));
return ;
}
POJ3368Frequent values[RMQ 游程编码]的更多相关文章
- poj3368Frequent values(RMQ)
http://poj.org/problem?id=3368 追完韩剧 想起这题来了 想用线段树搞定来着 结果没想出来..然后想RMQ 想出来了 算是离散吧 把每个数出现的次数以及开始的位置及结束的位 ...
- RMQ算法 以及UVA 11235 Frequent Values(RMQ)
RMQ算法 简单来说,RMQ算法是给定一组数据,求取区间[l,r]内的最大或最小值. 例如一组任意数据 5 6 8 1 3 11 45 78 59 66 4,求取区间(1,8) 内的最大值.数据量小 ...
- UVA 11235Frequent values(RMQ)
训练指南P198 题意:给出一个非降序排列的整数数组a1, a2…… an,你的任务是对于一系列询问(i,j),回答ai, ai+1 ……aj 中出现的次数最多的次数 这题不仅学到了rmq的应用还学到 ...
- poj 3368 Frequent values(RMQ)
/************************************************************ 题目: Frequent values(poj 3368) 链接: http ...
- POJ 3368 Frequent values RMQ ST算法/线段树
Frequent values Time Limit: 2000MS Memory Lim ...
- UVA 11235 Frequent Values ---RMQ
大白书上的例题,具体讲解见大白书,最好用用一个Log数组直接求k,这样就是纯O(1)了 #include <iostream> #include <cstdio> #inclu ...
- [poj3368]Frequent values(rmq)
题意:给出n个数和Q个询问(l,r),对于每个询问求出(l,r)之间连续出现次数最多的次数. 解题关键:统计次数,转化为RMQ问题,运用st表求解,注意边界. 预处理复杂度:$O(n\log n)$ ...
- POJ 3368 Frequent values(RMQ 求区间出现最多次数的数字的次数)
题目链接:http://poj.org/problem? id=3368 Description You are given a sequence of n integers a1 , a2 , .. ...
- UVa 11235 Frequent values (RMQ && 区间出现最多次的数的次数)
题意 : 给出一个长度为 n 的不降序序列,并且给出 q 个形如(L, R)的问询,问你这个区间出现的最多次的数的次数. 分析 : 很自然的想到将区间“缩小”,例如1 1 2 3 3 3就可以变成2 ...
随机推荐
- html 空链接 href="#"与href="javascript:void(0)"的区别
#包含了一个位置信息 默认的锚是#top 也就是网页的上端 而javascript:void(0) 仅仅表示一个死链接 这就是为什么有的时候页面很长浏览链接明明是#但跳动到了页首 而javascrip ...
- GridView嵌套在ScrollView里只有一行的问题
遇到这个问题 网上找到的解决办法: 方法一:就是上面说的通过计算出来ListView或者GridView中的子列高度和 进行显示:public void setListViewHeightBasedO ...
- MySQL的SSL加密连接与性能开销
本文转载自:http://www.innomysql.net/article/23959.html(只作转载, 不代表本站和博主同意文中观点或证实文中信息) Contents [hide] 1 前言 ...
- Linux下清理内存和Cache方法 /proc/sys/vm/drop_caches
Linux下清理内存和Cache方法 /proc/sys/vm/drop_caches 频繁的文件访问会导致系统的Cache使用量大增 $ free -m total used free shared ...
- 天津政府应急系统之GIS一张图(arcgis api for flex)讲解(十一)路径导航模块
config.xml文件的配置如下: <widget label="路径导航" icon="assets/images/lujingdaohang.png" ...
- K 均值算法(K-means)
K-means算法是最简单的一种聚类算法.算法的目的是使各个样本与所在类均值的误差平方和达到最小(这也是评价K-means算法最后聚类效果的评价标准) K-means聚类算法的一般步骤: 1. 初始化 ...
- centos虚拟机复制移动后网络配置无效
移植Centos虚拟机后无法联网解决1.迁移以后,会存在其中一个网卡无法启动(eth0 or eth1) [root@ ~]# ifup eth0 WARNING: Deprecated config ...
- 玩转大麦盒子airplay
长城宽待送了大麦盒子,一直没怎么用,既然是安卓的系统,那估计可以安装很多的软件吧,今天演练了一下. 大麦盒子 规格和介绍 http://baike.so.com/doc/7487612.html 有G ...
- Android内存泄漏
Java是垃圾回收语言的一种,其优点是开发者无需特意管理内存分配,降低了应用由于局部故障(segmentation fault)导致崩溃,同时防止未释放的内存把堆栈(heap)挤爆的可能,所以写出来的 ...
- AsyncHttpClient 源码分析
上一篇文章从功能和用法上对AsyncHttpClient做了个大致介绍,今天我们和以往一样,从内部实现.原理的角度带领大家看看 其工作机制,以便加深理解.写程序越多,我发现每接触一个新东西,都会有强烈 ...