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 ...
随机推荐
- 使用systemd将iptables规则在docker启动后自动导入
编写systemd文件 $ sudo vi /etc/systemd/system/iptables-import.service # /etc/systemd/system/iptables-imp ...
- HDC2021技术分论坛:异构组网如何解决共享资源冲突?
作者:lijie,HarmonyOS软总线领域专家 相信大家对HarmonyOS的"超级终端"比较熟悉了.那么,您知道超级终端场景下的多种设备在不同环境下是如何组成一个网络的吗?这 ...
- javaSE中级篇2 — 工具类篇 — 更新完毕
1.工具类(也叫常用类)-- 指的是别人已经写好了的,我们只需要拿来用就行了 官网网址:Overview (Java Platform SE 8 ) (oracle.com) ---- 但是这个是英文 ...
- day06 视图层
day06 视图层 今日内容 视图层 小白必会三板斧 JsonResponse form表单发送文件 FBV与CBV FBV基于函数的视图 CBV基于类的视图 模板层 模板语法的传值 模板语法之过滤器 ...
- 【原创】Altium生成Gerber时跳出The Film is too small for this PCB的解决办法
在用altium Designer画板子的时候,要生成gerber文件的时候,会出错,出现这样的提示框:"The Film is too small for this PCB" 原 ...
- Spring的事务传播机制(通俗易懂)
概述 Spring的事务传播机制有7种,在枚举Propagation中有定义. 1.REQUIRED PROPAGATION_REQUIRED:如果当前没有事务,就创建一个新事务,如果当前存在事务,就 ...
- Templates and Static variables in C++
Function templates and static variables: Each instantiation of function template has its own copy of ...
- 什么是maven(二)
转自博主--一杯凉茶 maven项目构建ssh工程(父工程与子模块的拆分与聚合) 前一节我们明白了maven是个什么玩意,这一节就来讲讲他的一个重要的应用场景,也就是通过maven将一个ssh项目 ...
- windows下安装linux虚拟机(wsl2),并安装docker。
一.windows terminal(重要工具,但也可以不装) 这是微软官方推荐的终端工具,类似mac的iterm2,可同时开启多个终端,最开始默认有power shall,cmd,可下载gsudo集 ...
- TV盒子 android 4.4安装kodi 和 安装jellyfin插件连接到 jellyfin服务器
很多IPTV盒子系统都是android 4.4的,无法安装新版kodi, 最高只能安装到改版的kodi, 版本是17.1,jellyfin插件也只能安装到0.5.8 首先下载kodi和jellyfin ...