BZOJ 1683.City skyline 城市地平线

传送门

从左到右扫一遍,考虑什么时候会和之前形成同一幢房子从而不用统计

显然是当前的高度和之前某个点高度相同,并且它们之间没有更矮的建筑

考虑用一个单调栈维护一个单调上升的房子轮廓,然后对于扫到的每一个高度,看看栈里有没有相同的高度就行了

但是我比较傻逼,没想到,所以用 $set$ 去维护单调栈就可以维护的东西...

每个位置进出 $set$ 一次,复杂度 $O(n \log n)$

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<set>
using namespace std;
inline int read()
{
    int x=0,f=1; char ch=getchar();
    while(ch<'0'||ch>'9') { if(ch=='-') f=-1; ch=getchar(); }
    while(ch>='0'&&ch<='9') { x=(x<<1)+(x<<3)+(ch^48); ch=getchar(); }
    return x*f;
}
const int N=5e5+7;
int n,m,ans;
set <int> S;
set <int>::iterator it,pit;
int main()
{
    n=read(),m=read();
    int x,y; S.insert(0);
    for(int i=1;i<=n;i++)
    {
        x=read(),y=read();
        for(it=S.upper_bound(y);it!=S.end();it=S.upper_bound(y)) S.erase(it);
        if(S.find(y)==S.end()) { ans++; S.insert(y); }
    }
    printf("%d\n",ans);
    return 0;
}

 

posted @ 2019-08-08 13:58  LLTYYC  阅读(265)  评论(0编辑  收藏  举报