zoukankan      html  css  js  c++  java
  • Balanced Lineup RMQ 中的ST算法(第一次做)

    Problem Description

    For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the same order. One day Farmer John decides to organize a game of Ultimate Frisbee with some of the cows. To keep things simple, he will take a contiguous range of cows from the milking lineup to play the game. However, for all the cows to have fun they should not differ too much in height.

    Farmer John has made a list of Q (1 ≤ Q ≤ 200,000) potential groups of cows and their heights (1 ≤ height ≤ 1,000,000). For each group, he wants your help to determine the difference in height between the shortest and the tallest cow in the group.

     
    Input
    Line 1: Two space-separated integers, N and Q.
    Lines 2..N+1: Line i+1 contains a single integer that is the height of cow i
    Lines N+2..N+Q+1: Two integers A and B (1 ≤ ABN), representing the range of cows from A to B inclusive.
     
    Output
    Lines 1..Q: Each line contains a single integer that is a response to a reply and indicates the difference in height between the tallest and shortest cow in the range.
     
    Sample Input
    6 3 1 7 3 4 2 5 1 5 4 6 2 2
     
    Sample Output
    6 3 0
    ***********************************************************************************************************************************
    dp[i][j]表示在区间(i,i+2^j-1)中所取得最值;
    其中求最值时也有优点dp,每次把最值保存在dp数组中
    ST算法最主要的优点是查询的时候费时为o(1);
    ***********************************************************************************************************************************
     1 #include<iostream>
     2 #include<string>
     3 #include<cstring>
     4 #include<cmath>
     5 #include<queue>
     6 #include<stack>
     7 #include<map>
     8 #include<cstdio>
     9 using namespace std;
    10 int cow[51001];
    11 int n,m,p;
    12 int dpmax[51001][51],dpmin[51001][51];
    13 void maxmp(int n)
    14 {
    15     for(int it=1; it<=n; it++)
    16         dpmax[it][0]=cow[it];
    17     for(int it=1; it<=(log((double)n)/log(2.0)); it++)
    18         for(int jt=1; jt+(1<<it)-1<=n; jt++)
    19             dpmax[jt][it]=max(dpmax[jt][it-1],dpmax[jt+(1<<(it-1))][it-1]);
    20 }
    21 
    22 void minmp(int n)
    23 {
    24     for(int it=1; it<=n; it++)
    25         dpmin[it][0]=cow[it];
    26     for(int it=1; it<=(log((double)n)/log(2.0)); it++)
    27         for(int jt=1; jt+(1<<it)-1<=n; jt++)
    28             dpmin[jt][it]=min(dpmin[jt][it-1],dpmin[jt+(1<<(it-1))][it-1]);
    29 }
    30 
    31 int querymax(int s,int v)
    32 {
    33     int k=(int)((log((double)(v-s+1))/log(2.0)));
    34     return max(dpmax[s][k],dpmax[v-(1<<k)+1][k]);
    35 }
    36 
    37 int querymin(int s,int v)
    38 {
    39     int k=(int)((log((double)(v-s+1))/log(2.0)));
    40     return min(dpmin[s][k],dpmin[v-(1<<k)+1][k]);
    41 }
    42 
    43 int main()
    44 {
    45 
    46     while(~scanf("%d %d",&n,&p))
    47     {
    48         for(int i = 1 ; i <= n ; ++i)
    49             scanf("%d",&cow[i]);
    50 
    51         maxmp(n);
    52         minmp(n);
    53         while(p--)
    54         {
    55             int s,v;
    56             scanf("%d%d",&s,&v);
    57             printf("%d
    ",querymax(s,v)-querymin(s,v));
    58         }
    59     }
    60     return 0;
    61 
    62 }
    View Code
  • 相关阅读:
    [服务器]Windows Server 2008 64位1核1G安装SQL Server2008
    [工作]离职了!好好休息一下
    [工作]IT连和IT恋产品已完成第一版,准备上线运营
    [SQL Server]储存过程中使用临时表循环操作数据
    [Swift]Xcode格式化代码快捷键
    [Swift]使用Alamofire传递参数时报错
    [工作]记录一下目前的工作
    [Swift]Swift图片显示方式设置,控件UIImageView的contentMode属性设置
    [Swift]创建桥接文件,Swift使用MJRefresh刷新插件
    我遇到了改变的机会
  • 原文地址:https://www.cnblogs.com/sdau--codeants/p/3483738.html
Copyright © 2011-2022 走看看