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

    A Famous City

    Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 2855    Accepted Submission(s): 1073


    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:
     
    Source
     
    题目大意是将下面的不同高度的区域,分割成尽量少的矩形,问矩形的数目
     
    对于每一个区域,如果它向左在到达高度小于它的区域之前
    出现了高度等于它的区域那么说明它能和该区域划分到同一矩形里面。
    否则划分为新的矩形区域。
    比如1 2 1,第三个1,向左找到第一个1,划分在同一矩形中。
    根据这个属性,也就是说,每个区域左边大于它的部分是不相关的,
    所以可以用单调栈进行处理。
    如要注意的是,如果某个区域的高度为0,则只起分割作用。
     
    代码如下:
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include <stack>
    using namespace std;
    const int MAXN=1e5+10;
    int n;
    int a[MAXN];
    int main()
    {
        int Case=0,ans;
        ios::sync_with_stdio(false);
        while(cin>>n)
        {
            Case++;
            ans=0;
            for(int i=1;i<=n;i++)
                cin>>a[i];
    
            stack<int>S;
            for(int i=1;i<=n;i++)
            {
              if(a[i]==0)
              {
                  while(!S.empty())
                    S.pop();
              }
              else
              {
                 if(S.empty())
                 {
                     S.push(i);
                     ans++;
                 }
                 else
                 {
                     while(!S.empty()&&a[S.top()]>a[i])
                     S.pop();
                     if(S.empty()||a[S.top()]<a[i])
                        ans++;
                     S.push(i);
                 }
              }
            }
            cout<<"Case "<<Case<<": "<<ans<<endl;
        }
    
        return 0;
    }
     
  • 相关阅读:
    javaScript第一篇
    数据结构与算法学习笔记2018-01-30
    各种编程技术文章收集链接
    git最简单直接粗爆与github教程
    Word2Vec原理之Negative Sample
    Word2Vec原理之层次Softmax算法
    统计语言模型
    各种优化方法总结比较(sgd/momentum/Nesterov/adagrad/adadelta)
    TensorBoard:可视化学习
    Batch Normalization
  • 原文地址:https://www.cnblogs.com/a249189046/p/8724665.html
Copyright © 2011-2022 走看看