zoukankan      html  css  js  c++  java
  • Largest Rectangle in a Histogram(dp)

    http://acm.hdu.edu.cn/showproblem.php?pid=1506

    题意:给出n个矩形的高度,每个矩形的宽都为1,求相邻的矩形能组合成的最大的矩形的面积。

    思路:求出比第i个矩形大的最左边的矩形的位置 l[i], 及比第i个矩形大的最右边的矩形的位置 r[i], 则第i个矩形的面积 s = (r[i]-l[i]+1)*hign[i]。

            如果第i-1个矩形比第i个矩形大,则 l[i] 必定在 l[i-1]的 左边,同理,r[i]必定在 r[i+1]的右边。

     1 #include <stdio.h>
     2 #include <string.h>
     3 #include <algorithm>
     4 #define LL __int64
     5 const int N=100002;
     6 using namespace std;
     7 LL high[N],l[N],r[N];
     8 int main()
     9 {
    10     int n;
    11     while(~scanf("%d",&n)&&n)
    12     {
    13         for (int i = 1; i <= n; i++)
    14             scanf("%I64d",&high[i]);
    15         for (int i = 1; i <= n; i++)
    16         {
    17             int pos = i;
    18             while(pos > 1 && high[pos-1] >= high[i])
    19             {
    20                 pos = l[pos-1];//如果第i个矩形左边的矩形(pos-1)比i高,
    21                               //则pos移动到l[pos-1](即比第pos-1个矩形高的最左边的矩形的位置)
    22             }
    23             l[i] = pos;//l[i]表示比第i个矩形连续高的最左边的矩形位置
    24         }
    25         for (int i = n; i >= 1; --i)
    26         {
    27             int pos = i;
    28             while(pos < n && high[i] <= high[pos+1])
    29             {
    30                 pos = r[pos+1];
    31             }
    32             r[i] = pos;//表示比第i个矩形连续高的最右边的矩形位置
    33         }
    34         LL maxn = 0;
    35         for (int i = 1; i <= n; i++)
    36         {
    37             maxn = max((r[i]-l[i]+1)*high[i],maxn);
    38         }
    39         printf("%I64d
    ",maxn);
    40     }
    41     return 0;
    42 }
    View Code

    同类型的题:

    http://acm.hdu.edu.cn/showproblem.php?pid=1505

     1 #include <stdio.h>
     2 #include <algorithm>
     3 #include <string.h>
     4 #define LL __int64
     5 const int N=100002;
     6 using namespace std;
     7 LL l[N],r[N],high[N];
     8 
     9 LL max_area(int m)
    10 {
    11     for (int i = 1; i <= m; i++)
    12     {
    13         if(i > 1 && high[i] <= high[i-1])
    14         {
    15             l[i] = l[i-1];
    16         }
    17         else
    18             l[i] = i;
    19     }
    20     for (int i = m; i >= 1; i--)
    21     {
    22 
    23         if(i < m && high[i] <= high[i+1])
    24         {
    25             r[i] = r[i+1];
    26         }
    27         else
    28             r[i] = i;
    29     }
    30     LL maxn = 0;
    31     for (int i = 1; i <= m; i++)
    32     {
    33         maxn = max((r[i]-l[i]+1)*high[i],maxn);
    34     }
    35     return maxn;
    36 }
    37 int main()
    38 {
    39     int t;
    40     char s[23];
    41     scanf("%d",&t);
    42     while(t--)
    43     {
    44         int n,m;
    45         LL ans = 0;
    46         scanf("%d %d",&n,&m);
    47         memset(high,0,sizeof(high));
    48         for (int i = 1; i <= n; i++)
    49         {
    50             for (int j = 1; j <= m; j++)
    51             {
    52                 scanf("%s",s);
    53                 if (s[0]=='F')
    54                     high[j]++;
    55                 else
    56                     high[j] = 0;
    57             }
    58             ans = max(max_area(m),ans);//第一行到第i行最大的面积
    59         }
    60         printf("%I64d
    ",ans*3);
    61     }
    62     return 0;
    63 }
    View Code
  • 相关阅读:
    为什么一看编程技术文章就这么兴奋呢?
    今天弄会了如何使用VB6使用AutoCAD提供的dll操作AutoCAD了
    简单工厂模式没想像中的难啊
    Linq本质论,C#3.0的特性。写的太好了!收藏!
    struct和class的区别
    饿了
    解决Eclipse 3.3+lomboz无法启动的问题
    IE8通过Acid2了
    Linux档案/目录 权限及修改
    Linux 初识man、info
  • 原文地址:https://www.cnblogs.com/lahblogs/p/3421652.html
Copyright © 2011-2022 走看看