1083 - Histogram
Time Limit: 2 second(s) Memory Limit: 64 MB

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 shows the histogram that consists of rectangles with the heights 2, 1, 4, 5, 1, 3, 3 measured in units where the width of the rectangles is 1.

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

Input starts with an integer T (≤ 20), denoting the number of test cases.

Each case contains a line with an integer N (1 ≤ N ≤ 30000) denoting the number of rectangles. The next line contains N space separated positive integers (≤ 30000) denoting the heights.

Output

For each case, print the case number and the largest rectangle that can be made.

Sample Input

Output for Sample Input

2

7

2 1 4 5 1 3 3

5

4 4 3 2 4

Case 1: 8

Case 2: 10

Note

Dataset is huge; use faster I/O methods.


Special Thanks: Jane Alam Jan (Description, Solution, Dataset)
思路:维护一个单调棧;
 1 #include<stdio.h>
2 #include<algorithm>
3 #include<iostream>
4 #include<string.h>
5 #include<stdlib.h>
6 #include<queue>
7 #include<stack>
8 using namespace std;
9 typedef long long LL;
10 typedef struct pp
11 {
12 int x;
13 int id;
14 } ss;
15 ss ans[400000];
16 stack<ss>sta;
17 int main(void)
18 {
19 int i,j,k;
20 int s;
21 int n;
22 scanf("%d",&k);
23 for(s=1; s<=k; s++)
24 {
25 while(!sta.empty())
26 sta.pop();
27 scanf("%d",&n);
28 for(i=1; i<=n; i++)
29 {
30 scanf("%d",&ans[i].x);
31 ans[i].id=i;
32 }
33 ans[n+1].x=0;
34 ans[n+1].id=n+1;
35 LL maxx=ans[1].x;
36 sta.push(ans[1]);
37 for(i=2; i<=n+1; i++)
38 {
39 while(!sta.empty())
40 {
41 ss mm=sta.top();
42 if(mm.x>ans[i].x)
43 {
44 sta.pop();
45 LL ak=(LL)mm.x*(LL)(i-mm.id);
46 maxx=max(ak,maxx);
47 ans[i].id=mm.id;
48 }
49 else break;
50 }
51 sta.push(ans[i]);
52 }
53 printf("Case %d: %lld\n",s,maxx);
54 }
55 return 0;
56 }

1083 - Histogram的更多相关文章

  1. XJOI 3606 最大子矩形面积/LightOJ 1083 Histogram(单调栈/笛卡尔树)

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

  2. lightoj刷题日记

    提高自己的实力, 也为了证明, 开始板刷lightoj,每天题量>=1: 题目的类型会在这边说明,具体见分页博客: SUM=54; 1000 Greetings from LightOJ [简单 ...

  3. Histogram LightOJ - 1083

    Histogram LightOJ - 1083 题意:给出一个直方图,由n个长条组成,它们的x轴上坐标分别为1-n,读入n之后读入的一行中,第i个表示x轴上坐标为i的长条长度.求直方图最大的正方形面 ...

  4. [LeetCode] Largest Rectangle in Histogram 直方图中最大的矩形

    Given n non-negative integers representing the histogram's bar height where the width of each bar is ...

  5. poj 2559 Largest Rectangle in a Histogram - 单调栈

    Largest Rectangle in a Histogram Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 19782 ...

  6. LeetCode 笔记系列 17 Largest Rectangle in Histogram

    题目: Largest Rectangle in Histogram Given n non-negative integers representing the histogram's bar he ...

  7. LeetCode: Largest Rectangle in Histogram(直方图最大面积)

    http://blog.csdn.net/abcbc/article/details/8943485 具体的题目描述为: Given n non-negative integers represent ...

  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. Largest Rectangle in Histogram

    Given n non-negative integers representing the histogram's bar height where the width of each bar is ...

随机推荐

  1. X-MagicBox-820的luatOS之路连载系列6

    继上次用Qt实现了显示地图和MQTT通信之后(X-MagicBox-820的luatOS之路连载系列5),说是要研究下地图的开放接口,也看了标记点和线的方法(地图上自定义标记点和轨迹线的实现).这次就 ...

  2. 第三个基础框架 — springMVC — 更新完毕

    1.什么是springMVC? 还是老规矩,百度百科一下 这里面说了一堆废话,去官网瞄一下 官网网址:https://docs.spring.io/spring-framework/docs/curr ...

  3. pyqt5 的串口编写进度

    2020.12.18 今天遇到一个问题, 想用回车实现串口数据的发送. 下面这句话是让光标移动到文字的尾部,但是不能够实现. 对QTextEdit控件中的文字改写,或清除后,再调用下面的移动到尾部,就 ...

  4. Oracle—全局变量

    Oracle全局变量 一.数据库程序包全局变量       在程序实现过程中,经常用遇到一些全局变量或常数.在程序开发过程中,往往会将该变量或常数存储于临时表或前台程序的全局变量中,由此带来运行效率降 ...

  5. HDFS初探之旅(一)

    1.HDFS简介                                                                                            ...

  6. OS开发之Objective-C与JavaScript的交互

    UIWebView是iOS最常用的SDK之一,它有一个stringByEvaluatingJavaScriptFromString方法可以将javascript嵌入页面中,通过这个方法我们可以在iOS ...

  7. OC-引用计数器,内存管理,野指针

    总结 全局断点 →-->+-->Add Exception Breakpoint 开启僵尸监听 打开Edit scheme -->Diagnostics-->Enable Zo ...

  8. 【Java】面向类与对象

    一.面向对象 对象封装:私有变量+公共方法 方法与构造方法 this变量 Animal.java public class Animal { String name; int age; void mo ...

  9. 10、Redis三种特殊的数据类型

    一.Geospatail地理位置 1.Geospatail的应用 朋友的位置,附近的人,打车距离 2.相关命令 1.geoadd:增加某个地理位置的坐标(可批量添加). 语法: GEOADD key ...

  10. JS 中常用的去重

    第一种:indexOf (获取字符串值在字符串中首次出现的位置,若没有这个值,则返回-1) let arr = [15,45,88,45,78,15,55,88]; let arr1 = []; // ...