• 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
  • 相关阅读:
    新线程 handler
    解决获取View的width和Height为0的4种方法
    回调深入理解 同步回调 以android中View.OnClickListener为列
    回调函数
    android:layout_weight
    studio rendering problems
    android:exported属性
    Codeforces 1264C/1265E Beautiful Mirrors with queries (概率期望、DP)
    Codeforces 1254C/1255F Point Ordering (交互题)
    Codeforces 576D Flights for Regular Customers (图论、矩阵乘法、Bitset)
  • 原文地址:https://www.cnblogs.com/zyfzyf/p/3906358.html
Copyright © 2020-2023  润新知