【CCF】201312-3 最大矩形

来源:互联网 发布:榆林广电网络 编辑:程序博客网 时间:2024/06/06 04:45

试题名称: 最大的矩形
时间限制: 1.0s
内存限制: 256.0MB

问题描述
  在横轴上放了n个相邻的矩形,每个矩形的宽度是1,而第i(1 ≤ i ≤ n)个矩形的高度是hi。这n个矩形构成了一个直方图。例如,下图中六个矩形的高度就分别是3, 1, 6, 5, 2, 3。
   这里写图片描述

  请找出能放在给定直方图里面积最大的矩形,它的边要与坐标轴平行。对于上面给出的例子,最大矩形如下图所示的阴影部分,面积是10。
这里写图片描述

输入格式
  第一行包含一个整数n,即矩形的数量(1 ≤ n ≤ 1000)。
  第二行包含n 个整数h1, h2, … , hn,相邻的数之间由空格分隔。(1 ≤ hi ≤ 10000)。hi是第i个矩形的高度。

输出格式
  输出一行,包含一个整数,即给定直方图内的最大矩形的面积。

样例输入
6
3 1 6 5 2 3

样例输出
10

// CCF201312-3-最大矩形.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include<iostream>using namespace std;int _tmain(int argc, _TCHAR* argv[]){    int h[10000];    int n;    cin >> n;    for (int i = 0; i <n; i++)    {        cin >> h[i];    }    int s=h[0];    if (h[0] - h[1] <= 0)    {        int l = 2;        for (int j = 3; h[0] - h[j] <= 0, j < n; j++)        {            l++;        }        if (s < h[0] * l)            s = h[0] * l;    }    else    {        int l = 1;        if (s < h[0] * l)            s = h[0] * l;    }    for (int i = 1; i < n; i++)    {        if (h[i] - h[i - 1] <= 0)        {            if (h[i] - h[i + 1] <= 0)            {                int l = 3;                for (int j = i + 2; h[i] - h[j] <= 0, j < n; j++)                {                    l++;                }                if (s < h[i] * l)                    s = h[i] * l;            }            else            {                int l = 2;                if (s < h[i] * l)                    s = h[i] * l;            }        }        else if (h[i] - h[i + 1] <= 0)        {            int l = 2;            for (int j = i + 1; h[i] - h[j + 1] <= 0, j < n; j++)            {                l++;            }            if (s < h[i] * l)                s = h[i] * l;        }        else        {            int l = 1;            if (s < h[i] * l)                s = h[i] * l;        }    }    cout << s << endl;    return 0;}
0 0
原创粉丝点击