Hdus 4252 a famous city

Source: Internet
Author: User
A famous citytime limit: 3000 msmemory limit: 32768 kbthis problem will be judged on HDU. Original ID: 4252
64-bit integer Io format: % i64d Java class name: Main after mr. B arrived in Warsaw, he was shocked by the skyscrapers and took several photos. but now when he looks at these photos, he finds in surprise that he isn' t able to point out even the number of buildings in it. so he decides to work it out as follows:
-Divide the photo into n vertical pieces from left to right. the buildings in the photo can be treated as rectangles, the lower edge of which is the horizon. one building may span several consecutive pieces, but each piece can only contain one visible building, or no buildings at all.
-Measure the height of each building in that piece.
-Write a program to calculate the minimum number of buildings.
Mr. B has finished the first two steps, the last comes to you. inputeach test case starts with a line containing an integer N (1 <= n <= 100,000 ). following this is a line containing N integers-the height of building in each piece respectively. note that zero Height means there are no buildings in this piece at all. all the input numbers will be nonnegative and less than 1,000,000,000.
Outputfor each test case, display a single line containing the case number and the minimum possible number of buildings in the photo. sample input
31 2 331 2 1
Sample output
Case 1: 3Case 2: 2
HintThe possible deployments of the samples are using strated below: Sourcefudan local programming contest 2012: It seems that the complexity is N * n in the worst case, which is enough to see the weak data in this question...
 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib>10 #include <string>11 #include <set>12 #include <stack>13 #define LL long long14 #define pii pair<int,int>15 #define INF 0x3f3f3f3f16 using namespace std;17 const int maxn = 100010;18 int d[maxn],n,m;19 int main(){20     int cs = 1;21     while(~scanf("%d",&n)){22         for(int i = 0; i < n; ++i){23             scanf("%d",d+i);24         }25         m = n;26         if(d[0] == 0) --m;27         for(int i = 1; i < n; ++i){28             if(d[i] == 0) m--;29             else{30                 for(int j = i-1; j >= 0; --j){31                     if(d[j] < d[i]) break;32                     if(d[j] == d[i]){33                         --m;34                         break;35                     }36                 }37             }38         }39         printf("Case %d: %d\n",cs++,m);40     }41     return 0;42 }
View code

 

Monotonous Stack

 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib>10 #include <string>11 #include <set>12 #include <stack>13 #define LL long long14 #define pii pair<int,int>15 #define INF 0x3f3f3f3f16 using namespace std;17 stack<int>stk;18 int main(){19     int cs = 1,tmp,n;20     while(~scanf("%d",&n)){21         while(!stk.empty()) stk.pop();22         int ans = 0;23         for(int i = 0; i < n; ++i){24             scanf("%d",&tmp);25             while(!stk.empty() && stk.top() > tmp){26                 ans++;27                 stk.pop();28             }29             if(tmp && (stk.empty() || tmp > stk.top())) stk.push(tmp);30         }31         printf("Case %d: %d\n",cs++,ans + stk.size());32     }33     return 0;34 }
View code

 

Hdus 4252 a famous city

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.