BZOJ 1628: [Usaco2007 Demo]City skyline【单调栈】

1628: [Usaco2007 Demo]City skyline

Time Limit: 5 Sec Memory Limit: 64 MB

Description
这里写图片描述

Input

第一行给出N,W
第二行到第N+1行:每行给出二个整数x,y,输入的x严格递增,并且第一个x总是1

Output

输出一个整数,表示城市中最少包含的建筑物数量

Sample Input

10 26
1 1
2 2
5 1
6 3
8 1
11 0
15 2
17 3
20 2
22 1
INPUT DETAILS:
The case mentioned above

Sample Output

6

题解

这题看起来很水,但是细节比较多,注意一下就可以了。

代码如下

#include<cstdio>
#include<stack>
#include<algorithm>
using namespace std;
int n,W,Ans;
stack<int> que;
int main(){
    #ifndef ONLINE_JUDGE
    freopen("prob.in","r",stdin);
    freopen("prob.out","w",stdout);
    #endif
    scanf("%d%d",&n,&W);Ans=n;que.push(0);
    for(int i=1;i<=n;i++){
        int x,y;scanf("%d%d",&x,&y);
        while(!que.empty()&&y<que.top()) que.pop();
        if(que.top()==y) Ans--;
        else que.push(y);
    }
    printf("%d\n",Ans);
    return 0;
}
posted @ 2018-05-16 21:31  XSamsara  阅读(151)  评论(0编辑  收藏  举报