日韩性视频-久久久蜜桃-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 (动态规划+奇思妙想单调栈)求最大矩状图面积的全部內容,希望文章能夠幫你解決所遇到的問題。

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

主站蜘蛛池模板: 一吻定情2013日剧 | 国精品无码一区二区三区 | 成人av高清在线 | 亚洲小视频网站 | 亚洲精品视频观看 | 毛片在哪里看 | 九九热最新 | 特黄特黄视频 | 成人h在线 | 性自由色xxxx免费视频 | 能在线观看的av网站 | 18禁裸男晨勃露j毛免费观看 | 少妇高潮av久久久久久 | 蜜桃视频日韩 | 日日干夜夜骑 | www.日本免费 | 日韩理论在线 | 日韩精品久 | 亚洲欧美日本一区二区 | av久久久| 亚洲最大福利视频 | 一级黄色录像大片 | 久久精品大片 | 伊人啪啪 | 国产一级影院 | 91香蕉嫩草 | 日韩欧美中文字幕一区二区三区 | 超碰在线免费 | 毛片无遮挡高清免费观看 | 999久久久精品视频 亚洲视频精品在线 | 成人性生交大片免费 | av爽妇网| 三级国产视频 | 国产精品久久久久久久久免费桃花 | 日本一区二区三区在线免费观看 | 免费中文字幕在线观看 | 成人av高清在线观看 | 欧美一区亚洲 | 里番acg★同人里番本子大全 | 日本黄在线 | 一级美女黄色片 | 成人涩涩网 | 日韩一区二区免费视频 | 中文字幕亚洲第一 | 在线永久看片免费的视频 | 国内av网 | 99精品久久久久久久婷婷 | 美女露出粉嫩尿囗让男人桶 | 冲田杏梨一区二区三区 | 九色91丨porny丨丝袜 | 四虎黄色 | 男女插鸡视频 | 伊人久久中文 | 欧美综合国产 | 成人午夜精品福利免费 | 欧洲在线观看 | 国产极品美女高潮无套嗷嗷叫酒店 | 日韩高清片 | 亚洲污污视频 | 91传媒在线免费观看 | 成人精品免费视频 | 久久精品无码av | 午夜理伦三级做爰电影 | 色综合啪啪 | 亚洲精品乱码久久久久久蜜桃91 | 国产人伦精品一区二区三区 | 无码免费一区二区三区免费播放 | 久天堂| 午夜亚洲精品 | 欧美久久久久 | 美国三级a三级18 | 老司机午夜免费精品视频 | 一本色道久久88综合日韩精品 | 99热这| 影音先锋在线视频观看 | 国产精品一区二区三区四区五区 | 巨胸喷奶水www久久久免费动漫 | 欧美日韩亚洲国产综合 | 织田真子作品 | 五月激情在线观看 | 欧美国产第一页 | 欧美少妇一级片 | 日韩网| 亚欧综合在线 | 人成网站在线观看 | 伊人www22综合色 | 亚洲一区二区三区久久 | 亚洲在线影院 | 男人的天堂色偷偷 | 亚洲1024| 日本美女视频网站 | 久久久精品国产sm调教网站 | 91看片视频 | 日韩片在线| 亚洲AV成人精品 | 亚洲国产免费视频 | 911亚洲精选 | 欧美精品一区二区三区四区 | 91成人在线看 |