zoukankan      html  css  js  c++  java
  • A Famous City(单调栈)

    4113: A Famous City 分享至QQ空间

    时间限制(普通/Java):2000MS/6000MS     内存限制:65536KByte
    总提交: 202            测试通过:83

    描述

     

    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.
     

    输入

     

    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.

    输出

     

    For each test case, display a single line containing the case number and the minimum possible number of buildings in the photo.
     

    样例输入

     

    3
    1 2 3
    3
    1 2 1

    样例输出

     

    Case 1: 3
    Case 2: 2

    提示

    The possible configurations of the samples are illustrated below:
     

    解题思路:题目意思是说:最少的楼 从小变大肯定不是同一幢楼从大到小如果和之前的一样那有可能是同一幢   

    维护一个不递减的栈!

     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 typedef long long ll;
     4 
     5 int n,jishu;
     6 int arr[100005];
     7 int que[100005];
     8 
     9 int main(){
    10     ios::sync_with_stdio(false);
    11     while(cin>>n){
    12         for(int i=1;i<=n;i++) cin>>arr[i];
    13         int num=n,top=0;
    14         for(int i=1;i<=n;i++){
    15             if(arr[i]==0){
    16                 top=0;
    17                 num--;   //这不是一幢楼
    18             }
    19             else{
    20                 if(top==0) que[++top]=arr[i];  //栈位空入站
    21                 else{
    22                     while(arr[i]<que[top]&&top){
    23                         top--;
    24                     }
    25                     if(arr[i]==que[top]) num--;   //和前面的楼可能是同一幢
    26                     que[++top]=arr[i];
    27                 }
    28             }
    29         }
    30         cout << "Case " << ++jishu << ": ";
    31         cout << num << endl;
    32     }
    33     return 0;
    34 }
    View Code
  • 相关阅读:
    volatile关键字
    线程的状态
    java中的匿名内部类
    java高精度实数和小数
    JS、JSP、ASP、CGI
    论文结构要求
    java中的标识符、关键字、保留字
    java IODemo
    Bagging和Boosting的区别
    由Memcached升级到 Couchbase的 Java 客户端的过程记录(一)
  • 原文地址:https://www.cnblogs.com/qq-1585047819/p/11323366.html
Copyright © 2011-2022 走看看