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.

InputThe 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.OutputFor 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 思路:单调队列板子题,注意在单调性出队的时候统计长度,将新入队的长度变长 提供一组数据 7 4 5 6 2 3 3 3 (14不是12
typedef long long LL;
typedef pair<LL, LL> PLL; const int maxm = 1e5+; int buf[maxm], q[maxm], len[maxm]; int main() {
ios::sync_with_stdio(false), cin.tie();
int n;
while(cin >> n && n) {
LL ans = ;
for(int i = ; i <= n; ++i) {
cin >> buf[i];
len[i] = ;
}
buf[n+] = ;
int l = , r = -, cnt;
bool flag = false;
for(int i = ; i <= n+; ++i) {
cnt = ; flag = false;
while(l <= r && buf[q[r]] > buf[i]) {
flag = true;
cnt += len[q[r]];
ans = max(ans, 1LL*cnt*buf[q[r]]);
r--;
}
q[++r] = i;
if(flag) len[i] = cnt+;
}
cout << ans << "\n";
} return ;
}

也可以用笛卡尔树来做,注意这里并不是严格的笛卡尔树,因为是有相同权值的节点的,一个点的长度就是他后裔的个数+自身(1),搜索时使用记忆化,不然会T

typedef long long LL;
typedef pair<LL, LL> PLL; const int maxm = 1e5+; int buf[maxm], q[maxm], Left[maxm], Right[maxm], length[maxm]; int getlen(int u) {
if(length[u]) return length[u];
int len = ;
if(Left[u]) len += getlen(Left[u]);
if(Right[u]) len += getlen(Right[u]);
return length[u] = len;
} int main() {
ios::sync_with_stdio(false), cin.tie();
int n;
while(cin >> n && n) {
LL ans = ;
for(int i = ; i <= n; ++i) Left[i] = Right[i] = length[i] = ;
for(int i = ; i <= n; ++i)
cin >> buf[i];
int top = ;
for(int i = ; i <= n; ++i) {
while(top && buf[q[top]] > buf[i]) {
Left[i] = q[top];
top--;
}
if(top)
Right[q[top]] = i;
q[++top] = i;
}
for(int i = ; i <= n; ++i) {
int len = ;
len = getlen(i);
ans = max(ans, 1LL*len*buf[i]);
}
cout << ans << "\n";
} return ;
}

好像dp也可以,兄弟题1505好像需要,补了再来

Day8 - C - Largest Rectangle in a Histogram HDU - 1506的更多相关文章

  1. Largest Rectangle in a Histogram HDU - 1506 (单调栈)

    A histogram is a polygon composed of a sequence of rectangles aligned at a common base line. The rec ...

  2. V - Largest Rectangle in a Histogram HDU - 1506

    两种思路: 1 单调栈:维护一个单调非递减栈,当栈为空或者当前元素大于等于栈顶元素时就入栈,当前元素小于栈顶元素时就出栈,出栈的同时计算当前值,当前值所包含的区间范围为从当前栈顶元素到当前元素i的距离 ...

  3. HDU 1506 Largest Rectangle in a Histogram (dp左右处理边界的矩形问题)

    E - Largest Rectangle in a Histogram Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format: ...

  4. HDU 1506 Largest Rectangle in a Histogram set+二分

    Largest Rectangle in a Histogram Problem Description: A histogram is a polygon composed of a sequenc ...

  5. hdu 1506 Largest Rectangle in a Histogram 构造

    题目链接:HDU - 1506 A histogram is a polygon composed of a sequence of rectangles aligned at a common ba ...

  6. HDU——T 1506 Largest Rectangle in a Histogram|| POJ——T 2559 Largest Rectangle in a Histogram

    http://acm.hdu.edu.cn/showproblem.php?pid=1506  || http://poj.org/problem?id=2559 Time Limit: 2000/1 ...

  7. HDU 1506 Largest Rectangle in a Histogram(区间DP)

    题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=1506 题目: Largest Rectangle in a Histogram Time Limit: ...

  8. DP专题训练之HDU 1506 Largest Rectangle in a Histogram

    Description A histogram is a polygon composed of a sequence of rectangles aligned at a common base l ...

  9. Hdu 1506 Largest Rectangle in a Histogram 分类: Brush Mode 2014-10-28 19:16 93人阅读 评论(0) 收藏

    Largest Rectangle in a Histogram Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 ...

随机推荐

  1. 2019牛客暑期多校训练营(第十场) Han Xin and His Troop (高精度+拓展中国剩余定理)

    题意 裸题 思路 题中的模数之间并不互质,所以应该用拓展中国剩余定理. 但是交上去会炸,__int128过不了,所以用高精度的板子或者java大数都挺好过的. 这里推荐java大数,因为高精度板子用起 ...

  2. 九、Appium-python-UI自动化之通过text定位

    1.通过xpath定位text xpath路径为://android.widget.EditText[@text='请输入包含街道的完整地址'] 2.通过AndroidUIAutomator # 这个 ...

  3. 一个支持国密SM2/SM3/SM4/SM9/ZUC/SSL的密码工具箱

    转:https://blog.csdn.net/xuq09/article/details/91815366 The GmSSL Project网址:http://gmssl.org/docs/qui ...

  4. ES-9200端口与9300端口

    (1)Elasticsearch是基于lucene的全文检索服务器 (1)9300:ES节点之间的通讯使用 (2)9200:ES节点和外部通讯使用

  5. WPS/office使用技巧系列

    一 WPS中如果要对比2个文档,不想以标签形式打开而是以多窗口形式,该怎么操作? 点击WPS左上角的蓝色或绿色框-->选项->->视图->勾选在任务栏中显示所有窗口,恢复书签模 ...

  6. 树莓派4B踩坑指南 - (7)root账户及权限设置

    树莓派启用root账户 树莓派默认用户是pi 密码为raspberry. root账户默认无密码,但是账户锁定. 开启root账户命令: sudo passwd root #执行后提示设置密码,输入2 ...

  7. 科幻电影免费百度云分享(Scince-fiction cloud share)

    Marvel episode Link  Passcode:6h9k Star War full episode Link Passcode:7abk Men In Black Episode Col ...

  8. Python 之并发编程之协程

    一.协程 ''' def gen(): for i in range(10): yield i # 初始化生成七函数 返回生成器对象,简称生成器 mygen = gen() for i in myge ...

  9. 3分钟让你的Eclipse拥有自动代码提示功能

    第一步:Window->Preferences->Java 第二步:Java->Editor->Content Assist->Auto Activation->将 ...

  10. python nohup linux 后台运行输出

    遇到问题 nohup python flush.py & 这样运行,生成了nohup.out文件,但是内容始终是空的,试了半天也不行.浪费了不少时间.原因 python的输出又缓冲,导致out ...