UVA 11235 Frequent Values ---RMQ
大白书上的例题,具体讲解见大白书,最好用用一个Log数组直接求k,这样就是纯O(1)了
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
#define N 100003 int a[N],num[N],le[N],ri[N],cnt[N];
int d[N][],n,type; void RMQ_init()
{
int i,j;
for(i=;i<=type;i++)
d[i][] = cnt[i];
for(j=;(<<j)<=type;j++)
{
for(i=;i+(<<j)-<=type;i++)
d[i][j] = max(d[i][j-],d[i+(<<(j-))][j-]);
}
} int LOG[];
void getLog(int n)
{
for(int i=;i<=n;i++)
LOG[i] = (int)(log((double)i)/log(2.0));
} int RMQ(int l,int r)
{
int k = LOG[R-L+];
return max(d[l][k],d[r-(<<k)+][k]);
} int main()
{
int q,i,j,pos;
int l,r;
while(scanf("%d",&n)!=EOF && n)
{
scanf("%d",&q);
for(i=;i<=n;i++)
scanf("%d",&a[i]);
a[] = -;
type = ;
for(pos=;pos<=n;pos++)
{
if(a[pos] != a[pos-])
{
if(pos != )
cnt[type] = pos-le[pos-];
num[pos] = ++type;
for(j=le[pos-];j<=pos-;j++)
ri[j] = pos-;
le[pos] = pos;
if(pos == n)
cnt[type] = ,ri[pos] = pos;
}
else
{
le[pos] = le[pos-];
num[pos] = num[pos-];
if(pos == n)
{
cnt[type] = pos - le[pos] + ;
for(j=le[pos];j<=n;j++)
ri[j] = n;
}
}
}
RMQ_init();
while(q--)
{
scanf("%d%d",&l,&r);
if(num[l] == num[r])
{
printf("%d\n",r-l+);
continue;
}
int lmax = ri[l]-l+;
int rmax = r-le[r]+;
int mmax;
if(num[l]+ > num[r]-)
mmax = ;
else
mmax = RMQ(num[l]+,num[r]-);
printf("%d\n",max(mmax,max(lmax,rmax)));
}
}
return ;
}
UVA 11235 Frequent Values ---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 11235 Frequent values (RMQ && 区间出现最多次的数的次数)
题意 : 给出一个长度为 n 的不降序序列,并且给出 q 个形如(L, R)的问询,问你这个区间出现的最多次的数的次数. 分析 : 很自然的想到将区间“缩小”,例如1 1 2 3 3 3就可以变成2 ...
- 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
2007/2008 ACM International Collegiate Programming Contest University of Ulm Local Contest Problem F ...
- [POJ] 3368 / [UVA] 11235 - Frequent values [ST算法]
2007/2008 ACM International Collegiate Programming Contest University of Ulm Local Contest Problem F ...
- POJ 3368 & UVA 11235 - Frequent values
题目链接:http://poj.org/problem?id=3368 RMQ应用题. 解题思路参考:http://blog.csdn.net/libin56842/article/details/4 ...
- 数据结构(RMQ):UVAoj 11235 Frequent values
Frequent values You are given a sequence of n integers a1 , a2 , ... , an in non-decreasing order. I ...
- poj 3368 Frequent values(RMQ)
/************************************************************ 题目: Frequent values(poj 3368) 链接: http ...
随机推荐
- json在php中的使用之如何转换json为数组
<?php $json = '{"a":1,"b":2,"c":3,"d":4,"e":5}' ...
- 细说Mysql四种安装方法及自动化部署
一.简介 数据库(Database)是按照数据结构来组织.存储和管理数据的仓库, 每个数据库都有一个或多个不同的API用于创建,访问,管理,搜索和复制所保存的数据. 我们也可以将数据存储在文件中,但是 ...
- springmvc+mybatis+spring 整合
获取[下载地址] [免费支持更新]三大数据库 mysql oracle sqlsever 更专业.更强悍.适合不同用户群体[新录针对本系统的视频教程,手把手教开发一个模块,快速掌握本系统] ...
- hdu 2952 Counting Sheep
本题来自:http://acm.hdu.edu.cn/showproblem.php?pid=2952 题意:上下左右4个方向为一群.搜索有几群羊 #include <stdio.h> # ...
- Java 上传文件到 SFTP 抛异常 java.lang.NoClassDefFoundError: Could not initialize class sun.security.ec.SunEC 的解决办法
最近从 Op 那里报来一个问题,说是SFTP上传文件不成功.拿到的 Exception 如下: Caused by: java.lang.NoClassDefFoundError: Could not ...
- [TypeScript] 建置输出单一JavaScript档案(.js)与Declaration档案(.d.ts)
[TypeScript] 建置输出单一JavaScript档案(.js)与Declaration档案(.d.ts) 问题情景 开发人员使用Visual Studio来开发TypeScript,可以很方 ...
- JS中检测数据类型的几种方式及优缺点
1.typeof 用来检测数据类型的运算符 typeof value 返回值首先是一个字符串,其次里面包含了对应的数据类型,例如:"number"."string&quo ...
- 函数改变全局变量-JS
切记,一定按三步走: 1. 全局变量声明 2. 函数声明 3. 函数调用 正确做法: var dataStr = null; function remoteCallback(data) { dataS ...
- 【DNN】C#中类的一些特性标注
[AttributeUsage] 其实AttributeUsage类就是描述了一个定制特性如和被使用. C# AttributeUsage的使用要明白: AttributeUsage有三个属性,我们可 ...
- SharePoint 2013: Search Architecture in SPC202
http://social.technet.microsoft.com/wiki/contents/articles/15989.sharepoint-2013-search-architecture ...