hdu Largest Rectangle in a Histogram
这道题目是一道动态规划的题目,动态规划的点不在面积上,而在每个矩形可左右扩展的坐标上。找出每个矩形最左边和最右边比它高的矩形的下标,最后求面积即可;
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include"stdio.h" __int64 h[100010],ans,temp; int l[100010],r[100010]; int main() { int n,i,j; while(scanf("%d",&n),n) { for(i=1;i<=n;i++) { scanf("%I64d",&h[i]); l[i]=r[i]=i; } h[0]=h[n+1]=-1; for(i=1;i<=n;i++) { while(h[l[i]-1]>=h[i]) l[i]=l[l[i]-1]; } for(i=n;i>=1;i--) //注意这个顺序,tle了好几次都是因为用的是i++,i<=n { while(h[r[i]+1]>=h[i]) r[i]=r[r[i]+1]; } ans=0; for(i=1;i<=n;i++) { temp=h[i]*(r[i]-l[i]+1); if(temp>ans) ans=temp; } printf("%I64d\n",ans); } return 0; }