Frequent values

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1146    Accepted Submission(s): 415

Problem Description
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
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
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
 
Hint

A naive algorithm may not run in time!

 
分成三段、中间RMQ、然后求最大值即可
#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
#define N 100010 int n,m;
int a[N];
int dp[N][];
int id[N];
int len[N],l[N],r[N]; void init()
{
int i,j;
for(i=;i<=n;i++)
{
dp[i][]=len[i];
}
int k=(int)(log((double)n)/log(2.0));
for(j=;j<=k;j++)
{
for(i=;i+(<<j)-<=n;i++)
{
dp[i][j]=max(dp[i][j-],dp[i+(<<(j-))][j-]);
}
}
}
int query(int i,int j)
{
int k=(int)(log((double)(j-i+))/log(2.0));
int res=max(dp[i][k],dp[j-(<<k)+][k]);
return res;
}
int main()
{
int i,pos;
while(scanf("%d",&n),n)
{
pos=;
scanf("%d",&m);
for(i=;i<=n;i++)
{
scanf("%d",&a[i]);
if(a[i]!=a[i-])
{
pos++;
l[pos]=i;
}
id[i]=pos;
r[pos]=i;
len[pos]=r[pos]-l[pos]+;
}
n=pos;
init();
int x,y,xx,yy,ans1,ans2,ans3;
while(m--)
{
int x,y;
scanf("%d%d",&x,&y);
xx=id[x];
yy=id[y];
if(xx==yy) ans1=ans2=y-x+; //特殊情况、当x和y在同一个区间、答案是y-x+1
else
{
ans1=r[xx]-x+;
ans2=y-l[yy]+;
}
ans3=;
if(xx+<=yy-)ans3=query(xx+,yy-);
printf("%d\n",max(max(ans1,ans2),ans3));
}
}
return ;
}

[HDU 1806] Frequent values的更多相关文章

  1. hdu 1806 Frequent values 线段树

    题目链接 给一个非递减数列, n个数, m个询问, 每个询问给出区间[L, R], 求这个区间里面出现次数最多的数的次数. 非递减数列, 这是最关键的一个条件... 需要保存一个区间最左边的数, 最右 ...

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

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

  3. UVA - 11235 Frequent values

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

  4. poj 3368 Frequent values(RMQ)

    /************************************************************ 题目: Frequent values(poj 3368) 链接: http ...

  5. H - Frequent values

    Problem F: Frequent values You are given a sequence of n integers a1 , a2 , ... , an in non-decreasi ...

  6. Frequent values && Ping pong

    Frequent values 题意是不同颜色区间首尾相接,询问一个区间内同色区间的最长长度. 网上流行的做法,包括翻出来之前POJ的代码也是RMQ做法,对于序列上的每个数,记录该数向左和向右延续的最 ...

  7. 【暑假】[实用数据结构]UVa11235 Frequent values

    UVa 11235 Frequent values Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 11241   Accep ...

  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. [POJ] 3368 / [UVA] 11235 - Frequent values [ST算法]

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

随机推荐

  1. 关于C#虚函数和构造函数的一点理解

    虚函数感觉总是很神秘,在本质的原理上一直也没有弄得很透彻,今天又有一点的新的感悟,纪录下来,有时间的话可以去研究一下C++对象模型 using System; using System.Collect ...

  2. nodejs的调试

    js的调试始终是一个比较麻烦也是比较困难的事情,从最原始的alert调试,到火狐的firebug工具,在到后来各个浏览器厂商的调试工具.调试工具的发展历程,也可以看出由JS构建的业务和技术逻辑越来越复 ...

  3. Chrome控制台函数

    你是光你是电,你是唯一的神话. Chrome的出现简直拯救了广大前端,不仅仅是因为其V8引擎的速度,更是因为其强大的控制台.为调试前端提供了强大的武器. 当然Firefox下的firebug也很强大, ...

  4. top每个参数的意义

    上图来源于本人虚拟机,作为每个参数解释的参考 10:32:20:现在的时间 up 1 min : 服务器运行的时长 1 user:现在只有1个用户登录 load average: 0.74, 0.25 ...

  5. php用正则表达式获取网站的标题内容

    已知网站的网址,用php获取网站的内容. 编写正则表达式. 用preg_match_all函数获取标题内容. $url='http://www.m-ivi.com'; $content=file_ge ...

  6. php四种基础排序算法的运行时间比较

    /** * php四种基础排序算法的运行时间比较 * @authors Jesse (jesse152@163.com) * @date 2016-08-11 07:12:14 */ //冒泡排序法 ...

  7. jQuery实现鼠标移到元素上动态提示消息框效果

    当光标移动到某些元素上时,会弹出像tips的提示框,这种效果想必大家都有见到过吧,下面有个不错的示例,大家可以感受下 当光标移动到某些元素上时,会弹出像tips的提示框. 复制代码代码如下: < ...

  8. python 魔法方法

    I am not a creator, I just a porter. Note: Everything is object in python. 对于Python来说一切都是对象,也就是函数的参数 ...

  9. 六,WPF的Application类

    Application.ShutdownMode属性:通过,只要有一个窗口还没有关闭,Application类就保持应用程序处于有效状态,如果这不是所期望的行为,就可以调整该属性. 应用程序事件 Ap ...

  10. EasyUI portal自定义小图标,不是用js方式加载

    <script src="~/Scripts/jquery.portal.js"></script> <script> $(function ( ...