UESTC_秋实大哥去打工 2015 UESTC Training for Data Structures<Problem G>
G - 秋实大哥去打工
Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others)
天行健,君子以自强不息。地势坤,君子以厚德载物。
天天过节的秋实大哥又要过节了,于是他要给心爱的妹子买礼物。但由于最近秋实大哥手头拮据,身为一个男人,他决定去打工!
秋实大哥来到一家广告公司。现在有n块矩形墙从左至右紧密排列,每一块高为Hi,宽为Wi。
公司要求秋实大哥找出一块最大的连续矩形区域,使得公司可以在上面贴出最大的海报。
Input
第一行包含一个整数n,表示矩形墙的个数。
接下来n行,每行有两个整数Wi,Hi,表示第i块墙的宽度和高度。
1≤n≤200000,保证Wi,Hi以及最后的答案<231。
Output
最大的连续矩形的面积。
Sample input and output
| Sample Input | Sample Output |
|---|---|
3 |
14 |
解题报告
关键是找到某个元素所能扩展到的最远位置,我们维护一个单调栈即可,扫描一遍,若该元素大于栈顶元素则入栈,若小于则出栈并更新答案,注意可能一直单调递增,可在最后加一个高度为0的虚矩形,保证扫描结束后所有合法矩形已经全部出栈.
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
typedef long long ll;
using namespace std;
const int maxn = 2e5 + ;
int h[maxn],w[maxn],sum[maxn],s[maxn],pos[maxn]; int main(int argc,char *argv[])
{
int n;
scanf("%d",&n);
for(int i = ; i <= n ; ++ i)
scanf("%d%d",&w[i],&h[i]);
sum[] = ;
h[n+] = ;
for(int i = ; i <= n ; ++ i)
sum[i] = sum[i-] + w[i];
int top = , ans = ;
for(int i = ; i <= n+ ; ++ i)
{
if (top > && h[i] < s[top-])
{
while(top > && h[i] < s[top-])
ans = max(ans , (sum[i-] - sum[pos[--top]-])*s[top]);
s[top++] = h[i];
}
else
{
pos[top] = i;
s[top++] = h[i];
}
}
printf("%d\n",ans);
return ;
}
UESTC_秋实大哥去打工 2015 UESTC Training for Data Structures<Problem G>的更多相关文章
- UESTC_秋实大哥搞算数 2015 UESTC Training for Data Structures<Problem N>
N - 秋实大哥搞算数 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Subm ...
- UESTC_秋实大哥打游戏 2015 UESTC Training for Data Structures<Problem H>
H - 秋实大哥打游戏 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Subm ...
- UESTC_秋实大哥与家 2015 UESTC Training for Data Structures<Problem E>
E - 秋实大哥与家 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Submi ...
- UESTC_秋实大哥与战争 2015 UESTC Training for Data Structures<Problem D>
D - 秋实大哥与战争 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Subm ...
- UESTC_秋实大哥与快餐店 2015 UESTC Training for Data Structures<Problem C>
C - 秋实大哥与快餐店 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Sub ...
- UESTC_秋实大哥与花 2015 UESTC Training for Data Structures<Problem B>
B - 秋实大哥与花 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Submi ...
- UESTC_秋实大哥与小朋友 2015 UESTC Training for Data Structures<Problem A>
A - 秋实大哥与小朋友 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Sub ...
- UESTC_秋实大哥掰手指 2015 UESTC Training for Dynamic Programming<Problem B>
B - 秋实大哥掰手指 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 2048/1024KB (Java/Others) Submit ...
- UESTC_秋实大哥与线段树 2015 UESTC Training for Data Structures<Problem M>
M - 秋实大哥与线段树 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Sub ...
随机推荐
- MVC View返回list列表
); Sql sql2 = ); Sql sql3 = ); Sql sql4 = ); Sql sql ...
- MCM1988 问题B_lingo_装货问题
两辆平板车的装货问题有七种规格的包装箱要装到两辆铁路平板车上去包装箱的宽和高是一样的但厚度(t,以厘米计)及重量(,以公斤计)是不同的.下表给出了每种包装箱的厚度重量以及数量每辆平板车有10.2 米长 ...
- Word Break II 解答
Question Given a string s and a dictionary of words dict, add spaces in s to construct a sentence wh ...
- Java日志终极指南
Java日志基础 Java使用了一种自定义的.可扩展的方法来输出日志.虽然Java通过java.util.logging包提供了一套基本的日志处理API,但你可以很轻松的使用一种或者多种其它日志解决方 ...
- windows10 离线包安装net3.5
找到离线镜像: 管理员命令行运行:dism.exe /online /enable-feature /featurename:netfx3 /Source:E:\sources\sxs 路径根据实际情 ...
- poj 2385 Apple Catching(dp)
Description It and ) in his field, each full of apples. Bessie cannot reach the apples when they are ...
- c++之 常用类型
C/C++常用类型的范围 C/C++里常用的类型及表示范围如下表所示: 类型 sizeof 表示范围 说明 char 1 -128 - 127 -2^7 - (2^7 - 1) short 2 -32 ...
- 用CSS/CSS3 实现 水平居中和垂直居中的完整攻略
水平居中:行内元素解决方案 只需要把行内元素包裹在一个属性display为block的父层元素中,并且把父层元素添加如下属性即可: .parent { text-align:center; } 水 ...
- strut2 自己定义文件上传错误信息
在文件上传过程中我们能够指定拦截器对文件类型.后缀名.大小进行设定,action中的配置: <interceptor-ref name="fileUpload"> &l ...
- [CSS3] CSS Display Property: Block, Inline-Block, and Inline
Understanding the most common CSS display types of block, inline-block, and inline will allow you to ...