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