《算法竞赛入门经典-训练指南》P198
记录一下区间的左右边界就可以了
#include <iostream>
#include <stack>
#include <queue>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <set>
#include <vector>
#include <cstring>
#include <algorithm> #define INF 0x7fffffff
#define N 100010
#define M 1000010
#define LL long long
#define mod 95041567 using namespace std; int arr[N];
int num[N][3];
int dp[N][20]; void RMQ_init(int len){
for(int j = 1; (1 << j) <= len; ++ j)
for(int i = 1; i + (1 << j) - 1 < len; ++ i)
dp[i][j] = max(dp[i][j - 1], dp[i + (1 << (j - 1))][j - 1]);
} int RMQ(int L, int R){
if(R < L) return 0;
else if(R == L) return dp[R][0];
int k = 0;
while((1 << (k + 1)) <= R - L + 1) ++ k;
return max(dp[L][k], dp[R - (1 << k) + 1][k]);
} int main()
{
int n, q;
// freopen("in.txt","r",stdin);
while(scanf("%d", &n) != EOF){
if(! n) break;
scanf("%d", &q);
for(int i = 0; i < n; ++ i) scanf("%d", &arr[i]);
for(int i = 0; i <= n + 2; ++ i)
for(int j = 0; j <= 20; ++ j)
dp[i][j] = 0;
arr[n] = INF;
int p = 0;
int len = 0;
for(int i = 1; i <= n; ++ i)
if(arr[i] != arr[i - 1]){
for(int j = p; j <= i - 1; ++ j){
num[j][0] = i - 1;
num[j][1] = p;
num[j][2] = len;
}
dp[len ++][0] = i - p;
p = i;
}
RMQ_init(len);
int L, R;
for(int i = 0; i < q; ++ i){
scanf("%d %d", &L, &R);
if(arr[L - 1] == arr[R - 1]){
printf("%d\n", R - L + 1);
continue;
}
//printf("%s\n", "++");
p = num[L - 1][0] - (L - 1) + 1;
p = max(p, R - num[R - 1][1]);
p = max(p, RMQ(num[L - 1][2] + 1, num[R - 1][2] - 1));
printf("%d\n", p);
}
}
return 0;
}

11235 - Frequent values的更多相关文章

  1. RMQ算法 以及UVA 11235 Frequent Values(RMQ)

    RMQ算法 简单来说,RMQ算法是给定一组数据,求取区间[l,r]内的最大或最小值. 例如一组任意数据 5 6 8 1 3 11 45 78 59 66 4,求取区间(1,8)  内的最大值.数据量小 ...

  2. UVA - 11235 Frequent values

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

  3. 数据结构(RMQ):UVAoj 11235 Frequent values

    Frequent values You are given a sequence of n integers a1 , a2 , ... , an in non-decreasing order. I ...

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

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

  5. UVA 11235 Frequent values(RMQ)

    Frequent values TimeLimit:3000Ms , ... , an in non-decreasing order. In addition to that, you are gi ...

  6. UVA 11235 Frequent values 线段树/RMQ

    vjudge 上题目链接:UVA 11235 *******************************************************大白书上解释**************** ...

  7. UVA 11235 Frequent Values ---RMQ

    大白书上的例题,具体讲解见大白书,最好用用一个Log数组直接求k,这样就是纯O(1)了 #include <iostream> #include <cstdio> #inclu ...

  8. POJ 3368 & UVA 11235 - Frequent values

    题目链接:http://poj.org/problem?id=3368 RMQ应用题. 解题思路参考:http://blog.csdn.net/libin56842/article/details/4 ...

  9. UVa 11235 Frequent values (RMQ && 区间出现最多次的数的次数)

    题意 : 给出一个长度为 n 的不降序序列,并且给出 q 个形如(L, R)的问询,问你这个区间出现的最多次的数的次数. 分析 : 很自然的想到将区间“缩小”,例如1 1 2 3 3 3就可以变成2 ...

随机推荐

  1. linux delete files older than 3 days

    4 down vote accepted This is easy enough (although note that this goes by a modification time more t ...

  2. WEB-INF目录与META-INF目录的作用

    /WEB-INF/web.xml Web应用程序配置文件,描述了 servlet 和其他的应用组件配置及命名规则. /WEB-INF/classes/包含了站点所有用的 class 文件,包括 ser ...

  3. [swustoj 1023] Escape

    Escape     Description BH is in a maze,the maze is a matrix,he wants to escape! Input The input cons ...

  4. 工作中的 Vim 和 git

    ————————Vim———————— 1. gf 可以转到文件中指明路径的文件. 这样可以实现文件的快速切换. ctrl+o: A -> B,  返回A ctrl+6: A.B之间快速切换 2 ...

  5. Java [Leetcode 319]Bulb Switcher

    题目描述: There are n bulbs that are initially off. You first turn on all the bulbs. Then, you turn off ...

  6. Java [Leetcode 204]Count Primes

    题目描述: Description: Count the number of prime numbers less than a non-negative number, n. 解题思路: Let's ...

  7. Vagrant搭建Ubuntu-JavaEE开发环境——Tomcat+JDK+MySQL+dubbo+测试

    Vagrant搭建(Tomcat8+JDK7+MySQL5+dubbo) JDK 1.下载jdk 2.解压JDK tar -xzvf jdk-7u79-linux-x64.tar.gz 3.设置环境变 ...

  8. Meta标签详解(转)

    引言 您的个人网站即使做得再精彩,在“浩瀚如海”的网络空间中,也如一叶扁舟不易为人发现,如何推广个人网站,人们首先想到的方法无外乎以下几种: ● 在搜索引擎中登录自己的个人网站 ● 在知名网站加入你个 ...

  9. 我的web前端之路 分享些前端的好书(转)

    WEB前端研发工程师,在国内算是一个朝阳职业,这个领域没有学校的正规教育,大多数人都是靠自己自学成才.本文主要介绍自己从事web开发以来 (从大二至今)看过的书籍和自己的成长过程,目的是给想了解Jav ...

  10. mybatis实战教程(mybatis in action)之九:mybatis 代码生成工具的使用

    mybatis 应用程序,需要大量的配置文件,对于一个成百上千的数据库表来说,完全手工配置,这是一个很恐怖的工作量. 所以mybatis 官方也推出了一个mybatis代码生成工具的jar包. 今天花 ...