题目大意:
在一张直方图(每天柱子的宽都是1)中找出最大面积矩形并输出其面积。
有多个测例,每个测例都会给出该测例中唯一一个直方图中柱子的数量n(n ≤ 100,000),并依次(按照直方图中从左往右的顺序)给出每根柱子的高度hi(hi ≤ 1,000,000,000),对于每个测例都需要输出最大矩形的面积,以n等于0结束测例退出程序。
!!!和POJ 2082类似
C语言实现栈:
注释代码:
/*
* Problem ID : POJ 2559 Largest Rectangle in a Histogram
* Author : Lirx.t.Una
* Language : C++
* Run Time : 141 ms
* Run Memory : 332 KB
*/
#include <stdio.h>
//矩形的最大数量(矩形都存入栈中)
#define MAXSTACKSIZE 100000
//由于高度可以到达十位数(和int的最大值同一个级别)
//因此计算面积时使用int可能会越界
//所以使用8位的long long
typedef long long dlong;
typedef struct {
int w;
int h;//高度最大为10,000没超过int的最大限度
} Rec;
Rec stk[MAXSTACKSIZE];
int
main() {
int n;
int i;
int w_tmp;
dlong s_tmp;
dlong ans;
int top;
Rec rec;
while ( scanf("%d", &n), n ) {
ans = 0;
top = -1;
for ( i = 1; i <= n; i++ ) {
rec.w = 1;
scanf("%d", &rec.h);
w_tmp = 0;
while ( top > -1 && rec.h <= stk[top].h ) {
w_tmp += stk[top].w;
s_tmp = (dlong)w_tmp * (dlong)stk[top].h;
if ( s_tmp > ans )
ans = s_tmp;
top--;
}
top++;
stk[top].w = rec.w + w_tmp;
stk[top].h = rec.h;
}
w_tmp = 0;
while ( top > -1 ) {
w_tmp += stk[top].w;
s_tmp = (dlong)w_tmp * (dlong)stk[top].h;
if ( s_tmp > ans )
ans = s_tmp;
top--;
}
printf("%lld\n", ans);//注意输出时使用的格式是lld
}
return 0;
}
无注释代码:
#include <stdio.h>
#define MAXSTACKSIZE 100000
typedef long long dlong;
typedef struct {
int w;
int h;
} Rec;
Rec stk[MAXSTACKSIZE];
int
main() {
int n;
int i;
int w_tmp;
dlong s_tmp;
dlong ans;
int top;
Rec rec;
while ( scanf("%d", &n), n ) {
ans = 0;
top = -1;
for ( i = 1; i <= n; i++ ) {
rec.w = 1;
scanf("%d", &rec.h);
w_tmp = 0;
while ( top > -1 && rec.h <= stk[top].h ) {
w_tmp += stk[top].w;
s_tmp = (dlong)w_tmp * (dlong)stk[top].h;
if ( s_tmp > ans )
ans = s_tmp;
top--;
}
top++;
stk[top].w = rec.w + w_tmp;
stk[top].h = rec.h;
}
w_tmp = 0;
while ( top > -1 ) {
w_tmp += stk[top].w;
s_tmp = (dlong)w_tmp * (dlong)stk[top].h;
if ( s_tmp > ans )
ans = s_tmp;
top--;
}
printf("%lld\n", ans);
}
return 0;
}
STL stack:
/*
* Problem ID : POJ 2559 Largest Rectangle in a Histogram
* Author : Lirx.t.Una
* Language : G++
* Run Time : 172 ms
* Run Memory : 860 KB
*/
#pragma G++ optimize("O2")
#include <iostream>
#include <cstdio>
#include <stack>
using namespace std;
typedef long long dlong;
struct Rec {
int w;
int h;
};
int
main() {
int n;
int i;
int w_tmp;
dlong s_tmp;
dlong ans;
Rec rec;
stack<Rec> stk;
while ( scanf("%d", &n), n ) {
ans = 0;
for ( i = 1; i <= n; i++ ) {
scanf("%d", &rec.h);
rec.w = 1;
w_tmp = 0;
while ( !stk.empty() && rec.h <= stk.top().h ) {
w_tmp += stk.top().w;
s_tmp = (dlong)w_tmp * (dlong)stk.top().h;
if ( s_tmp > ans )
ans = s_tmp;
stk.pop();
}
rec.w += w_tmp;
stk.push(rec);
}
w_tmp = 0;
while ( !stk.empty() ) {
w_tmp += stk.top().w;
s_tmp = (dlong)w_tmp * (dlong)stk.top().h;
if ( s_tmp > ans )
ans = s_tmp;
stk.pop();
}
printf("%lld\n", ans);
}
return 0;
}
单词解释:
histogram:n, 直方图
polygon:n, 多边形
be composed of:...由...组成
align:vt, 联盟,连成一行
discrete:adj, 离散的
distribution:n, 分布,分配
discrete distribution:n, 离散分布
e.g.:for example(发音也如此),举例来说
frequency:n, 频率
Note that:注意到....(句首)
dipict:vt, 描述,描绘
denote:vt, 表示