HDU 1506 & 1505 - Largest Rectangle in a Histogram & City Game
Largest Rectangle in a Histogram
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 17635 Accepted Submission(s): 5261
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.
Input
The 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.
Output
For 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
对于a[i],我们需要记录的是他之前和之后最大的连续的值比a[i]大的长度,如果每次都一个个去比对,数据大小是100000,时间复杂度是O(n^2),超时那是必然的。但是其实对于a[i]来说,如果去求后面连续的值,完全没必要一个个去比对,直接看a[i-1]的值就行了。 比如说2、3、4、5这个序列,如果我们要看3往后能延伸多长,并不需要去逐个和4和5比较,在计算4的时候,我们已经计算过5是比4大的,因为3比4小,4所能往后延伸的长度,3也一定能达到(4能延伸的长度内的数据都大于等于4,当然也都比3大),我们可以直接比较在4达到的最终长度的末端之后的值。 这道题计算的时候进制转换也需要特别注意,如果temp没有强制转换成__int64位,提交会wa。 这道题优化的思路非常巧妙,很值得学习。
参照这个思路,用dp求h[i]的左右边界,
先初始化h[i]的left[i]和right[i]都是i
对于第i个数h[i],假设它右边的所有数字的right[]都已经得到,那么比较h[i]和h[ right[i]+1 ]的大小,如果h[i] <= h[ right[i]+1 ],那么把right[i]变成right[i]+1的高度,再循环前面的步骤即可,直到right[i]+1到达整个h数组的边界即停止。
类似的可得到left[i]。
#include<cstdio>
int main()
{
int n,h[],left[],right[];
while(){
scanf("%d",&n);
if(n<=) break;
for(int i=;i<=n;i++){
scanf("%d",&h[i]);
right[i]=left[i]=i;
}
for(int i=n-;i>=;i--){
while(right[i]+<=n && h[i]<=h[right[i]+]) right[i]=right[right[i]+];
}
for(int i=;i<=n;i++){
while(left[i]->= && h[i]<=h[left[i]-]) left[i]=left[left[i]-];
}
unsigned long long ans=;
for(int i=;i<=n;i++)
{
unsigned long long temp=(unsigned long long)(right[i]-left[i]+) * h[i];
if(temp>ans) ans=temp;
}
printf("%llu\n",ans);
}
return ;
}
City Game
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6835 Accepted Submission(s): 2956
The strategic task of his game is to win as much rent money from these free spaces. To win rent money you must erect buildings, that can only be rectangular, as long and wide as you can. Bob is trying to find a way to build the biggest possible building in
each area. But he comes across some problems – he is not allowed to destroy already existing buildings, trees, factories and streets in the area he is building in.
Each area has its width and length. The area is divided into a grid of equal square units.The rent paid for each unit on which you're building stands is 3$.
Your task is to help Bob solve this problem. The whole city is divided into K areas. Each one of the areas is rectangular and has a different grid size with its own length M and width N.The existing occupied units are marked with the symbol R. The unoccupied
units are marked with the symbol F.
by a blank space. The next M lines contain N symbols that mark the reserved or free grid units,separated by a blank space. The symbols used are:
R – reserved unit
F – free unit
In the end of each area description there is a separating line.
0
上面一题的进阶版,懒了……不想写思路了……
#include<cstdio>
#include<iostream>
using namespace std;
int deep[][];
int l[][];
int r[][];
int main()
{
int t;
scanf("%d",&t);
while(t--){
int m,n;char temp;
scanf("%d%d",&m,&n);
for(int j=;j<=n;j++) deep[][j]=;
for(int i=;i<=m;i++){
for(int j=;j<=n;j++){ cin>>temp;
if(temp=='F')
{
deep[i][j]=deep[i-][j]+;
}
else if(temp=='R')
{
deep[i][j]=;
} l[i][j]=j;r[i][j]=j;
}
} for(int i=;i<=m;i++){
for(int j=n-;j>=;j--){
while( r[i][j]+<=n && deep[i][j]<=deep[i][ (r[i][j]+) ] ) r[i][j]=r[i][ (r[i][j]+) ];
}
for(int j=;j<=n;j++){
while( l[i][j]->= && deep[i][j]<=deep[i][ (l[i][j]-) ] ) l[i][j]=l[i][ (l[i][j]-) ];
}
} int ans=;
for(int i=;i<=m;i++){
for(int j=;j<=n;j++){
int temp=deep[i][j]*(r[i][j]-l[i][j]+);
if(ans<temp) ans=temp;
}
}
printf("%d\n",ans*);
}
}
HDU 1506 & 1505 - Largest Rectangle in a Histogram & City Game的更多相关文章
- HDU 1505 Largest Rectangle in a Histogram && HDU 1506 City Game(动态规划)
1506意甲冠军:给你一个连续的直方图(拼贴底部长度1).求连续基质区. 对每一个直方图,分别向左向右进行扩展. #include<cstdio> #include<stdlib.h ...
- 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 ...
- HDU 1506 Largest Rectangle in a Histogram (dp左右处理边界的矩形问题)
E - Largest Rectangle in a Histogram Time Limit:1000MS Memory Limit:32768KB 64bit IO Format: ...
- HDU 1506 Largest Rectangle in a Histogram set+二分
Largest Rectangle in a Histogram Problem Description: A histogram is a polygon composed of a sequenc ...
- 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 ...
- HDU 1506 Largest Rectangle in a Histogram(区间DP)
题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=1506 题目: Largest Rectangle in a Histogram Time Limit: ...
- Day8 - C - 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 ...
- 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 ...
- 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 ...
随机推荐
- 让 Python 更加充分的使用 Sqlite3
我最近在涉及大量数据处理的项目中频繁使用 sqlite3.我最初的尝试根本不涉及任何数据库,所有的数据都将保存在内存中,包括字典查找.迭代和条件等查询.这很好,但可以放入内存的只有那么多,并且将数据从 ...
- 【C#】详解C#异常
目录结构: contents structure [+] 异常处理机制 try块 catch块 finally块 自定义异常 CLS异常和非CLS异常 在这篇文章中,笔者会阐述C#中的异常.C#是一门 ...
- git 命令常用总结
详细git教程可参考:http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000 基础命令 用 ...
- pandas DataFrame(1)
之前介绍了numpy的二维数组,但是numpy二维数组有一些局限性,比如,它数组里所有的值的类型必须相同,不能某一列是数值型,某一列是字符串型,这样会导致无法使用 mean() , std() 等方法 ...
- PSR PHP业界规范
0x0 大型项目的问题 随着项目越来越大,参与的人数越来越多,代码变得越来越不可维护了. 每个人都给项目带来自己的风格,所以这时就需要大家采用一个统一的标准. 0x1 解决办法 于是顶尖的PHPer们 ...
- Java多线程系列——过期的suspend()挂起、resume()继续执行线程
简述 这两个操作就好比播放器的暂停和恢复. 但这两个 API 是过期的,也就是不建议使用的. 不推荐使用 suspend() 去挂起线程的原因,是因为 suspend() 在导致线程暂停的同时,并不会 ...
- SQL 逗号分隔将一行拆成多行
and number<=len(a.KOrderID) and type=)=',')
- Java知多少(81)框架窗口基础
窗口是GUI编程的基础,小应用程序或图形界面的应用程序的可视组件都放在窗口中,在GUI中,窗口是用户屏幕的一部分,起着在屏幕中一个小屏幕的作用.有以下三种窗口: Applet窗口:Applet类管理这 ...
- C# 内存理论与实践
The C# Memory Model in Theory and Practice Best Practices All code you write should rely only on the ...
- C# 验证给定的字符串形式的日期是否合法
用于验证日期的有效性,对于用户输入的不规则日期也作了简单处理,比如用户输入了“今天”,则代码会认为用户要返回的是今天的日期,另外可以对纯数字的日期进行解析,比如:20130906 /// <su ...