Bzoj1628 [usaco2007 demo] city skyline (monotonous stack)

Source: Internet
Author: User
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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.