zoukankan      html  css  js  c++  java
  • BZOJ1699: [Usaco2007 Jan]Balanced Lineup排队

    1699: [Usaco2007 Jan]Balanced Lineup排队

    Time Limit: 5 Sec  Memory Limit: 64 MB
    Submit: 933  Solved: 568
    [Submit][Status]

    Description

    每天,农夫 John 的N(1 <= N <= 50,000)头牛总是按同一序列排队. 有一天, John 决定让一些牛们玩一场飞盘比赛. 他准备找一群在对列中为置连续的牛来进行比赛. 但是为了避免水平悬殊,牛的身高不应该相差太大. John 准备了Q (1 <= Q <= 180,000) 个可能的牛的选择和所有牛的身高 (1 <= 身高 <= 1,000,000). 他想知道每一组里面最高和最低的牛的身高差别. 注意: 在最大数据上, 输入和输出将占用大部分运行时间.

    Input

    * 第一行: N 和 Q. * 第2..N+1行: 第i+1行是第i头牛的身高.

     * 第N+2..N+Q+1行: 两个整数, A 和 B (1 <= A <= B <= N), 表示从A到B的所有牛.

    Output

    *第1..Q行: 所有询问的回答 (最高和最低的牛的身高差), 每行一个.

    Sample Input

    6 3
    1
    7
    3
    4
    2
    5
    1 5
    4 6
    2 2

    Sample Output

    6
    3
    0

    HINT

     

    Source

    题解:
    裸的RMQ。。。
    代码:
     1 uses math;
     2 var i,n,m,k,mx,mi,x,y:longint;
     3     f,g:array[0..60000,0..20] of longint;
     4     a:array[0..60000] of longint;
     5 procedure rmq;
     6  var i,j:longint;
     7  begin
     8  fillchar(f,sizeof(f),60);
     9  for i:=1 to n do f[i,0]:=a[i];
    10  for j:=1 to trunc(ln(n)/ln(2)) do
    11   for i:=1 to n-1<<j+1 do
    12    f[i,j]:=min(f[i,j-1],f[i+1<<(j-1),j-1]);
    13  fillchar(g,sizeof(g),0);
    14  for i:=1 to n do g[i,0]:=a[i];
    15  for j:=1 to trunc(ln(n)/ln(2)) do
    16   for i:=1 to n-1<<j+1 do
    17    g[i,j]:=max(g[i,j-1],g[i+1<<(j-1),j-1]);
    18  end;
    19 begin
    20   assign(input,'input.txt');assign(output,'output.txt');
    21   reset(input);rewrite(output);
    22   readln(n,m);
    23   for i:=1 to n do readln(a[i]);
    24   rmq;
    25   for i:=1 to m do
    26     begin
    27       readln(x,y);
    28       k:=trunc(ln(y-x+1)/ln(2));
    29       mx:=max(g[x,k],g[y-1<<k+1,k]);
    30       mi:=min(f[x,k],f[y-1<<k+1,k]);
    31       writeln(mx-mi);
    32     end;
    33   close(input);close(output);
    34 end.  
    View Code
  • 相关阅读:
    MQTT TLS 加密传输
    python多进程并发redis
    各种消息队列的特点
    mqtt异步publish方法
    Numpy API Analysis
    Karma install steps for unit test of Angular JS app
    reinstall bower command
    Simulate getter in JavaScript by valueOf and toString method
    How to: Raise and Consume Events
    获取对象的类型信息 (JavaScript)
  • 原文地址:https://www.cnblogs.com/zyfzyf/p/3906358.html
Copyright © 2011-2022 走看看