zoukankan      html  css  js  c++  java
  • POJ

    ..

    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

    ..

    TLE代码,思想朴素的说QWQ

     1 #include <iostream>
     2 #include <algorithm>
     3 #include <cmath>
     4 #include <stdio.h>
     5 #include <cstring>
     6 #include <string>
     7 #include <cstdlib>
     8 #include <queue>
     9 #include <stack>
    10 #include <set>
    11 #include <vector>
    12 #include <map>
    13 #include <list>
    14 #include <iomanip>
    15  #include <fstream>
    16 using namespace std;
    17 typedef long long ll;
    18 const int maxn=1000009;
    19 
    20 struct per{
    21     int h;
    22     int id;
    23 }a[50007]; 
    24 
    25 bool high(per a,per b)
    26 {
    27     return a.h<b.h;
    28 }
    29 
    30 bool keep(per a,per b)
    31 {
    32     return a.id<b.id;
    33 } 
    34 int main()
    35 {
    36     int n,q,big=0,sma=0x3f3f3f3f;
    37     scanf("%d%d",&n,&q);
    38     for(int i=1;i<=n;++i)
    39     {
    40         scanf("%d",&a[i].h);
    41         a[i].id=i;
    42     }
    43     while(q--)
    44     {
    45         int l,r;
    46         scanf("%d%d",&l,&r);
    47         //突然感觉这里可以利用倍增快速查询区间最大最小值?||感觉又不太像 
    48         //sort排序会打乱顺序emm 
    49          //所以先搞个区间高度排序的
    50          sort(a+l,a+r+1,high);
    51 //         for(int i=l;i<=r;++i)
    52 //         {
    53 //             cout<<"sort:"<<i<<":"<<a[i].h<<endl;
    54 //         }
    55 //         printf("==============
    ");
    56          printf("%d
    ",a[r].h-a[l].h);
    57          //再按编号排序恢复咳咳 
    58          sort(a+l,a+r+1,keep);
    59 //         for(int i=l;i<=r;++i)
    60 //         {
    61 //             cout<<"sort:"<<i<<":"<<a[i].id<<" "<<a[i].h<<endl;
    62 //         }
    63 //         printf("==============
    ");
    64     }
    65     
    66     return 0;
    67 }
    View Code

    ..

    (内容来自计创17曲锦涛学长,方便复习查阅)

     1 void ST(){
     2     for(int i=1;i<=n;i++) f[i][0]=a[i];
     3     int t=log(n)/log(2)+1;
     4     for(int j=1;j<t;++j){
     5         for(int i=1;i<=n-(1<<j)+1;++i){
     6             f[i][j]=max(f[i][j-1],f[i+(1<<(j-1))][j-1]);
     7         }
     8     }
     9 }
    10 int stQuery(int l,int r){
    11     int k=log(r-l+1)/log(2);
    12     return max(f[l][k],f[r-(1<<k)+1][k]);
    13 }        
    View Code

    ..

  • 相关阅读:
    88. 合并两个有序数组
    680. 验证回文字符串 Ⅱ
    345. 反转字符串中的元音字母
    633. 平方数之和
    分支程序设计
    scanf函数(初学者)
    输入与输出(初学者)
    C语句详细(初学者)
    算术运算符和算术表达式(初学者)
    变量赋值(初学者)
  • 原文地址:https://www.cnblogs.com/greenaway07/p/11197409.html
Copyright © 2011-2022 走看看