A Famous City

                               Time Limit:7000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu

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 even able to point out 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 remaining 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.

Example

Input:
3
1 2 3
3
1 2 1

Output:
Case 1: 3
Case 2: 2

Explanation

The possible configurations of the samples are illustrated below:

题意:
一张相片上的很多建筑相互遮住了,根据高低不同就在相片上把一座高楼的可见部分作为一个矩形,
并用数字描述其高度,若一张相片上的两个建筑群中间有空地,高度则为0;求最少有多少个建筑;
思路:
1 2 1最少为2座,因为2把1挡住了,而3 1 3则为3座,1挡不住3;
如:1 2 3 1 2 3 0 1
输入的0不是建筑,则输入的总个数减去0的个数就是最多可能有的建筑数,设为s;
再从第一个开始从前往后搜,若发现比它高的则继续搜,若发现比它低的则说明它是一个独立的建筑,
这时可以返回搜其它的;若与它相等,说明这时它们还没有出现比它们低的建筑,则视其为同一个建筑,
则sum减1;

代码:

#include<stdio.h>
int main()
{
    int n,a[100001],t,i,j,k,sum;
    t=1;
    while(~scanf("%d",&n))
    {
        sum=n;
        for(i=0;i<n;i++)
        {
            scanf("%d",&a[i]);
        }
        for(i=0;i<n;i++)
        {
            if(a[i]==0)
               sum--;
            else
            {
               for(j=i+1;j<n;j++)
               {
                  if(a[i]>a[j])
                    break;
                  else if(a[i]==a[j])
                 {
                     sum--;
                     break;
                 }
               }
            }
        }
        printf("Case %d: ",t++);
        printf("%d\n",sum);
    }
    return 0;
}

A Famous City

时间: 2024-11-01 12:24:18

A Famous City的相关文章

HDOJ 4252 A Famous City 单调栈

单调栈: 维护一个单调栈 A Famous City Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1671    Accepted Submission(s): 644 Problem Description After Mr. B arrived in Warsaw, he was shocked by the skyscrap

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 p

HDU4252 A Famous City

题解: set 此题关键是理解题意 题意链接:http://blog.csdn.net/luyuncheng/article/details/8053971 代码: #include<iostream> #include<cstring> #include<cstdio> #include<algorithm> #include<cmath> #include<map> #include<set> #include<

单调栈3_水到极致的题 HDOJ4252

A Famous City 题目大意 给出正视图  每一列为楼的高度 最少有几座楼 坑点 楼高度可以为0 代表没有楼 贡献了两发RE 原因 if(!s.empty()&&tem){s.push(tem); continue;}并不能筛去 空栈且 tem为0的情况 改为 if(!s.empty()){if(tem) s.push(tem); continue;} 后AC 题目思路 维护一个单调递增的栈  假如新加入的楼高度小于top元素 那我们知道top元素一定是单独的一栋楼 我们就pop掉

考研英语复试

一.复试中的口试要求 1.评价标准: (1)语言准确性(语法和用词的准确性.语法结构的复杂性.词汇的丰富程度.发音的准确性) (2)话语的长短和连贯性(内容的连贯性.寻找合适词语而造成的停顿频率及长短.表达思想的语言长短等) (3)语言的灵活性和适合性(语言表达是否灵活.自然,话语是否得体,语言能否与语境.动能和目的相适应) 2.口语测试一般包含如下两部分: 第一部分:考查学生理解并回答有关日常生活.家庭.工作.学习等问题的能力(3--5分钟). 第二部分:考查学生连续表达的能力.考生从所给的问

hdu 3624 City Planning(暴力,也可扫描线)

City Planning Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 503    Accepted Submission(s): 213 Problem Description After many years, the buildings in HDU has become very old. It need to rebui

HDU 3634 City Planning (离散化)

题意  给你n个矩形  每个矩形都有自己的value  你可以任意改变矩形的表里关系   被覆盖的地方的value取最表层的   求总value的最大值 刚看了扫描线  感觉这个可以用扫描线做就直接写了  其实直接离散化就行了  因为最多也就20个矩形  那坐标最多也就40个  那我们对坐标进行离散化  然后将矩形按value从小到大一个个的放  暴力更新覆盖格子的value  最后直接将2n * 2n个小格子的value加起来就行了 #include <cstdio> #include &l

Textile City Textile World of loss and rebirth

From Zhejiang keqiao " China Textile City" buy, to Zhengzhou to sell, then radiation towards the surrounding provinces , when the enterprise is extremely, really superb cash each day tired . Dengsanlun enable people today shipping laborer , some

City Upgrades

City Upgrades Time limit: 1000 msMemory limit: 128 MB There are N cities placed in a line. For each city ii you know its coodinate x??. You can upgrade exactly K of these cities. Your goal is to choose what cities to upgrade in a way the minimizes th