[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 ...
随机推荐
- 把zlog封装成模块,隐藏zlog
mylog.h #ifndef _MY_LOG_H #define _MY_LOG_H int init(char *filename); void *get_category(char * cate ...
- 『安全工具』目录扫描 DirBuster AND 御剑
要想熟悉目标网站的体系架构,知道网站有哪些目录是必不可少的 向AWVS,Burp类大型扫描工具也可以进行目录扫描,不过个人感觉远没有专业扫描工具来的简单,实在 0x 01 DirBuster 简介:D ...
- PHP 之mysql空字符串问题
有一张user表如下所示:字段name不能为空. CREATE TABLE `user` ( `id` ) NOT NULL AUTO_INCREMENT, `name` ) NOT NULL, `a ...
- PHP安装OPENSSL扩展模块
新项目上线时,PHP开发同事反映邮件功能不能正常使用. 原来是用465的SMTP加密端口,不是25端口.那要为当前的PHP安装OPENSSL扩展啦. 还好,网上有很多,弄一个过来就搞定. http:/ ...
- Html5/Css3 向下兼容placeholder
Css3下input标签的placeholder属性在IE10以下是不兼容的,页面加入这段JS脚本后,能够兼容IE6+ //@charset "utf-8"; /** * jque ...
- [VBA]发布一个计算桩号之差的Excel自定义函数(VBA)
这是一个可以计算桩号之差(也就是得到长度)的Excel(或WPS)扩展函数,可以减少工程师在统计工程量时的工作量. 该函数具有一定的通用性.可以在MS Office和金山WPS上使用. 文末会给出使用 ...
- bzoj1047-理想的正方形(二维单调队列)
题意: 给一个矩阵,给出行列和每个数,再给出一个N,求出所有N*N的子矩阵中最大值最小值之差的最小值解析: 暴力枚举肯定不行,这题可以用二维单调队列做,把同一行的连续N个点缩成一个点保存最大最小值预处 ...
- 【hihoCoder第十七周】最近公共祖先·三
之前就写的是离线算法.思路就是先序一遍树,记录层数,然后高效RMQ就好.ST和线段树都能过. 以后有时间将之前的在线算法补上. #include <bits/stdc++.h> using ...
- 蓝桥杯 BASIC 24 龟兔赛跑预測(模拟)
[思路]:模拟.注意一个是在兔子歇息的时间乌龟可能到达了.刚開始没考虑WA80%. [AC代码]: #include <iostream> #include <algorithm&g ...
- leetcode:Minimum Path Sum(路线上元素和的最小值)【面试算法题】
题目: Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right w ...