zoukankan      html  css  js  c++  java
  • POJ 3264 Balanced Lineup

     
    Time Limit: 5000MS   Memory Limit: 65536K
    Total Submissions: 48497   Accepted: 22722
    Case Time Limit: 2000MS

    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 ≤ A ≤ B ≤ N), 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

    Source

    USACO 2007 January Silver
    题目大意:
    John有n头奶牛(1 ≤ n ≤ 50000),给定Q个询问区间(1 ≤ Q ≤ 200000)和每头奶牛的高度(1 ≤ 高度 ≤ 1000000),对于每个询问区间,询问在此区间内最高牛和最矮牛的高度差。
     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<cmath>
     5 using namespace std;
     6 int a[50005],fminnn[50005][35],fmaxxx[50005][35];
     7 int n,m;
     8 int main()
     9 {
    10     scanf("%d%d",&n,&m);
    11     for(int i=1;i<=n;i++)
    12     {
    13         scanf("%d",&a[i]);
    14         fminnn[i][0]=fmaxxx[i][0]=a[i];
    15     }
    16       
    17     for(int i=1,j=2;j<=n;j*=2,i++)
    18       for(int k=1;k+j-1<=n;k++)
    19         fminnn[k][i]=min(fminnn[k][i-1],fminnn[k+j/2][i-1]),fmaxxx[k][i]=max(fmaxxx[k][i-1],fmaxxx[k+j/2][i-1]);
         //预处理出出宽度为 1,2,4,8,... 的区间最小值
    20 // 处理最大值和最小值的两个表 一定要分开 否则会报错 21 for(int l,r,i=1;i<=m;i++) 22 { 23 scanf("%d%d",&l,&r); 24 int p=int(log(r-l+1)/log(2)+0.001); 25 int minn=min(fminnn[l][p],fminnn[r-(1<<p)+1][p]); 26 int maxx=max(fmaxxx[l][p],fmaxxx[r-(1<<p)+1][p]); 27 printf("%d ",maxx-minn); 28 } 29 30 31 return 0; 32 } 33 // 本题目我刚开始都用的 cin cout 然而都TLE了 换成scanf printf 就都过了~~~ -_- 34 // -_- -_- -_- -_- -_-

    思路: 开两个ST表,一个用于求最大,一个用于求最小

    中心思想:依次求出宽度为 1,2,4,8,... 的区间最小值,
    所有可能的位置都要计算一遍。通过合并两个窄区间,得到
    一个大区间的信息。

  • 相关阅读:
    datetime函数和random.seed()函数的应用
    TP5 display()
    _STORAGE_WRITE_ERROR_
    nginx下基于ThinkPHP框架的网站url重写
    Thinkphp3.2版本Controller和Action的访问方法
    Undefined class constant 'MYSQL_ATTR_INIT_COMMAND'
    如何将word公式粘贴到TinyMCE里面
    如何将word公式粘贴到eWebEditor里面
    如何将word公式粘贴到wangEditor里面
    如何将word公式粘贴到xhEditor里面
  • 原文地址:https://www.cnblogs.com/suishiguang/p/5966681.html
Copyright © 2011-2022 走看看