BZOJ1683: [Usaco2005 Nov]City skyline 城市地平线 单调栈

Description

Input

   第1行:2个用空格隔开的整数NW.

    2N+1行:每行包括2个用空格隔开的整数x,y,其意义如题中所述.输入中的x严格递增,并且第一个z总是x

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
题解
用单调栈来维护,横着的行并没有什么用,只需考虑竖着的列即可,求出可以减少的覆盖量,可以减少的情况是指在两列的高度是相同的(因为覆盖的楼是矩形),并且两列之间没有比他们高度小的,当单调栈退栈时,若有此种情况,那么计数器++,最后ans为n-计数器(因为最差的情况是用n个才能解决) 代码如下:
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <stack>
 4 using namespace std;
 5 const int MAXN=1000001;
 6 int s[MAXN];
 7 int main(int argc, char *argv[])
 8 {
 9     int n,a,b,top=0,cnt=0,t,w; 
10     scanf("%d%d",&n,&w);t=n;
11     s[++top]=0;
12     while(t--)
13     {
14         scanf("%d%d",&a,&b);
15         while(b<=s[top]&&top)
16         {
17             if(b==s[top]) cnt++;
18             top--;
19         } 
20         top++;
21         s[top]=b;
22     }
23     printf("%d\n",n-cnt);
24     return 0;
25 }

 


posted @ 2016-08-05 09:59  BeyondW  阅读(309)  评论(0编辑  收藏  举报