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 K (Java/Others)

Total Submission(s): 11706    Accepted Submission(s): 3219

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

Hdu 1506 Largest Rectangle in a Histogram                                                    分类:            Brush Mode             2014-10-28 19:16    93人阅读    评论(0)    收藏

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

#include <iostream>
#include <cmath>
#include <algorithm>
#include <string>
#include <deque>
#include <cstring>
#include <cstdio>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <cstdlib>
#include <iomanip>
using namespace std; #define rep(i, a, b) for( i = (a); i <= (b); i++)
#define reps(i, a, b) for( i = (a); i < (b); i++)
#define pb push_back
#define ps push
#define mp make_pair
#define CLR(x,t) memset(x,t,sizeof x)
#define LEN(X) strlen(X)
#define F first
#define S second
#define Debug(x) cout<<#x<<"="<<x<<endl; const double euler_r = 0.57721566490153286060651209;
const double pi = 3.141592653589793238462643383279;
const double E = 2.7182818284590452353602874713526;
const int inf=~0U>>1;
const int MOD = int(1e9) + 7;
const double EPS=1e-6; typedef long long LL;
__int64 a[1000010], l[1000010], r[1000010];
int main()
{
int t, n, i;
__int64 ans;
while(cin >> n && n)
{
rep(i, 1, n) scanf("%I64d",&a[i]);
a[0] = -1;
rep(i, 1, n)
{
t = i;
while(a[t-1] >= a[i]) t = l[t-1];
l[i] = t;
}
a[n+1] = -1;
for(i = n; i >= 1; i--)
{
t = i;
while(a[t+1] >= a[i]) t = r[t+1];
r[i] = t;
}
ans = 0;
//cout << l[1] << r[1] << endl;
rep(i, 1, n)
{
//cout << i << " " << 1LL * a[i] * (r[i] - l[i] + 1) << endl;
ans = max(ans, 1LL * a[i] * (r[i] - l[i] + 1));
}
printf("%I64d\n",ans);
//cout << ans << endl;
}
return 0;
}
上一篇:[LeetCode] Largest Palindrome Product 最大回文串乘积


下一篇:一个简单易用的视频下载工具 Downie v3.5.1,适配苹果所有 macOS 系统