日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Largest Rectangle in a Histogram (动态规划+奇思妙想单调栈)求最大矩状图面积

發布時間:2023/12/4 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Largest Rectangle in a Histogram (动态规划+奇思妙想单调栈)求最大矩状图面积 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

感覺動態規劃都是玄妙的很,思維題吧(單調棧思維)

題解:讓求最大矩形面積,寬為1,暴力超時

可以發現 ? 當第i-1個比第i個高的時候 ? 比第i-1個高的所有也一定比第i個高 ?

于是可以用到動態規劃的思想

令left[i]表示包括i在內比i高的連續序列中最左邊一個的編號 ??right[i]?為最右邊一個的編號

那么有 ? 當?h[left[i]-1]>=h[i]]時 ? left[i]=left[left[i]-1]??從前往后可以遞推出left[i] ??

同理 ? ? ?當?h[right[i]+1]>=h[i]]時 ? right[i]=right[right[i]+1]?? 從后往前可遞推出righ[i]

最后答案就等于??max((right[i]-left[i]+1)*h[i])?了;

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 integers h1, ..., 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 /*題目題意:題目給了n個矩形的高度,問最大連續矩形的公共面積(底乘以這段連續矩形中 最短的高度),每個矩形的底是1 題目分析:我們可以枚舉每一個矩形,把它當作最矮的矩形,剩下就差知道底了。 既然這個矩形是最矮的的那一個,那么它左邊的矩形和右邊的矩形的高度應該大于等于它!*/ #include<iostream> #include<stdio.h> #include<cstring> #include<cmath> #define ll long long using namespace std; const int maxn=1e5+1000; ll a[maxn],Left[maxn],Right[maxn]; int main() {int n;while (~scanf("%d",&n)&&n){for(int i=1; i<=n; i++)scanf("%lld",&a[i]);Left[1]=1;Right[n]=n;for (int i=2; i<=n; i++) ///求出每個矩形左端非遞減連續的下標{int t=i;while (t>1&&a[i]<=a[t-1])/**狀態轉移*/t=Left[t-1];Left[i]=t;}for (int i=n-1; i>=1; i--) ///求出每個矩形右端非遞減連續的下標{int t=i;while (t<n&&a[i]<=a[t+1])t=Right[t+1];Right[i]=t;}ll ans=0;for (int i=1; i<=n; i++)ans=max(ans,(Right[i]-Left[i]+1)*a[i]);printf("%lld\n",ans);}return 0; }

?

總結

以上是生活随笔為你收集整理的Largest Rectangle in a Histogram (动态规划+奇思妙想单调栈)求最大矩状图面积的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 都市豪门艳霸淫美妇 | 中文在线最新版天堂8 | 超碰偷拍 | 在线免费看毛片 | 日本免费一二区 | 国产精品国产av | 成人av国产 | 亚洲国产精品激情在线观看 | 涩涩视频在线免费看 | www.色在线观看 | 夜夜嗨av一区二区三区免费区 | 99国产在线视频 | 可以直接在线观看的av | 久久精品无码一区二区三区毛片 | 精品一区二区三区蜜臀 | 日本一区二区视频在线播放 | 天天免费视频 | 中文字幕免费视频 | 波多野结衣网站 | 久久久久极品 | 一区二区三区视频在线免费观看 | 日本熟妇乱子伦xxxx | 蜜桃av导航 | 国产成人精品综合在线观看 | 波多野结衣视频免费在线观看 | 成人激情文学 | 天天爽夜夜爽视频 | av观看网 | 成人影视免费 | 男人桶进美女尿囗 | 国产综合区 | 熟妇高潮一区二区高潮 | av午夜激情 | 美女日批在线观看 | 第五色婷婷 | 免费性片 | av青草 | 欧州一级片 | 欧美成人精品三级网站 | 福利精品| 欧美少妇18p | 国产一区在线免费观看 | 狠狠人妻久久久久久综合 | 国产一区二区精彩视频 | 色丁香六月 | 中文字幕在线观看网站 | 日韩中文字幕av | 青青导航 | 天天插天天狠天天透 | 五月综合在线 | 干骚视频 | 齐天大性床战铁扇公主 | 天天视频入口 | 黄色一大片 | 操视频网站 | 黄色网战大全 | 奇米777视频| 在线播放免费av | 午夜影院日本 | 在线亚洲不卡 | 国产h视频在线观看 | 亚洲一区二区三区av无码 | 都市激情亚洲 | 一级一片免费播放 | 国产高清在线观看视频 | 欧美一级视频 | 国产日韩欧美自拍 | 色在线免费| 秋霞久久精品 | 成人观看视频 | 影音先锋久久久久av综合网成人 | 国产欧美视频在线观看 | 欧美有码视频 | 有码视频在线观看 | japanese24hdxxxx日韩 | 青娱乐超碰在线 | 亚洲av无码国产综合专区 | 午夜不卡在线观看 | 成人亚洲玉足脚交系列 | 欧美精产国品一二三区 | 日韩电影一区二区在线观看 | 久久影音先锋 | 欧美日日 | 日本吃奶摸下激烈网站动漫 | 日本一区二区三区四区视频 | www国产91| sese久久 | 嫩草嫩草嫩草嫩草嫩草嫩草 | 中文字幕综合网 | 高清久久久久久 | 不卡一区二区在线视频 | 午夜剧场成人 | 亚洲视频免费在线播放 | 欧美日韩在线国产 | 精品国产乱码久久久久久1区2区 | 黄色一级视频免费 | 天天伊人网 | 亚洲在线播放 | 色多多在线视频 |