BZOJ1113 Poi2008 海报PLA【单调栈】【水】

 

BZOJ1113 Poi2008 海报PLA


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


直接单调栈扫过去就可以了
注意合并高度相等的情况


 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define fu(a,b,c) for(int a=b;a<=c;++a)
 4 #define fd(a,b,c) for(int a=b;a>=c;--a)
 5 #define N 300010
 6 int n,w[N],h[N];
 7 int s[N],top=0;
 8 int main(){
 9   scanf("%d",&n);
10   fu(i,1,n)scanf("%d%d",&w[i],&h[i]);
11   int ans=0;
12   fu(i,1,n){
13     while(top&&h[s[top]]>h[i])ans++,top--;
14     while(top&&h[s[top]]==h[i])top--;
15     s[++top]=i;
16   }
17   ans+=top;
18   printf("%d",ans);
19   return 0;
20 }

 

posted @ 2018-09-19 13:13  Dream_maker_yk  阅读(146)  评论(0编辑  收藏  举报