【P3467】PLA-Postering【單調棧】

P3467 [POI2008]PLA-Postering
  1. 題目來源

    P3467 [POI2008]PLA-Postering

  2. 題目意思

    N個矩形,排成一排。現在希望用盡量少的矩形海報Cover住它們。

  3. 題解

    我們發現:答案與寬度是沒有關係的,於是我們只需要按高度維護一個單調遞增

    如果發現當前高度已經在棧中,就不需要另外一張海報了。

    於是用一個ans變量記錄一下,最後用總海報數減去不需要另外的海報數便是答案。

  4. AC代碼

    #include<set>
    #include<map>
    #include<queue>
    #include<stack>
    #include<vector>
    #include<cmath>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    
    typedef long long ll;
    const int maxn = 250000+10;
    int a[maxn];
    int main(){
        int n;
        cin >> n;
        int ans = 0;
        stack<int> q;
        q.push(0);
        for(int i = 0; i < n; i++){
            int x;
            cin >> x;
            cin >> a[i];
            while(a[i]<=q.top()){
                int tmp = q.top();
                if(tmp==a[i]) ans++;
                q.pop();
            }
            q.push(a[i]);
        }
        cout<<n-ans<<endl;
        return 0;
    }
    

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章