<题目链接>

题目大意:

给你一个长度为n的序列,这个序列每个数都有一个值,接下来进行q次询问,问在指定区间内出现次数最多的数出现了几次。

解题分析:

因为该序列是非降序的,所以该序列中的所有相等的数都是连续的,因此,我们可以先用dp预处理一下,dp[i]表示以第i个数结尾的最大连续相等个数,于是,本题目就变成了,在指定区间内,dp[i]的最大的值是多少。

 #include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std; const int M =1e5+;
int st[M][],dp[M],val[M];
int n,q;
void RMQ_init(){
for(int i=;i<=n;i++) //初始化
st[i][]=dp[i];
int k=log((double)(n+))/log(2.0);
for(int j=;j<=k;j++) //用倍增预处理ST表
for(int i=;i+(<<j)-<=n;i++)
st[i][j]=max(st[i][j-],st[i+(<<(j-))][j-]); //倍增递推式
}
int RMQ(int l,int r){
if(l>r)return ;
int k=log((double)(r-l+))/log(2.0);
return max(st[l][k],st[r-(<<k)+][k]);
}
int main(){
while(scanf("%d",&n)!=EOF,n){
scanf("%d",&q);
dp[]=;
for(int i=;i<=n;i++){
scanf("%d",&val[i]);
if(i>)dp[i]=(val[i]==val[i-]?dp[i-]+:); //dp[i]表示以第i个数结尾的最大连续相等个数
}
RMQ_init();
while(q--){
int l,r;
scanf("%d%d",&l,&r);
int tmp=l; //因为dp[i]表示以第i个数结尾的最大连续相等个数,而这里规定了区间的左下标,所以要对该区间与第一个值相同数的个数进行特殊处理,不能直接套用原来预先得到dp[l]
while(tmp<=r&&val[tmp]==val[tmp-])tmp++;
printf("%d\n",max(RMQ(tmp,r),tmp-l));
}
}
return ;
}

2018-10-19

poj 3368 Frequent values(经典)【RMQ】的更多相关文章

  1. POJ 3368 Frequent values (基础RMQ)

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

  2. poj 3368 Frequent values(RMQ)

    题目:http://poj.org/problem?id=3368 题意:给定n个数,顺序为非下降,询问某个区间内的数出现最多的数的 出现次数.. 大白书上的 例题..算是RMQ变形了, 对 原数组重 ...

  3. (简单) POJ 3368 Frequent values,RMQ。

    Description You are given a sequence of n integers a1 , a2 , ... , an in non-decreasing order. In ad ...

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

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

  5. poj 3368 Frequent values(RMQ)

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

  6. POJ 3368 Frequent values RMQ ST算法/线段树

                                                         Frequent values Time Limit: 2000MS   Memory Lim ...

  7. poj 3368 Frequent values(段树)

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

  8. POJ 3368 Frequent values(RMQ 求区间出现最多次数的数字的次数)

    题目链接:http://poj.org/problem? id=3368 Description You are given a sequence of n integers a1 , a2 , .. ...

  9. [RMQ] [线段树] POJ 3368 Frequent Values

    一句话,多次查询区间的众数的次数 注意多组数据!!!! RMQ方法: 预处理 i 及其之前相同的数的个数 再倒着预处理出 i 到不是与 a[i] 相等的位置之前的一个位置, 查询时分成相同的一段和不同 ...

随机推荐

  1. WebSocket服务端和客户端使用

    using System;using System.Collections.Generic;using System.IO;using System.Linq;using System.Net;usi ...

  2. vue.js 入门学习

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  3. LoadRunner学习笔记

    什么是性能测试: 简单说,功能测试是软件是否能用,性能测试是看软件好不好用: 性能测试的含义,大体来讲就是通过自动化的手段,模拟生产运行的业务压力或者相应的场景,来测试协同系统是否满足生产需要. 性能 ...

  4. JSP 隐式对象

    一. 隐式对象 JSP隐式对象 对象 类型 request javax.servlet.http.HttpServletRequest response javax.servlet.http.Http ...

  5. laravel 资源控制器

    Artisan 生成器来生成一个资源控制器(在之前命名后加上 --resource 选项) php artisan make:controller PostController --resource ...

  6. 网络编程—tcp

    一.TCP简介 TCP介绍 TCP协议,传输控制协议(英语:Transmission Control Protocol,缩写为 TCP)是一种面向连接的.可靠的.基于字节流的传输层通信协议,由IETF ...

  7. K/3 Cloud Web API接口说明文

    K/3 Cloud Web API接口说明文 目的 三方集成,提供第三方系统与Cloud集成调用接口. 技术实现 HTTP + Json 提供标准接口 编号 名称 说明 1 Kingdee.BOS.W ...

  8. asp.net core MVC 控制器,接收参数,数据绑定

    1.参数 HttpRequest HttpRequest 是用户请求对象 QueryString Form Cookie Session Header 实例: public IActionResult ...

  9. Task.Run()任务执行

    1)Task本身就是异步执行的(4.5的那个类). 2)控制数量和终止线程问题可以考虑这个模式: static async void RunAsync() { CancellationTokenSou ...

  10. WPF:如何高速更新Model中的属性

    原文:[WPF/MVVM] How to deal with fast changing properties In this article, I will describe a problem w ...