1628: [Usaco2007 demo]city Skyline time limit:5 Sec Memory limit:64 MB
submit:320 solved:260
[Submit] [Status] [Discuss] The first line of Descriptioninput gives N,w the second line to the n+1 line: Each row gives two integers x, y, the input is strictly incremented, and the first X is always 1Output
Outputs an integer that represents the minimum number of buildings in the city
Sample Input10 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 Output6
HINT Source
Silver
A monotonic stack has at most a maximum of n buildings, and if there are two storeys of equal height and there is no lower building than these, the number of buildings can be-1.
#include <iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<algorithm>#include<Set>using namespacestd;intn,x,w,h[50005],ans,top,stack[50005];intMain () {scanf ("%d%d",&n,&W); Ans=N; for(intI=1; i<=n;i++) scanf ("%d%d",&x,&H[i]); for(intI=1; i<=n;i++) { while(H[i]<stack[top]) top--; if(H[i]==stack[top]) ans--; Elsestack[++top]=H[i]; } printf ("%d", ans); return 0;}
[Usaco2007 Demo] [BZOJ1628] City skyline