A Famous City
Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 472 Accepted Submission(s): 190
Problem Description
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.
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
3
1 2 3
3
1 2 1
Sample Output
Case 1: 3
Case 2: 2
Hint
The possible configurations of the samples are illustrated below:
首先我想吐槽下這題目的給的測試數據,乍一看還以為求最大值(昨天比賽時就有人這麼寫)。 原本我以為是一棟房子可以在多張圖片中出現,所以是先排序,在求不同層數的房子。接連wa兩次! 得出一個結論,以後盡量看點點英語
題目原意是一棟房子可以接連出現在連續的幾張照片中,給出的數字只代表這張照片中最高的樓。例如這兩組數據:
5
1 2 1 2 1
10
1 2 3 4 5 4 3 2 1 0
的答案分別是3和5。第一組的三個1,可能是一棟一層的房子跨越了五張照片。而兩個2一定代表兩棟房子,因為中間的“1”。
題意知道後,這題就不難了!AC代碼如下
[cpp]
#include<stdio.h>
#include<string.h>
int a[100005];
bool flag[100005];
int main()
{
int n,i,j,s=1;
while(scanf("%d",&n)!=EOF)
{
int ans=0;
for(i=0;i<n;i++)
scanf("%d",&a[i]);
memset(flag,0,sizeof(flag));
for(i=0;i<n;i++)
{
if(a[i]==0) {flag[i]=1;continue;}
int t=a[i];
if(!flag[i])
for(j=i+1;;j++)
{
if(a[j]==t) flag[j]=1;
else if(a[j]<t) break;
}
}
for(i=0;i<n;i++)
if(!flag[i]) ans++;
printf("Case %d: %d\n",s++,ans);
}
return 0;
}
作者:cxb569262726