• 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;
    }
     
  • 相关阅读:
    python运行js---execjs 使用
    使用百度文字识别API进行图片中文字的识别
    cpca 使用python提取中文地址描述中的省市区信息
    Android 设备信息获取详解
    Android实现左滑退出Activity(完美封装)
    Postman测试Soap协议接口
    如何使用postman带Token测试接口?
    Postman高级使用——Tests 断言校验返回结果
    Android Service完全解析,关于服务你所需知道的一切(下)
    Android Service完全解析,关于服务你所需知道的一切(上)
  • 原文地址:https://www.cnblogs.com/a249189046/p/8724665.html
Copyright © 2020-2023  润新知