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 | ||||||
"尚学堂杯"哈尔滨理工大学第七届程序设计竞赛 |
题意是啥: 。。。。。不解释,,自己有道去。。
题解是啥:。。。。不解释单调栈和上个差不多
数据小可以暴力:
#include<stdio.h>
#include<string.h>
using namespace std;
int a[190003];
int main()
{
int n;
while(~scanf("%d",&n))
{
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
int ans=-0x1f1f1f1f;
for(int i=0;i<n;i++)
{
int x=0,y=0;
for(int j=i-1;j>=0;j--)
{
if(a[j]<a[i])
break;
x++;
}
for(int j=i+1;j<n;j++)
{
if(a[j]<a[i])
break;
y++;
}
y++;
ans=ans>((x+y)*a[i])?ans:((x+y)*a[i]);
}
printf("%d\n",ans);
}
}
单调栈 :
#include<bits/stdc++.h>
using namespace std;
int a;
int main()
{
int n;
while(~scanf("%d",&n))
{
stack<pair<int ,int >>s;
int res=0;
for(int i = 0; i < n ; i++)
{
scanf("%d",&a);
int w=0;
while(!s.empty()&&a <= s.top().first)
{
int mh=s.top().first;
int mw=s.top().second;
s.pop();
w+=mw;
res=max(res,mh*w);
}
s.push(make_pair(a,w+1));
}
int tmp = 0;
while(!s.empty())
{
res=max(res,s.top().first*(tmp+s.top().second));
tmp+=s.top().second;
s.pop();
}
printf("%d\n",res);
}
}