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. H5禁止底部横向滚动条,使一个元素居中

    1.禁止底部横向滚动条 选择元素设置样式 { overflow-y:auto; overflow-x:hidden } 2.元素居中 { margin-left:auto ; margin-right ...

  2. Core Data 基本数据操作 增删改查 排序

    所有操作都基于Core Data框架相关 API,工程需要添加CoreData.framework支持 1.增  NSEntityDescription insertNewObjectForEntit ...

  3. 软件环境常识 --dev sit uat

    DEV环境:DEV顾名思义就是develop,即代码开发的环境. SIT环境:System Integration Test系统集成测试,开发人员自己测试流程是否走通. UAT环境:User Acce ...

  4. MySQL序列解决方案

    MySQL序列解决方案 MySQLOracleSQL  MySQL自增长与Oracle序列的区别: 自增长只能用于表中的其中一个字段 自增长只能被分配给固定表的固定的某一字段,不能被多个表共用. 自增 ...

  5. day07-Python运维开发基础(深/浅拷贝、字典/集合/相关操作)

    1. 深拷贝与浅拷贝 # ### 深拷贝 和 浅拷贝 """ a = 7 b = a a = 8 print(b) lst1 = [1,2,3] lst2 = lst1 ...

  6. zigbee CC2530首选方案模组:TZU06A1

    模块特点 微型24-pin 邮票式SMT 封装 提供U.FL 接口,用于外接SMA 天线 小尺寸封装:16mm*20mm*3.7mm 通过欧盟CE0168.欧盟ROHS 认证 基于8051 单片机架构 ...

  7. Redis Cluster 强制kill某一个节点和shutdown某一个节点后修复过程

    redis cluster 命令行,执行以下命令需登录cluster,是集群所独有的集群(cluster)CLUSTER INFO 打印集群的信息CLUSTER NODES 列出集群当前已知的所有节点 ...

  8. H7-TOOL脱机烧录器功能开源发布

    H7-TOOL汇总帖:https://www.cnblogs.com/armfly/p/12283459.html 当前已经对STM32F030,STM32F103,STM32F429,STM32F7 ...

  9. 深度学习之常用linux命令总结

    深度学习中常用linux命令总结 1.创建文件夹 mkdir 文件名2.删除文件 rm -d 目录名 #删除一个空目录 rmdir 目录名 #删除一个空目录 rm -r 目录名 #删除一个非空目录 r ...

  10. Linux操作系统之Shell程序设计

    Shell是种命令解释程序,也可以看作是一种命令语言的解释器. 用户在与Shell交互时所输入的命令行必须符合Shell命令的语法和语义规范,才能够被Shell理解并执行.Shell所使用的这种命令语 ...