Blind Father | ||||||
| ||||||
Description | ||||||
Mr. Leng, who is the predominately inheritor of buried-love family (One of the most vogue families during your primary school maybe, anyway, this is unimportant), has such cool, cooler and coolest temperament. But, only a good ACMer can be the real leader, in other words, be admitted as a successor. One day, a problem come to Mr. Leng 's father about carpentry. There are N pieces of planks upright the ground in one line. Each one width 1 and height uncertain. His father wants to cut some of them and then get a rectangle. How the biggest rectangle does him can make? It too difficult to his father to solve the problem, but it is really easy for Mr. Leng. So do you. Please surmount the problem as soon as you can. Ps: You can't move or change the posture or position of any planks. | ||||||
Input | ||||||
There are multiple cases. In each cases, the first line only contains an integer N, means the number of the planks. And the second line contains N numbers, means the height of N planks one by one.
1<=N<=10000
| ||||||
Output | ||||||
Please output the biggest rectangle that Mr. Leng 's father can get. | ||||||
Sample Input | ||||||
3 10 6 10 | ||||||
Sample Output | ||||||
18 | ||||||
Hint | ||||||
样例图形 | ||||||
Source | ||||||
"尚学堂杯"哈尔滨理工大学第七届程序设计竞赛 |
题意:有n个宽为1的矩形并排放置,求其最大子矩形面积。
思路:枚举每一个小矩形,向两边找第一个比它短的矩形,复杂度是O(n^2),但这题数据较水,也是可以过的。
PS:正解貌似使用单调栈做。。
#include<bits/stdc++.h>
using namespace std;
const int N = 10000 + 10 ;
int main()
{
int n;
int a[N];
while(scanf("%d",&n)==1)
{
int maxi=-1;
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
for(int i=1,j,k;i<=n;i++)
{
for(j=i;j<=n;j++)
if(a[j]<a[i])
break;
for(k=i;k>0;k--)
if(a[k]<a[i])
break;
//printf("%d %d\n",j,k);
maxi=max(maxi,(j-k-1)*a[i]);
}
printf("%d\n",maxi);
}
}