• poj 3264 Balanced Lineup (RMQ)


     1 /*******************************************************
     2 题目:     Balanced Lineup(poj 3264)
     3 链接:     http://poj.org/problem?id=3264
     4 题意:     给个数列,查询一段区间的最大值与最小值的差
     5 算法:     RMQ
     6 ********************************************************/
     7 #include<cstdio>
     8 #include<cstring>
     9 #include<algorithm>
    10 #include<iostream>
    11 #include<cmath>
    12 using namespace std;
    13 
    14 const int mx=50010;
    15 int dpmax[mx][20];
    16 int dpmin[mx][20];
    17 int a[mx];
    18 int n,q;
    19 
    20 void makermq()
    21 {
    22     for (int i=1;i<=n;i++)
    23     {
    24         dpmax[i][0]=a[i];
    25         dpmin[i][0]=a[i];
    26     }
    27     for (int j=1;(1<<j)<=n;j++)
    28     {
    29         for (int i=1;i+(1<<j)-1<=n;i++)
    30         {
    31             dpmax[i][j]=max(dpmax[i][j-1],dpmax[i+(1<<(j-1))][j-1]);
    32             dpmin[i][j]=min(dpmin[i][j-1],dpmin[i+(1<<(j-1))][j-1]);
    33         }
    34     }
    35 
    36 }
    37 
    38 int rmq(int u,int v)
    39 {
    40     int k=(int)(log(v-u+1)/log(2.0));
    41     return max(dpmax[u][k],dpmax[v-(1<<k)+1][k])-min(dpmin[u][k],dpmin[v-(1<<k)+1][k]);
    42 }
    43 
    44 int main()
    45 {
    46     scanf("%d%d",&n,&q);
    47     for (int i=1;i<=n;i++) scanf("%d",&a[i]);
    48     makermq();
    49     while (q--)
    50     {
    51         int u,v;
    52         scanf("%d%d",&u,&v);
    53         printf("%d
    ",rmq(u,v));
    54     }
    55 }
  • 相关阅读:
    centos7下安装Jdk8、Tomcat8
    CentOS7.3下使用YUM安装MySQL5.6
    数据库表直接生成Excel
    分页工具类
    ResponseUtil
    图片上传
    UUIDUtils
    html和js
    java
    Eclipse启动发生的错误An internal error occurred during: "Initializing Java Tooling".
  • 原文地址:https://www.cnblogs.com/pblr/p/5733542.html
Copyright © 2020-2023  润新知