Frequent values

TimeLimit:3000Ms

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,对于一系列询问(i,j),输出Ai,A(i+1),……,Aj中出现次数最多的值出现的次数。

分析:由于整个数组是非降序的,全部相等元素会聚集在一起,这样就能够把这个数组进行游标编码。比方-1,1,1,2,2,2,4就能够编码成(-1,1),(1,2),(2,3),(4,1),当中(a,b)表示有b个连续的a。

用value[i]和count[i]分别表示第i段的数值和出现次数,num[p]、left[p]、right[p]分别表示位置p所在段的编号和左右端点位置,则每次查询时的结果为下面三部分的最大值:从L到L所在段的结束处的元素个数(即right[L]-L+1)、从R所在段的開始处到R处的元素个数(即R-left[R]+1)、中间第num[L]+1段到第num[R]-1段的count的最大值。这样问题就差点儿转化为了RMQ问题。

#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
#include<iostream>
using namespace std;
const int N = 1e5 + 10;
int n, tot, Q;
int dp[N][20];
int num[N], cnt[N], Left[N], Right[N];
void RMQ_Init()
{
memset(dp, 0, sizeof(dp));
for(int i = 1; i <= tot; i++)
dp[i][0] = cnt[i];
for(int j = 1; (1<<j) <= n; j++)
for(int i = 1; i + (1<<j) - 1 <= tot; i++)
dp[i][j] = max(dp[i][j-1], dp[i+(1<<(j-1))][j-1]);
}
int RMQ(int L, int R)
{
if(L > R)
return 0;
int k = 0;
while((1<<(k+1)) <= R - L + 1) k++;
return max(dp[L][k], dp[R-(1<<k)+1][k]);
}
int main()
{
int v, last_v, i;
while(~scanf("%d",&n))
{
if(n == 0) break;
scanf("%d",&Q);
tot = 0;
memset(Left, 0, sizeof(Left));
memset(Right, 0, sizeof(Right));
memset(cnt, 0, sizeof(cnt));
for(i = 1; i <= n; i++)
{
scanf("%d",&v);
if(i == 1)
{
++tot;
last_v = v;
Left[tot] = 1;
}
if(last_v == v)
{
num[i] = tot;
cnt[tot]++;
Right[tot]++;
}
else
{
num[i] = ++tot;
cnt[tot]++;
Left[tot] = Right[tot] = i;
last_v = v;
}
}
RMQ_Init();
int L, R;
for(int i = 0; i < Q; i++)
{
scanf("%d%d",&L,&R);
if(num[L] == num[R])
printf("%d\n", R - L + 1);
else
{
int tmp1 = Right[num[L]] - L + 1;
int tmp2 = R - Left[num[R]] + 1;
int tmp3 = RMQ(num[L] + 1, num[R] - 1);
printf("%d\n",max(tmp1, max(tmp2, tmp3)));
}
}
}
return 0;
}

版权声明:本文博主原创文章,博客,未经同意不得转载。

UVA 11235 Frequent values(RMQ)的更多相关文章

  1. UVA-11235 Frequent values (RMQ)

    题目大意:在一个长度为n的不降序列中,有m次询问,每次询问(i,j)表示在区间(i,j)中找出出现次数最多的元素的出现次数. 题目分析:因为序列有序,可以将序列分段,并且记录每段的元素个数.每一个元素 ...

  2. 【POJ 3368】Frequent values(RMQ)

    Description You are given a sequence of n integers a1 , a2 , ... , an in non-decreasing order. In ad ...

  3. poj 3368 Frequent values(RMQ)

    题目:http://poj.org/problem?id=3368 题意:给定n个数,顺序为非下降,询问某个区间内的数出现最多的数的 出现次数.. 大白书上的 例题..算是RMQ变形了, 对 原数组重 ...

  4. RMQ算法 以及UVA 11235 Frequent Values(RMQ)

    RMQ算法 简单来说,RMQ算法是给定一组数据,求取区间[l,r]内的最大或最小值. 例如一组任意数据 5 6 8 1 3 11 45 78 59 66 4,求取区间(1,8)  内的最大值.数据量小 ...

  5. UVA 11235 Frequent values 线段树/RMQ

    vjudge 上题目链接:UVA 11235 *******************************************************大白书上解释**************** ...

  6. UVA - 11235 Frequent values

    2007/2008 ACM International Collegiate Programming Contest University of Ulm Local Contest Problem F ...

  7. [POJ] 3368 / [UVA] 11235 - Frequent values [ST算法]

    2007/2008 ACM International Collegiate Programming Contest University of Ulm Local Contest Problem F ...

  8. poj 1806 Frequent values(RMQ 统计次数) 详细讲解

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1806 题目大意:给你一个非降序排列的整数数组,你的任务是对于一系列的询问,(i,j),回答序列中出现次 ...

  9. UVa 11235 Frequent values (RMQ && 区间出现最多次的数的次数)

    题意 : 给出一个长度为 n 的不降序序列,并且给出 q 个形如(L, R)的问询,问你这个区间出现的最多次的数的次数. 分析 : 很自然的想到将区间“缩小”,例如1 1 2 3 3 3就可以变成2 ...

随机推荐

  1. 我们究竟什么时候可以使用Ehcache缓存(转)

    一.Ehcache是什么 EhCache是Hibernate的二级缓存技术之一,可以把查询出来的数据存储在内存或者磁盘,节省下次同样查询语句再次查询数据库,大幅减轻数据库压力. 二.Ehcache的使 ...

  2. Oracle左连接、右连接示例

    建表: create table a ( id ), name ) ); create table b ( name ), age ) ); create table c ( name ), dept ...

  3. php 跳转控制

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. WordPress数据备份

    服务器钱用光了要关了或者是服务器想要搬家,需要备份各种数据. 今天简单的备份了一下在服务器上面wordpress各种文件和资源. wordpress的数据主要分两个部分,一个是文字部分的:一个是附件部 ...

  5. hdu 4090 GemAnd Prince

    题目大意: 别人说是消消看,至于你玩没玩过.反正我是没玩过的. 就是选择一个钻石,可以消除与它相连的所有钻石.并获得 消除数量*消除数量  的分 思路: 直接暴搜,然后用一个cnt数组表示每一种钻石剩 ...

  6. ios点击产生波纹效果

    ios点击产生波纹效果 by 伍雪颖 - (void)viewDidLoad { [super viewDidLoad]; RippleView = [[UIView alloc] initWithF ...

  7. NET Core RC2 and .NET Core SDK Preview

    NET Core RC2 and .NET Core SDK Preview 先看一下 .NET Core(包含 ASP.NET Core)的路线图: Beta6: 2015年7月27日 Beta7: ...

  8. ecshop 微信支付插件

    眼下已完毕支付測试,可以支付成功,支付逻辑自己实现.后台通知.发货通知.订单查询未測. 当中用到了redis 下载

  9. Spring Security Source Code -- 验证标准流程

    除了初始阶段: 主干验证流程链: MyInvocationSecurityMetadataSource.getAttributes(Object) line: 43     MyFilterSecur ...

  10. SLIC superpixel实现分析

    http://infoscience.epfl.ch/record/149300这是SLIC算法的官网,网站有和SLIC相关的资源. SLIC主要运用K-means聚类算法进行超像素的处理,聚类算法中 ...