51Nod 1174 区间中最大的数

1174 区间中最大的数

基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 收藏 关注

描述

给出一个有N个数的序列,编号0 - N - 1。进行Q次查询,查询编号i至j的所有数中,最大的数是多少。

例如: 1 7 6 3 1。i = 1, j = 3,对应的数为7 6 3,最大的数为7。(该问题也被称为RMQ问题)

Input

第1行:1个数N,表示序列的长度。(2 <= N <= 10000)

第2 - N + 1行:每行1个数,对应序列中的元素。(0 <= S[i] <= 10^9)

第N + 2行:1个数Q,表示查询的数量。(2 <= Q <= 10000)

第N + 3 - N + Q + 2行:每行2个数,对应查询的起始编号i和结束编号j。(0 <= i <= j <= N - 1)

Output

共Q行,对应每一个查询区间的最大值。

Input示例

5

1

7

6

3

1

3

0 1

1 3

3 4

Output示例

7

7

3

题解

RMQ(查询区间最值)模版。代码如下:

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <cmath>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <utility>
#define ll long long

using namespace std ;

const int MAXN = 1e4+10 ;
int max_[MAXN][32] ;
int arr_[MAXN] ;
int n ;

void rmq(){
    for ( int i = 1 ; i <= n ; i ++ ){
        max_[i][0] = arr_[i] ;
    }
    int k = log(n * 1.0) / log(2.0) ;
    for ( int j = 1 ; j <= k ; j ++ ){
        for ( int i = 1 ; i <= n ; i ++ ){
            if ( i + (1 << j) - 1 > n ) break ;
            max_[i][j] = max(max_[i][j - 1] , max_[i + (1 << (j - 1))][j - 1]) ;
        }
    }
    return ;
}

int query( int x , int y ){
    int k = log(y - x + 1.0) / log(2.0) ;
    return max(max_[x][k] , max_[y + 1 - (1 << k)][k]) ;
}

int main(){
    cin >> n ;
    for ( int i = 1 ; i <= n ; i ++ ){
        cin >> arr_[i] ;
    }
    int t ;
    cin >> t ;
    rmq() ;
    while ( t -- ){
        int x , y ;
        cin >> x >> y ;
        cout << query( x + 1 , y + 1 ) << endl ;
    }
    return 0 ;
}

原文地址:https://www.cnblogs.com/Cantredo/p/9813843.html

时间: 2024-08-28 12:16:05

51Nod 1174 区间中最大的数的相关文章

(DP ST表 线段树)51NOD 1174 区间中最大的数

给出一个有N个数的序列,编号0 - N - 1.进行Q次查询,查询编号i至j的所有数中,最大的数是多少. 例如: 1 7 6 3 1.i = 1, j = 3,对应的数为7 6 3,最大的数为7.(该问题也被称为RMQ问题) Input 第1行:1个数N,表示序列的长度.(2 <= N <= 10000) 第2 - N + 1行:每行1个数,对应序列中的元素.(0 <= S[i] <= 10^9) 第N + 2行:1个数Q,表示查询的数量.(2 <= Q <= 1000

51Nod—1174 区间中最大的数 线段树模版

在大佬们题解的帮助下算是看懂了线段树吧...在这mark下防一手转头就忘. #include<iostream> #include<stdio.h> using namespace std; struct ki { int m,l,r; }tree[40005]; int ans=-1,a[10005]; void build(int n,int l,int r) { tree[n].l=l; tree[n].r=r; if(l==r) { tree[n].m=a[l];retur

51Nod 1174 区间中最大的数(RMQ)

1 #include <iostream> 2 #include <algorithm> 3 #include <cstring> 4 5 using namespace std; 6 const int maxn = 10000 + 5; 7 int a[maxn]; 8 int f[maxn][15]; 9 10 void rmq(int cnt){ 11 memset(f, 0, sizeof(f)); 12 for (int i = 1; i <= cnt

51nod 1174 1174 区间中最大的数

题目链接:51nod 1174 1174 区间中最大的数 ST(Sparse Table)算法学习参考博客:http://blog.csdn.net/niushuai666/article/details/6624672 O(nlogn)预处理,O(1)查询 1 #include<cstdio> 2 #include<algorithm> 3 #include<cmath> 4 using namespace std; 5 const int N = 10001; 6

区间中最大的数RMQ

1174 区间中最大的数 dmax[i][j]表示区间[i,i+j<<2) 1 #include <iostream> 2 #include <stdio.h> 3 using namespace std; 4 int n,q,dmax[10010][25],a[10010]; 5 void init(){ 6 for(int i = 1; i <= n; i ++){ 7 dmax[i][0] = a[i]; 8 } 9 for(int j = 1; (1<

51nod 1062 序列中最大的数(打表预处理)

1062 序列中最大的数 题目来源: Ural 1079 基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题 收藏 关注 取消关注 有这样一个序列a: a[0] = 0 a[1] = 1 a[2i] = a[i] a[2i+1] = a[i] + a[i+1] 输入一个数N,求a[0] - a[n]中最大的数. a[0] = 0, a[1] = 1, a[2] = 1, a[3] = 2, a[4] = 1, a[5] = 3, a[6] = 2, a[7] =

51Nod 1062 序列中最大的数

1062 序列中最大的数 题目来源: Ural 1079 基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题 描述 有这样一个序列a: a[0] = 0 a[1] = 1 a[2i] = a[i] a[2i+1] = a[i] + a[i+1] 输入一个数N,求a[0] - a[n]中最大的数. a[0] = 0, a[1] = 1, a[2] = 1, a[3] = 2, a[4] = 1, a[5] = 3, a[6] = 2, a[7] = 3, a[8]

51Nod 1062 序列中最大的数 | 简单DP

#include "iostream" #include "cstdio" using namespace std; #define LL long long #define N 100020 int a[N],b[N]; void init() { a[0]=0;a[1]=a[2]=1; b[0]=0;b[1]=b[2]=1; for(int i=3;i<N;i++){ if(i%2) a[i]=a[i/2]+a[i/2+1]; else a[i]=a[i/

CDOJ 1104 求两个数列的子列的交集 查询区间小于A的数有多少个 主席树

求两个数列的子列的交集 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/problem/show/1104 Description 给两个数列A, B,长度分别为n1, n2,保证A中每个元素互不相同,保证B中每个元素互不相同..进行Q次询问,每次查找A[l1...r1]和B[l2..r2]的交集 集合 大小是多少.. 比如 A = {1,2,3,4,5,6,7},B = {7,6,5,4,3,2,1}