hdu A Famous City

A Famous City

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:
hdu A Famous City

题意:有 n 张拍有大厦的相片,一栋大厦可以出现在多张相片中,一张相片不会有多栋大厦,求大厦的最少数量;注意大厦的高度为0的情况。

分析:1 2 1 至少有 2 栋 因为高的可以挡住矮的 ;2 1 2 一定是 3 栋 因为高的不能挡住矮的;

        使用 set  依次来存储 大厦的高度,set 的作用判断高度当前的高度有没有出现过,如果没出现则 ans++ ,否则依次查找这个这个元素之间的所有元素,  有没有比他小的,且没有两个连续的两个高度相同

AC代码:

#include <stdio.h>
#define maxn 1500000
int a[maxn],lk[maxn];
int main ( )
{
int i,j;
int n,m=1;
int ans;
while(scanf("%d",&n)!=EOF)
{
for( i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
a[0]=-1;
lk[1]=0;
ans=(a[1]==0?0:1);
for( i=2;i<=n;i++)
{
lk[i]=i-1;
while(a[lk[i]]>a[i]) lk[i]=lk[lk[i]];
if(a[i]!=0&&a[lk[i]]!=a[i])
++ans;
}
printf("Case %d: %d\n",m++,ans);
}
return 0;
}

链接:http://acm.hdu.edu.cn/showproblem.php?pid=4252

 

 

你可能感兴趣的:(HDU)