The first line of descriptioninput is given n, and the second line of W is given to the n + 1 line. Each line is given two integers x and y. The input x is strictly incrementing, and the first X is always 1 output.
Returns an integer indicating 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
$ W $ of the question is meaningless. We find that when the height $ h $ occurs once, the answer + 1 $ h $ appears twice in a sequence that first increases and then drops, obviously the answer is as long as + 1 then we can set $ ans = N $ to consider maintaining a monotonous incremental stack, When a number appears 2 times, $ ans-1 $
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #define re register 5 using namespace std; 6 int n,q,h,w,st[50005],tp,ans; 7 int main(){ 8 scanf("%d%d",&n,&w); ans=n; 9 for(re int i=1;i<=n;++i){10 scanf("%d%d",&q,&h);11 while(tp&&st[tp]>h) --tp;12 if(st[tp]==h) --ans;13 else st[++tp]=h; 14 }printf("%d",ans);15 return 0;16 }
View code
Bzoj1628 [usaco2007 demo] city skyline (monotonous stack)