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. sgu 101 domino

    题意还算简洁明了,加上有道翻译凑过着读完了题.题意大体上是 给你 n 个多米诺骨牌, 给出每个骨牌两端的数字, 只有数字相同才可以推到, 比如 2-3和3-2.你可以旋转这些多米诺骨牌, 输出一个可以 ...

  2. Linux C 程序 基础(FOUR)

    1.标识符:C语言本身不限制变量长度,但是某些编译器会限制变量长度,命名最好不要超过8位.         以数字开头,保留字,*,空格非法 2.关键字:类型说明符,int , 语句定义符,if el ...

  3. centos 关闭触摸板,触摸板点击

    yum install xorg-x11-apps xinput –list 由图可以看出俺的本本触摸板ID为14,于是可以通过以下命令开启与关闭它: 禁止touchpad:xinput set-in ...

  4. SET QUOTED_IDENTIFIER ON

    语法  SET QUOTED_IDENTIFIER { ON | OFF }    注释  当 SET QUOTED_IDENTIFIER 为 ON 时,标识符可以由双引号分隔,而文字必须由单引号分隔 ...

  5. Linux下实现流水灯等功能的LED驱动代码及测试实例

    驱动代码: #include <linux/errno.h> #include <linux/kernel.h> #include <linux/module.h> ...

  6. Python守护进程(多线程开发)

    本段代码主要作用是httpsqs队列的消费端守护进程,从httpsqs中取出数据,放入mongodb #!/usr/bin/python import sys,time,json,logging im ...

  7. leetcode解题—Longest Palindromic Substring

    题目: Given a string S, find the longest palindromic substring in S. You may assume that the maximum l ...

  8. We7——很有意思的一个开源CMS

    目前做门户.做网站,基本上都需要用到一个系统,那就是CMS内容管理系统:现在开源产品有很多,笔者也是从事这个行业的,国内的各大CMS提供商基本上都试用过,今天向大家推荐一款很有意思的产品——We7CM ...

  9. keil 51警告编译优化

    KeilC51中将工程中没有调用的函数不进行编译的方法 把Target Options中的Device页中选上"Use Extended Linker(LX51)instead of BL5 ...

  10. 告诉你KVC的一切-b

    KVC(Key-value coding)键值编码,单看这个名字可能不太好理解.其实翻译一下就很简单了,就是指iOS的开发中,可以允许开发者通过Key名直接访问对象的属性,或者给对象的属性赋值.而不需 ...