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
  • 相关阅读:
    Server.UrlEncode UrlDecode 动态绑定gridview列发送接收乱码的问题
    gridview新用法,一直不知道gridview可以这么用
    vm workstation15 迁移至ESXi6.7步骤
    http 502与504的区别
    Asp.net项目部署ActiveReport
    不能在 Page 回调中调用 Response.Redirect 解决方法
    JQuery TextExt 控件使用
    通过ashx获取JSON数据的两种方式
    jQuery Mobile对话框插件
    替换文本框title提示文本
  • 原文地址:https://www.cnblogs.com/sdau--codeants/p/3483738.html
Copyright © 2011-2022 走看看