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

A naive algorithm may not run in time!

 

 #include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm> using namespace std; const int maxn=;
int cnt=;
struct node
{
int L,R,nb,v;
}N[maxn]; int dp[maxn+][];
int a[maxn+],kt[maxn+],n,m; int RMQ_init()
{
for(int i=;i<=cnt;i++) dp[i][]=N[i].nb;
for(int j=;(<<j)<=cnt;j++)
{
for(int i=;i+(<<j)-<=cnt;i++)
{
dp[i][j]=max(dp[i][j-],dp[i+(<<(j-))][j-]);
}
}
} int RMQ(int L,int R)
{
int k=;
while(R-L+>=(<<k)) k++; k--;
return max(dp[L][k],dp[R-(<<k)+][k]);
} int main()
{
while(scanf("%d",&n)&&n)
{
scanf("%d",&m);
///yu chu li
memset(N,,sizeof(N));
scanf("%d",a+);
cnt=;N[cnt].v=a[];N[cnt].L=;N[cnt].nb=;
for(int i=;i<=n;i++)
{
scanf("%d",a+i);
if(a[i]==N[cnt].v)
{
N[cnt].nb++;
}
else
{
cnt++;
N[cnt-].R=i-;
N[cnt].L=i;N[cnt].v=a[i];N[cnt].nb=;
}
if(i==n) N[cnt].R=n;
}
int pos=;
kt[]=-;
for(int i=;i<=cnt;i++)
{
int temp=N[i].nb;
while(temp--)
{
kt[pos]=i;
pos++;
}
}
RMQ_init();
while(m--)
{
int a,b,na=-,nb=-;
scanf("%d%d",&a,&b);
na=kt[a],nb=kt[b];
if(na==nb)
{
printf("%d\n",b-a+);
}
else if(na+==nb)
{
printf("%d\n",max(N[na].R-a+,b-N[nb].L+));
}
else
{
int ans1=N[na].R-a+,ans2=b-N[nb].L+,ans3=;
int L=na+,R=nb-;
ans3=RMQ(L,R);
printf("%d\n",max(ans1,max(ans2,ans3)));
}
}
}
return ;
}

UVA - 11235 Frequent values的更多相关文章

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

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

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

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

  3. UVA 11235 Frequent values(RMQ)

    Frequent values TimeLimit:3000Ms , ... , an in non-decreasing order. In addition to that, you are gi ...

  4. UVA 11235 Frequent values 线段树/RMQ

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

  5. UVA 11235 Frequent Values ---RMQ

    大白书上的例题,具体讲解见大白书,最好用用一个Log数组直接求k,这样就是纯O(1)了 #include <iostream> #include <cstdio> #inclu ...

  6. POJ 3368 & UVA 11235 - Frequent values

    题目链接:http://poj.org/problem?id=3368 RMQ应用题. 解题思路参考:http://blog.csdn.net/libin56842/article/details/4 ...

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

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

  8. 数据结构(RMQ):UVAoj 11235 Frequent values

    Frequent values You are given a sequence of n integers a1 , a2 , ... , an in non-decreasing order. I ...

  9. 11235 - Frequent values

    <算法竞赛入门经典-训练指南>P198 记录一下区间的左右边界就可以了 #include <iostream> #include <stack> #include ...

随机推荐

  1. Django 前后台的数据传递

    Django 从后台往前台传递数据时有多种方法可以实现. 最简单的后台是这样的: from django.shortcuts import render def main_page(request): ...

  2. (转)浅谈Java中的equals和==

    原文地址: http://www.cnblogs.com/dolphin0520/p/3592500.html 在初学Java时,可能会经常碰到下面的代码: 1 String str1 = new S ...

  3. 中大东校小米路由器mini实现inode上网,ipv6 wifi【中大】【东校】【inode】【ipv6】

    还有不到4个月就要毕业了,前几天半夜没事捣鼓小米路由没想到竟然实现了wifi的ipv6. 正好又安利了同学一台小米路由mini,从刷机到inode到ipv6全搞了一遍. 这里将教程写出来,服务学弟妹. ...

  4. 第9章 Shell基础(4)_Bash的运算符及环境变量配置文件

    5. Bash的运算符 5.1 数值运算与运算符 5.1.1 declare 声明变量类型:#declare [+/-] [选项] 变量名 选项 说明 - 给变量设定类型属性 + 取消变量的类型属性 ...

  5. JDBC中的Statement和PreparedStatement的区别

    JDBC中的Statement和PreparedStatement的区别  

  6. [LeetCode] Add Binary 二进制数相加

    Given two binary strings, return their sum (also a binary string). For example,a = "11"b = ...

  7. ASP.NET提取多层嵌套json数据的方法

    本文实例讲述了ASP.NET利用第三方类库Newtonsoft.Json提取多层嵌套json数据的方法,具体例子如下. 假设需要提取的json字符串如下: {"name":&quo ...

  8. Azure AD Connect 手动同步

    我们目前采用工具Azure AD Connect 目录同步工具将本地域控制器的用户信息同步至office365和Azure 在之前目录同步工具中使用Windows 任务计划程序或单独的 Windows ...

  9. cocos2dx-lua_修改源码流程(cocos2dx-3.10、win7、Cocos Code IDE1.2)

    以下是在cocos2dx-3.10.win7.Cocos Code IDE1.2下假定你已经配置好了cocos2dx的环境.1.修改源代码步骤(1)在Cocos/Cocos2d-x/cocos2d-x ...

  10. jquery中ajax用return来返回值无效

    jquery中,ajax返回值,有三种写法,只有其中一种是成功的 /** * async:false,同步调用 * 返回1:2 * 失败 * 分析:ajax内部是一个或多个定义的函数,ajax中ret ...