首页 > 代码库 > HDU 4252 A Famous City
HDU 4252 A Famous City
A Famous City
Time Limit: 3000ms
Memory Limit: 32768KB
This problem will be judged on HDU. Original ID: 425264-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.
- 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.
Input
Each 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.
Output
For 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
Hint
The possible configurations of the samples are illustrated below:Source
Fudan Local Programming Contest 2012
解题:这个写法貌似最坏情况下复杂度是n*n的居然也可以过,足以见此题数据之弱。。。
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 }
单调栈
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 }
HDU 4252 A Famous City
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。