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.
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的更多相关文章
- XJOI 3606 最大子矩形面积/LightOJ 1083 Histogram(单调栈/笛卡尔树)
A histogram is a polygon composed of a sequence of rectangles aligned at a common base line. The rec ...
- lightoj刷题日记
提高自己的实力, 也为了证明, 开始板刷lightoj,每天题量>=1: 题目的类型会在这边说明,具体见分页博客: SUM=54; 1000 Greetings from LightOJ [简单 ...
- Histogram LightOJ - 1083
Histogram LightOJ - 1083 题意:给出一个直方图,由n个长条组成,它们的x轴上坐标分别为1-n,读入n之后读入的一行中,第i个表示x轴上坐标为i的长条长度.求直方图最大的正方形面 ...
- [LeetCode] Largest Rectangle in Histogram 直方图中最大的矩形
Given n non-negative integers representing the histogram's bar height where the width of each bar is ...
- poj 2559 Largest Rectangle in a Histogram - 单调栈
Largest Rectangle in a Histogram Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 19782 ...
- LeetCode 笔记系列 17 Largest Rectangle in Histogram
题目: Largest Rectangle in Histogram Given n non-negative integers representing the histogram's bar he ...
- LeetCode: Largest Rectangle in Histogram(直方图最大面积)
http://blog.csdn.net/abcbc/article/details/8943485 具体的题目描述为: Given n non-negative integers represent ...
- 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 ...
- Largest Rectangle in Histogram
Given n non-negative integers representing the histogram's bar height where the width of each bar is ...
随机推荐
- Oracle完整的压测记录
问题描述:对oracle进行一次完整的数据压测,从制造数据到压测的过程,路上踩了一些坑,现在分享出来 1.下载swingbenh软件,一个比较好用的oracle压测软件 2.利用oewizard工具( ...
- 腾讯云联合中国信通院&作业帮等首发《降本之源-云原生成本管理白皮书》
在11月4日举办的2021腾讯数字生态大会云原生专场上,腾讯云联合中国信通院.作业帮等率先在国内重磅发布了<降本之源-云原生成本管理白皮书>(简称白皮书),基于腾讯云在业内最大规模的 Ku ...
- 日常Java 2021/10/19
Java集合框架 Java 集合框架主要包括两种类型的容器,一种是集合(Collection),存储一个元素集合,另一种是图(Map),存储键/值对映射. Collection接口又有3种子类型,Li ...
- The Go tools for Windows + Assembler很好玩
我想用python做个tiny BASIC编译器.赋值和加减乘除,IF和FOR. 语法分析python有ply包,用ply.lex和ply.yacc做个计算器很简单,我已经做了. 做个解释器应该也不难 ...
- day13 装饰器与语法糖
day13 装饰器与语法糖 一.装饰器 1.什么是装饰器 装饰器就是装饰别人的工具,具体是指为被装饰者添加新功能 装饰器->函数 被装饰者->函数 2.为何要用装饰器 装饰器的核心思想:( ...
- Running shell commands by C++
#include <iostream> #include <stdio.h> #include <stdlib.h> using namespace std; st ...
- Ubantu nodejs卸载与二进制安装
#apt-get 卸载 sudo apt-get remove --purge npm sudo apt-get remove --purge nodejs sudo apt-get remove - ...
- 转 onSaveInstanceState()和onRestoreInstanceState()使用详解
转 https://www.jianshu.com/p/27181e2e32d2 背景 如果系统由于系统约束(而不是正常的应用程序行为)而破坏了Activity,那么尽管实际 Activity实例已经 ...
- Mysql不锁表备份之Xtrabackup的备份与恢复
一.Xtrabackup介绍 MySQL冷备.热备.mysqldump都无法实现对数据库进行增量备份.如果数据量较大我们每天进行完整备份不仅耗时且影响性能.而Percona-Xtrabackup就是为 ...
- SpringMVC(1):SpringMVC入门
一,MVC 概述 MVC:模型,视图,控制器,是一种软件设计规范,本质是将业务逻辑,数据,显示,分离的方式来编写代码:前后端分离 Model:数据模型,提供要展示的数据,一般我们都会把这两个分离开来. ...