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
直接单调栈扫过去就可以了
注意合并高度相等的情况
#include<bits/stdc++.h>
using namespace std;
#define fu(a,b,c) for(int a=b;a<=c;++a)
#define fd(a,b,c) for(int a=b;a>=c;--a)
#define N 300010
int n,w[N],h[N];
int s[N],top=0;
int main(){
scanf("%d",&n);
fu(i,1,n)scanf("%d%d",&w[i],&h[i]);
int ans=0;
fu(i,1,n){
while(top&&h[s[top]]>h[i])ans++,top--;
while(top&&h[s[top]]==h[i])top--;
s[++top]=i;
}
ans+=top;
printf("%d",ans);
return 0;
}