[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 ...
随机推荐
- Gradle Android客户端程序打包(基于gradle 1.12版本验证通过)
一.前言 android客户端开发进入尾声,负责SEO同事突然发给我一个涉及45个发布渠道的噩耗,之前只发布自有渠道的工作方式(手动修改参数打包)已经不满足需求,所以引入最近比较流行的gradle打包 ...
- linux文件合并
第一:两个文件的交集,并集前提条件:每个文件中不得有重复行1. 取出两个文件的并集(重复的行只保留一份)2. 取出两个文件的交集(只留下同时存在于两个文件中的文件)3. 删除交集,留下其他的行1. c ...
- VC6.0 编译 gdlib 库
环境 WinXP, MSVC6.0 1 从 https://bitbucket.org/libgd/gd-libgd/downloads 下载最新版本 libgd 2 可以用 nmake 编译 w ...
- 负重前行的婚纱线上路 - i天下网商-最具深度的电商知识媒体
负重前行的婚纱线上路 - i天下网商-最具深度的电商知识媒体 负重前行的婚纱线上路
- zoj 3811 Untrusted Patrol(bfs或dfs)
Untrusted Patrol Time Limit: 3 Seconds Memory Limit: 65536 KB Edward is a rich man. He owns a l ...
- AllocConsole
#include<iostream> using namespace std; AllocConsole(); freopen("CONIN$", "r+t& ...
- WebService- 使用 CXF 开发 SOAP 服务
选框架犹如选媳妇,选来选去,最后我还是选了“丑媳妇(CXF)”,为什么是它?因为 CXF 是 Apache 旗下的一款非常优秀的 WS 开源框架,具备轻量级的特性,而且能无缝整合到 Spring 中. ...
- (转)Iphone数组一些基础操作 NSArray/NSMutableArray
/******************************************************************************************* NSArray ...
- Win32多线程编程(2) — 线程控制
Win32线程控制只有是围绕线程这一内核对象的创建.挂起.恢复.终结以及通信等操作,这些操作都依赖于Win32操作系统提供的一组API和具体编译器的C运行时库函数.本篇围绕这些操作接口介绍在Windo ...
- ARM指令集——数据处理指令
ARM汇编指令集 ARM汇编文件的组成 指令:编译完成后作为一条指令(机器码)存储在内存单元中,CPU执行时能够完成处理的操作 伪指令:在编译时替换成能被识别的ARM指令 伪操作:知道编译器进行编译, ...
[2] ans=R-L+1
[3] ans=max(max(Right[i]-L+1,R-Left[j]+1),RMQ(k))
代码: