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;
}
阅读更多

City Skyline

08-11

DescriptionnnThe best part of the day for Farmer John's cows is when the sun sets. They can see the skyline of the distant city. Bessie wonders how many buildings the city has. Write a program that assists the cows in calculating the minimum number of buildings in the city, given a profile of its skyline. nnThe city in profile is quite dull architecturally, featuring only box-shaped buildings. The skyline of a city on the horizon is somewhere between 1 and W units wide (1 <= W <= 1,000,000) and described using N (1 <= N <= 50,000) successive x and y coordinates (1 <= x <= W, 0 <= y <= 500,000), defining at what point the skyline changes to a certain height. nnAn example skyline could be: n.......................... n.....XX.........XXX....... n.XXX.XX.......XXXXXXX..... nXXXXXXXXXX....XXXXXXXXXXXX nnand would be encoded as (1,1), (2,2), (5,1), (6,3), (8,1), (11,0), (15,2), (17,3), (20,2), (22,1). nnThis skyline requires a minimum of 6 buildings to form; below is one possible set of six buildings whose could create the skyline above: nn.......................... .......................... n.....22.........333....... .....XX.........XXX....... n.111.22.......XX333XX..... .XXX.XX.......5555555..... nX111X22XXX....XX333XXXXXXX 4444444444....5555555XXXXX nn.......................... n.....XX.........XXX....... n.XXX.XX.......XXXXXXX..... nXXXXXXXXXX....666666666666nInputnn* Line 1: Two space separated integers: N and W nn* Lines 2..N+1: Two space separated integers, the x and y coordinate of a point where the skyline changes. The x coordinates are presented in strictly increasing order, and the first x coordinate will always be 1.nOutputnn* Line 1: The minimum number of buildings to create the described skyline.nSample Inputnn10 26n1 1n2 2n5 1n6 3n8 1n11 0n15 2n17 3n20 2n22 1nSample Outputnn6

没有更多推荐了,返回首页