Bzoj1113: [poi2008] Poster PLA

Source: Internet
Author: User
1113: [poi2008] Poster platime limit: 10 sec memory limit: 162 MB
Submit: 691 solved: 412
[Submit] [Status] Descriptionn rectangles in a row. now we want to cover them with a small number of rectangular posters. the first line of input gives the number N, which indicates that there are n rectangles. N is in N rows under [], and each row gives the length and width of the rectangle. the number of posters with a value of [1, 1/2] 2 posteringoutput is the minimum. sample input5
1 2
1 3
2 2
2 5
1 4


Sample output4


Hint Source

Question:

Ans can be equal to N at most. Under what circumstances can ans be reduced?

First, if it is reduced, it must have been pasted with a poster covering two rectangles with the same height (the width is soy sauce ...)

Then, if H [I] = H [J], we must ensure that there is no lower height between I and j than they are.

Why? Think of something, right, monotonous stack! Maintain a monotonically decreasing stack. When the element is equal to the top element of the stack, ANS -- and stack the top element.

Code:

 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  begin10    top:=0;ans:=n;11    a[n+1]:=-1;12    for i:=1 to n+1 do13     begin14       while (top>0) and (a[i]<=a[sta[top]]) do15        begin16          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 begin23   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

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.