zoukankan      html  css  js  c++  java
  • BZOJ1113: [Poi2008]海报PLA

    1113: [Poi2008]海报PLA

    Time Limit: 10 Sec  Memory Limit: 162 MB
    Submit: 691  Solved: 412
    [Submit][Status]

    Description

    N个矩形,排成一排. 现在希望用尽量少的矩形海报Cover住它们.

    Input

    第一行给出数字N,代表有N个矩形.N在[1,250000] 下面N行,每行给出矩形的长与宽.其值在[1,1000000000]2 1/2 Postering

    Output

    最少数量的海报数.

    Sample Input

    5
    1 2
    1 3
    2 2
    2 5
    1 4


    Sample Output

    4


    HINT

     

    Source

    题解:

    ans最多等于n,那么什么情况下ans可以减小呢

    首先如果减少了,肯定是贴了一张海报,覆盖住两张高度相同的矩形(宽度是打酱油的。。。)

    那么就如果 h[i]=h[j] 那么就得保证 i 与 j 之间没有 比它们高度更小的了

    咦?想到了什么,对了,单调栈!维护一个单调递减栈,当该元素=栈顶元素时ans--,并将栈顶元素弹栈

    代码:

     1 var top,ans,i,n:longint;
     2     a,sta:array[0..260000] of longint;
     3 procedure init;
     4  begin
     5    readln(n);
     6    for i:=1 to n do readln(a[i],a[i]);
     7  end;
     8 procedure main;
     9  begin
    10    top:=0;ans:=n;
    11    a[n+1]:=-1;
    12    for i:=1 to n+1 do
    13     begin
    14       while (top>0) and (a[i]<=a[sta[top]]) do
    15        begin
    16          if a[i]=a[sta[top]] then dec(ans);dec(top);
    17        end;
    18       inc(top);sta[top]:=i;
    19     end;
    20    writeln(ans);
    21  end;
    22 begin
    23   assign(input,'input.txt');assign(output,'output.txt');
    24   reset(input);rewrite(output);
    25   init;
    26   main;
    27   close(input);close(output);
    28 end.  
    View Code
  • 相关阅读:
    Bootstrap-table一个表格组件神器(学习二)实战
    JS基础_for循环练习3
    JS基础_for循环练习2
    JS基础_for循环练习1
    JS基础_for循环
    JS基础_while的练习2
    JS基础_while的练习1
    JS基础_while循环
    JS基础_条件分支语句:switch语句
    JS基础_if注意问题
  • 原文地址:https://www.cnblogs.com/zyfzyf/p/3905121.html
Copyright © 2011-2022 走看看