天天看點

BZOJ 1113 海報PLA——————單調棧

BZOJ 1113 海報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

BZOJ 1113 海報PLA——————單調棧

Sample Output

4

BZOJ 1113 海報PLA——————單調棧

我們可以直接忽略長度,因為長度對這一道題的結果影響不大

/*--------- Hongjie ----------*/
/**
 *        ┏┓   ┏┓+ +
 *       ┏┛┻━━━┛┻┓ + +
 *       ┃       ┃
 *       ┃   ━   ┃ ++ + + +
 *       ████━████ ┃+
 *       ┃       ┃ +
 *       ┃   ┻   ┃
 *       ┃       ┃ + +
 *       ┗━┓   ┏━┛
 *         ┃   ┃
 *         ┃   ┃ + + + +
 *         ┃   ┃    Code is far away from bug with the animal protecting
 *         ┃   ┃ +     神獸保佑,代碼無bug
 *         ┃   ┃
 *         ┃   ┃  +
 *         ┃    ┗━━━┓ + +
 *         ┃        ┣┓
 *         ┃        ┏┛
 *         ┗┓┓┏━┳┓┏┛ + + + +
 *          ┃┫┫ ┃┫┫
 *          ┗┻┛ ┗┻┛+ + + +
 */

// #include<bits/stdc++.h>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<deque>
#include<cmath>
#include<cstdio>
#include<bitset>
#include<string>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

typedef long long ll;
typedef pair<int ,int> P;

const int INF  = 0x3f3f3f3f;
const int MAXN = 3e6+7;
int h[MAXN];

int main(){
        // freopen("../in.txt","r",stdin);
        // freopen("../out.txt","w",stdout);
        ios::sync_with_stdio(0);
        cin.tie(0);
        int n,x;
        while(cin>>n) {
                for(int i=0;i<n;++i) {
                        cin>>x>>h[i];
                }
                stack<int> s;
                int ans = n;
                for(int i=0;i<n;++i) {
                        while(!s.empty() && s.top() >= h[i]) {
                                if(h[i]==s.top()) //如果目前元素與棧頂元素相等,
                                        ans --;   //那麼它就可以與棧頂元素在同一個矩陣裡面
                                s.pop();
                        }
                        s.push(h[i]);
                }
                cout<<ans<<endl;
        }

        return 0;
}