Frequent values
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 14764   Accepted: 5361

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

这题自己没有做出来,一直TLE,像当年做扩展欧几里德题目似的。看网上代码,才知道RMQ这种算法,总之是要记住了这种有动态规划思想的东西,还有这种询问这么多次的题目要离线处理啊啊啊啊,这都不懂你还搞毛啊光速小子。。。

我自己都不好意思舔这个大脸上这个代码,为了以后自己常常看看,记住它吧。

觉得这个代码写得真的好,很多细节做的真的不错(。。。。。。):

#include <iostream>
#include <cmath>
#include <algorithm>
#pragma warning(disable:4996)
using namespace std; int num[100005];
int fre[100005];
int n,q; int max_v[100005][20]; void RMQ()
{
int i,j;
for(i=1;i<=n;i++)
{
max_v[i][0]=fre[i];
}
int temp = log((double)n)/log(2.0)+1;
for(j=1;j<=temp;j++)
{
for(i=1;i+(1<<j)-1<=n;i++)
{
max_v[i][j]=max(max_v[i][j-1],max_v[i+(1<<(j-1))][j-1]);
}
}
} int cal(int h,int k)
{
if(h>k)
return 0;
int temp=k-h+1;
int temp2=log((double)temp)/log(2.0); return max(max_v[h][temp2],max_v[k-(1<<temp2)+1][temp2]);
} int main()
{
//freopen("input.txt","r",stdin);
//freopen("out.txt","w",stdout); while(scanf("%d",&n))
{
int i,h,k;
if(n==0)
break;
scanf("%d",&q); memset(fre,0,sizeof(fre));
memset(max_v,0,sizeof(max_v));
for(i=1;i<=n;i++)
{
scanf("%d",&num[i]);
if(i==1)
{
fre[1]=1;
}
else
{
if(num[i]==num[i-1])
fre[i]=fre[i-1]+1;
else
fre[i]=1;
}
} RMQ(); for(i=1;i<=q;i++)
{
scanf("%d%d",&h,&k);
int temp;
int t=0; while((h+t<=k)&&(num[h+t]==num[h]))
{
t++;
}
temp=h+t;
int temp2=cal(temp,k);
cout<<max(t,temp2)<<endl;
}
}
return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

POJ 3368:Frequent values的更多相关文章

  1. POJ 3368:Frequent values(线段树区间合并)

    题目大意,给出一段非降序列,求一些区间中出现频率最高的数的出现次数. 分析: 显然,区间中一个数多次出现必然是连续的,也就是最长的连续相等的一段. 用线段树解决,维护三个信息:一个区间最长连续的区间的 ...

  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 2785:4 Values whose Sum is 0 二分

    4 Values whose Sum is 0 Time Limit: 15000MS   Memory Limit: 228000K Total Submissions: 18221   Accep ...

  4. UVA - 11235:Frequent values

    非常优美的RMQ问题,可以运到桶的思想 #include<cstdio> #include<cstdlib> #include<algorithm> #includ ...

  5. poj 3368 Frequent values(RMQ)

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

  6. POJ 3368 Frequent values (基础RMQ)

    Frequent values Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 14742   Accepted: 5354 ...

  7. POJ 3368 Frequent values 【ST表RMQ 维护区间频率最大值】

    传送门:http://poj.org/problem?id=3368 Frequent values Time Limit: 2000MS   Memory Limit: 65536K Total S ...

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

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

  9. poj 3368 Frequent values(段树)

    Frequent values Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 13516   Accepted: 4971 ...

随机推荐

  1. LoRaWAN协议(一)------架构解析

    摘自:http://www.cnblogs.com/answerinthewind/p/6200497.html LoRaWAN协议(一)-----架构解析 (1)LoRaWAN分层 LoRaWAN总 ...

  2. windows环境变量引发的血案

    最近重装了系统,决心使用Anaconda来管理python包和虚拟环境.在完成一系列配置后,运行程序,发现老是报错 D:\Anaconda3\envs\jobnote>python E:\wor ...

  3. [排错] VO对象和POJO对象的关系

    这或许是一个很蠢的笔记吧...... 这次项目中, 作为一个新人, 没少被这两个概念虐得死去活来的, 现在特别做一次记录, 关于它们二者之间在项目中的应用. 在这里呢, 就不再赘述 VO(view o ...

  4. c/c++ struct的大小以及sizeof用法

    直入主题,怎么判断内存对齐规则,sizeof的结果怎么来的,请牢记以下3条原则:(在没有#pragma pack宏的情况下) 1:数据成员对齐规则:结构(struct)(或联合(union))的数据成 ...

  5. 【STM32H7教程】第53章 STM32H7的LTDC应用之汉字小字库和全字库制作

    完整教程下载地址:http://www.armbbs.cn/forum.php?mod=viewthread&tid=86980 第53章       STM32H7的LTDC应用之汉字小字库 ...

  6. 「NOIP2016」天天爱跑步

    传送门 Luogu 解题思路 树上差分+桶计数. 我们发现在一条路径上的点 \(i\) ,它可以观测到玩家的条件是: \(i \in (u \to LCA),dep_u=w_i+dep_i\) \(i ...

  7. Python—网络通信编程之tcp通信编程

    服务端代码 import socket # 1.创建流式套接字实例 # server = socket.socket() server = socket.socket(socket.AF_INET, ...

  8. 使用 XMLBeans 进行编程

      深入考察 XMLBeans 的特性和功能.本文通过一个简单的例子介绍 XMLBeans 技术,然后一步一步地介绍编译和绑定的过程,讨论 XML 游标.标记和 XQuery 表达式等高级特性.本文还 ...

  9. MyEclipse8.5 + jdk 1.6.0_43 + tomcat6 + maven 3.0.4 + win7 32位 ,环境搭建并看到“hello world”

    一.前期准备 1.首先安装jdk,具体略,用java -version在cmd中看到下图所示就说明安装好了 2.安装tomcat6,具体略,在浏览器中输入http://localhost:8080/, ...

  10. 从ofo到乐视,变卖资产好过冬靠谱吗?

    今年年底,有很多人"被迫"离职.他们为了应对生活压力和找工作的不确定性,尝试在二手平台上卖出自己的奢侈品或心爱之物,以期度过潜在的难关.而对于很多企业来说,这个冬天也非常冷.依靠常 ...