[POJ] 3368 / [UVA] 11235 - Frequent values [ST算法]
2007/2008 ACM International Collegiate Programming Contest
University of Ulm Local Contest
Problem F: Frequent values
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 Specification
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 Specification
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 题解:因为序列a1,a2,a3...an是不下降的,所以相同的数字肯定连续集中在一段。维护d[i][0]为相同数字从左到右分别从1到n开始编号,例如样例中的d[i][0]从左到右为 1,2,1,2,3,4,1,1,2,3。这样就转化成了RMQ问题。因为没有中途修改a[i]的值,所以可以用Sparse-Table算法,d[i][j]表示从i开始长度为2^i的一段元素的最大值。维护数组Left[i],Right[i]分别为相同数字连续一段最左端的编号和最右端的编号,例如样例中的Left[i]分别为:1,1,3,3,3,3,7,8,8,8。所以,序列分布共有以下几种情况:![]()
[1] ans=max(Right[i]-L+1,R-Left[j]+1)[2] ans=R-L+1
[3] ans=max(max(Right[i]-L+1,R-Left[j]+1),RMQ(k)) 代码:
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<ctype.h>
#include<stdlib.h>
#include<stdbool.h> #define rep(i,a,b) for(i=(a);i<=(b);i++)
#define red(i,a,b) for(i=(a);i>=(b);i--)
#define clr(x,y) memset(x,y,sizeof(x))
#define sqr(x) (x*x)
#define LL long long int i,j,n,m,maxn,q,
a[],d[][],Left[],Right[]; void pre()
{
clr(d,);
clr(Left,);
clr(Right,);
clr(a,);
} int max(int a,int b)
{
if(a>b) return a;
return b;
} int RMQ_init()
{
int i,j; for(j=;(<<j)<=n;j++)
for(i=;i+(<<j)-<=n;i++)
d[i][j]=max(d[i][j-],d[i+(<<(j-))][j-]); return ; } int RMQ(int L,int R)
{
int k;
k=; while(<<(k+)<=R-L+) k++; maxn=max(d[L][k],d[R-(<<k)+][k]);; return ; } int Num_init()
{
int i; a[]=;
rep(i,,n)
if(a[i]!=a[i-]) Left[i]=i;
else Left[i]=Left[i-]; a[n+]=;
red(i,n,)
if(a[i]!=a[i+]) Right[i]=i;
else Right[i]=Right[i+]; return ;
} int main()
{
int i,x,y; while(true) {
pre();
scanf("%d",&n); if(n==) exit();
scanf("%d",&q);
a[]=;
rep(i,,n) {
scanf("%d",&a[i]);
if(a[i]!=a[i-]) d[i][]=;
else d[i][]=d[i-][]+; } Num_init();
RMQ_init(); while(q--) {
scanf("%d%d",&x,&y);
if(Left[x]==Left[y]) printf("%d\n",y-x+);
else {
if(Right[x]+==Left[y]) printf("%d\n",max(Right[x]-x+,y-Left[y]+));
else {
RMQ(Right[x]+,Left[y]-);
printf("%d\n",max(max(Right[x]-x+,y-Left[y]+),maxn));
}
}
}
} return ;
}
[POJ] 3368 / [UVA] 11235 - Frequent values [ST算法]的更多相关文章
- POJ 3368 & UVA 11235 - Frequent values
题目链接:http://poj.org/problem?id=3368 RMQ应用题. 解题思路参考:http://blog.csdn.net/libin56842/article/details/4 ...
- RMQ算法 以及UVA 11235 Frequent Values(RMQ)
RMQ算法 简单来说,RMQ算法是给定一组数据,求取区间[l,r]内的最大或最小值. 例如一组任意数据 5 6 8 1 3 11 45 78 59 66 4,求取区间(1,8) 内的最大值.数据量小 ...
- UVA - 11235 Frequent values
2007/2008 ACM International Collegiate Programming Contest University of Ulm Local Contest Problem F ...
- UVA 11235 Frequent values(RMQ)
Frequent values TimeLimit:3000Ms , ... , an in non-decreasing order. In addition to that, you are gi ...
- UVA 11235 Frequent values 线段树/RMQ
vjudge 上题目链接:UVA 11235 *******************************************************大白书上解释**************** ...
- UVa 11235 Frequent values (RMQ && 区间出现最多次的数的次数)
题意 : 给出一个长度为 n 的不降序序列,并且给出 q 个形如(L, R)的问询,问你这个区间出现的最多次的数的次数. 分析 : 很自然的想到将区间“缩小”,例如1 1 2 3 3 3就可以变成2 ...
- UVA 11235 Frequent Values ---RMQ
大白书上的例题,具体讲解见大白书,最好用用一个Log数组直接求k,这样就是纯O(1)了 #include <iostream> #include <cstdio> #inclu ...
- 数据结构(RMQ):UVAoj 11235 Frequent values
Frequent values You are given a sequence of n integers a1 , a2 , ... , an in non-decreasing order. I ...
- 11235 - Frequent values
<算法竞赛入门经典-训练指南>P198 记录一下区间的左右边界就可以了 #include <iostream> #include <stack> #include ...
随机推荐
- ubuntu apt 命令参数(转)
apt-get是一条linux命令,适用于deb包管理式的操作系统,主要用于自动从互联网的软件仓库中搜索.安装.升级.卸载软件或操作系统. apt-get update 在修改/etc/apt/sou ...
- QT 串口通信 数据16进制发送
在QT中进行串口通信时,很多情况要用到发送16进制的数据.从网上找来了一段代码测试能用: static QByteArray QString2Hex(QString str) { QByteArray ...
- C#读取文件高效方法实现
C# Code 12345678910111213141516171819202122232425262728293031 private void button1_Click ...
- iOS 9之New System Fonts(San Francisco 字体)
金田 此次苹果发布的iOS 9系统测试版目前已经开放下载,新系统将弃用Helvetica字体,改用了San Francisco字体, 包括系统菜单.App名称等各个部分. 最初San Francisc ...
- (7)如何得到所有的 "水仙花数" ?
本程序转载自:如何得到所有的水仙花数 感谢Android_iPhone(日知己所无),preferme(冰思雨)等人: package test; import java.math.BigIntege ...
- sgu495:概率dp / 推公式
概率题..可以dp也可以推公式 抽象出来的题目大意: 有 n个小球,有放回的取m次 问 被取出来过的小球的个数的期望 dp维护两个状态 第 i 次取出的是 没有被取出来过的小球的 概率dp[i] 和 ...
- 掌握 Java 8 Lambda 表达式
Lambda 表达式 是 Java8 中最重要的功能之一.使用 Lambda 表达式 可以替代只有一个函数的接口实现,告别匿名内部类,代码看起来更简洁易懂.Lambda 表达式 同时还提升了对 集合 ...
- [ES6] ITERATORS
Iterables return an iterator object. This object knows how to access items from a collection 1 at a ...
- qwtplot3D安装及运行-----终结解决方案
..\qwtplot3d\include\qwt3d_openglhelper.h:67: 错误:'gluErrorString' was not declared in this scope..\q ...
- C-冒泡排序,选择排序,数组
——构造类型 ->数组 ->一维数组 ->相同类型的一组数据 ->类型修饰符--数组名—[数组的元素个数(必须是整型表达式或者是整型常量,不能是变 ...