1. 程式人生 > >BZOJ1113 Poi2008 海報PLA【單調棧】【水】

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

直接單調棧掃過去就可以了 注意合併高度相等的情況

#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; }