1. 程式人生 > >poj2559(單調棧)

poj2559(單調棧)

題目連結:http://poj.org/problem?id=2559

Largest Rectangle in a Histogram
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 20199 Accepted: 6489

Description

A histogram is a polygon composed of a sequence of rectangles aligned at a common base line. The rectangles have equal widths but may have different heights. For example, the figure on the left shows the histogram that consists of rectangles with the heights 2, 1, 4, 5, 1, 3, 3, measured in units where 1 is the width of the rectangles: 

Usually, histograms are used to represent discrete distributions, e.g., the frequencies of characters in texts. Note that the order of the rectangles, i.e., their heights, is important. Calculate the area of the largest rectangle in a histogram that is aligned at the common base line, too. The figure on the right shows the largest aligned rectangle for the depicted histogram.

Input

The input contains several test cases. Each test case describes a histogram and starts with an integer n, denoting the number of rectangles it is composed of. You may assume that 1<=n<=100000. Then follow n integersh1,...,hn, where 0<=hi<=1000000000. These numbers denote the heights of the rectangles of the histogram in left-to-right order. The width of each rectangle is 1
. A zero follows the input for the last test case.

Output

For each test case output on a single line the area of the largest rectangle in the specified histogram. Remember that this rectangle must be aligned at the common base line.

Sample Input

7 2 1 4 5 1 3 3
4 1000 1000 1000 1000
0

Sample Output

8
4000

Hint

Huge input, scanf is recommended.

尷尬,只會n^2做法,只能水過學校OJ的資料,水不過poj原題,.........

沒想到就是沒想到,菜得摳腳.......

網上有個比較巧的思路:

使用一個叫“單調棧”的東西

想法很巧妙:將木板從小到大扔進棧中,一旦手裡拿的準備進棧的木板(稱之為A板)比棧頂的木板還大

就把棧頂的木板拉出來(稱之為B板),統計一下面積,與ans比較,存起來最大的,

然後再把棧頂的板拉出來(稱之為C板),如果還是小於準備進棧的A板,那麼把B板切成C板大小,

統計“兩塊C板”的面積,與ans比較,存最大(原因:因為棧是單調棧,從小到大存放,那麼拉出來的C板肯定比B板小

那麼B板肯定可以切成C板大小,那麼統計時就有兩塊C板合成的大板),如此往復......

直到棧頂的板終於不大於A板了,把之前那一堆疊頂彈出,不斷切小的板(B板C板等的疊起來的n塊板)

切成A板大小,與A板釘在一起扔進棧中,那麼此時相當於有 n + 1 塊A板合起來的大板(理解為面積)

切成和手裡的板的大小相同,在將手裡的兩塊板疊成一塊的(理解為面積不變,厚度增加......)

最後把所以棧中的存板全扔出來切,看最多能切成面積多大的板.......

為何能這麼幹??

首先:單調棧控制棧中板大小遞增

其次:一旦手裡板小於棧頂板,這意味著沒有其他比棧頂板更大的板可以切成棧頂的板,那麼面積為棧頂板大小的板只有1塊
(這裡還有一個前提:“合成板”只能是自身前後相鄰的幾塊組成,不能跳過某塊板與其他“合成”)

#include <iostream>
#include <algorithm>
#include <map>
#include <vector>
#include <queue>
#include <stack>
#include <queue>
#include <cstdio>
#include <string.h>
#include <cstdlib>
#include <cmath>
#include <string>
typedef long long ll;
#define CLR(a) memset(a, 0, sizeof(a))
#define SD(a) scanf("%d", &a)
#define FOR(i, a, b)  for(i = a; i < b; i++)
#define INF 0x3f3f3f3f
#define PI acos(-1.0)
#define MAXN 100000 + 10
using namespace std;

typedef struct
{
     ll width;
     ll height;
}Block;

Block arr[MAXN];
int main()
{
     int n;
     while (~scanf("%d", &n) && n > 0)
     {
          ll ans = 0;
          
          stack<Block> blockStack;
          for (int i = 0; i < n; i++)
          {
               scanf("%lld", &arr[i].height);
               ll tmp = 0;
               while (blockStack.empty() == false && arr[i].height < blockStack.top().height)
               {
                    ans = max(ans, (blockStack.top().width + tmp) * blockStack.top().height);
                    tmp += blockStack.top().width;
                    blockStack.pop();
               }
               arr[i].width = tmp + 1;
               blockStack.push(arr[i]);
          }

          ll tmp = 0;
          while (blockStack.empty() == false)
          {
               ans = max(ans, (blockStack.top().width + tmp) * blockStack.top().height);
               tmp += blockStack.top().width;
               blockStack.pop();
          }
          printf("%lld\n", ans);
     }

     return 0;
}


相關推薦

poj2559單調

esp ++ 矩形覆蓋 view sta sed std 所在 bsp 給一系列並排的矩形,寬都是1,長不同,求最大的矩形(可被上述矩形覆蓋)的面積 單調棧,棧中元素為每個值所在的位置,記錄下從每個值大於當前值所能到達最遠的左邊和右邊的距離,此時中間的值一定是最小,然後H*

poj2559(單調)

題目連結:http://poj.org/problem?id=2559 Largest Rectangle in a Histogram Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2019

poj2559單調)最大矩形面積

//單調棧 //思路很好的 #include<cstdio> #include<iostream> using namespace std; const int mn=10

poj2559 Largest Rectangle in a Histogram(單調

題意 有n個矩形,它們在一條水平線上且寬度都為1,長度分別為a[i]。求其中的最大子矩陣。 思考過程 我們從左往右列舉每一個矩形,第i個矩形依次嘗試寬度為1~n-i+1,記錄最低高度和最大面積

單調模板【poj2559

單調棧,就是具有單調性的棧。 本題你只要維護棧的單調性即可。每個矩形入棧時,判斷它的高度是否大於等於棧頂矩形的高度,如果滿足,則直接入棧。否則就向前找,一邊出棧一邊記錄寬度,計算面積。知道找到第一

POJ2559 Largest Rectangle in a Histogram(單調)

題意: 給出一組矩形的高,求最多能拼成矩形的最大面積,看圖就很清楚了。 要點: 還是單調棧,現在有點感覺了,單調棧大概就是能求出當前值左右的比它大或小的數的範圍。這題用高度作為單調棧,分別往左右找比當

單調POJ2559

name have integer align set base namespace .org ase Description A histogram is a polygon composed of a sequence of rectangles aligned at

【bzoj3238】[Ahoi2013]差異 後綴數組+單調

其中 int alt sin bsp 大於等於 com 輸出 最小 題目描述 輸入 一行,一個字符串S 輸出 一行,一個整數,表示所求值 樣例輸入 cacao 樣例輸出 54 題解 後綴數組+單調棧,幾乎同 bzoj3879 的後半部分。 我

【BZOJ3190】[JLOI2013]賽車 單調+幾何

scrip 所有 ring 速度 ostream 正整數 一個 包括 題解 【BZOJ3190】[JLOI2013]賽車 Description 這裏有一輛賽車比賽正在進行,賽場上一共有N輛車,分別稱為個g1,g2……gn。賽道是一條無限

POJ 2559 Largest Rectangle in a Histogram(單調

common pst locale str flow bold text function target 【題目鏈接】:click here~~ 【題目大意】: A histogram is a polygon composed of a sequence of

POJ-2559 Largest Rectangle in a Histogram(單調)

comm pict mon include max specified out names align Largest Rectangle in a Histogram Time Limit: 1000MS Memory Limit: 65536K Tota

Codeforces 319B. Psychos in a Line【單調

push_back 找到 cnblogs air inline nod pre type == 題目鏈接: http://codeforces.com/problemset/problem/319/B 題意: 一串數列,每一個值如果大於相鄰右一位的值的話,那麽就可以把右邊這

玲瓏杯”ACM比賽 Round #19 B 維護單調

prev algorithm mon view etc spa queue eva -i 1149 - Buildings Time Limit:2s Memory Limit:128MByte Submissions:588Solved:151

單調 BZOJ2364 城市美化

-h int ring d+ clas mes double bmi des 2364: 城市美化 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 182 Solved: 42[Submit][Status][Discuss

bzoj1057: [ZJOI2007]棋盤制作 [dp][單調]

bsp color -m pro %d out mx2 ons 圍棋 Description   國際象棋是世界上最古老的博弈遊戲之一,和中國的圍棋、象棋以及日本的將棋同享盛名。據說國際象棋起源 於易經的思想,棋盤是一個8*8大小的黑白相間的方陣,對應八八六十四卦,

51nod 1215 數組的寬度&poj 2796 Feel Good(單調

close queue fin click nod stream ++ std tchar   單調棧求每個數在哪些區間是最值的經典操作。   把數一個一個丟進單調棧,彈出的時候[st[top-1]+1,i-1]這段區間就是彈出的數為最值的區間。   poj2796 彈

poj 2059 單調

1.0 sca eas down rec cpp 操作 string typedef 題意:求柱狀圖中最大矩形面積。 單調棧:顧名思義就是棧內元素單調遞增的棧。每次插入數據來維護這個棧,假設當前須要插入的數據小於棧頂的元素,那就一直彈出棧頂的元素。直到

51nod 1279 單調

its 空間 好處 continue blank 最大 ret c++ .com http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1279 1279 扔盤子 題目來源: Codility 基準時間限

51nod 1215 單調/叠代

允許 typedef 端點 修改 min end tput bsp http http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1215 1215 數組的寬度 題目來源: Javaman 基準時間限制

hdu 3410 單調

repl nal printf another telling all clu test mod http://acm.hdu.edu.cn/showproblem.php?pid=3410 Passing the Message Time Limit: 2000/1000