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. [PR & ML 2] [Introduction] Example: Polynomial Curve Fitting

    啊啊啊,竟然不支持latex,竟然HTML代码不能包含javascript,代码编辑器也不支持Matlab!!!我要吐槽博客的编辑器...T_T只能贴图凑合看了,代码不是图,但这次为了省脑细胞,写的不 ...

  2. 简单重置Centos服务器中Mysql的root密码

    1.编辑MySQL配置文件my.cnf vi /etc/my.cnf #编辑文件,找到[mysqld],在下面添加一行skip-grant-tables [mysqld] skip-grant-tab ...

  3. python(二)基础

    1.lambda表达式(一般格式为冒号前边是参数冒号后边是返回值) 单参数: 多参数: 我们如果实现一个普通的filter过滤函数过滤掉偶数: 而我们如果用lambda表达式就很简便: 2.递归 普通 ...

  4. c/c++中的各种字符串转换

    一:CString 和 *char 的转换: 1:CString -> *char 1)CString转化为*char可以使用CString中的GetBuffer()函数,具体如下: CStri ...

  5. emctl start dbconsole OC4J_dbconsole*** not found

    C:\windows\system32>emctl start dbconsole OC4J Configuration issue. D:\app\product\\db_1/oc4j/j2e ...

  6. Django视图函数

    一.视图函数 1. 视图函数的第一个参数一定是一个HTTPRequest类型的对象,这个对象是Django自动创建的,具体形参名通常用request.通过这个对象,可以调用请求的一些参数,比如requ ...

  7. CFNetwork学习总结

  8. sjtu1333 函数时代

    Description Taring说:生活的过程就是执行函数的过程.需要命令,也需要回报.更重要的,是得到回报的过程. 好吧...这道题其实和上面的也没啥关系TAT,我们要求的是下面的问题: 给定\ ...

  9. Fiddler对安卓应用手机抓包图文教程

    http://www.cr173.com/html/37625_1.html 做开发需要抓取手机app的http/https的数据包,想看APP发出的http请求和响应是什么,这就需要抓包了,这可以得 ...

  10. sql replace into 与 insert into

    sql replace into用法详细说明 REPLACE的运行与INSERT很相似.只有一点例外,假如表中的一个旧记录与一个用于PRIMARY KEY或一个UNIQUE索引的新记录具有相同的值,则 ...