poj2082----Terrible Sets(处女座单调栈求区间问题)

来源:互联网 发布:php用echo输出表格 编辑:程序博客网 时间:2024/05/17 06:25

题意:处女座的出题人!!!太禽兽的,题意简单的一道题给他出成这样。

给你几个紧贴着的矩形,高度宽度各不相同,但是边上是紧贴着的,底边在同一水平线上,于是可以看成是一个有凹有凸的图形,问在这个图形里找一个面积最大的矩形。

思路:单调栈求区间,做这提前最好做一下poj2796。

#include<iostream>#include<string>#define M 50050using namespace std;struct stack{int top,h[M];int  w[M];        };stack s;int main(){int n,i,j,k,max,width,height;while(scanf("%d",&n),n!=-1){s.top=max=0;for(i=0;i<n;i++){scanf("%d%d",&width,&height);k=0;while(s.top!=0&&s.h[s.top]>=height)           //如果栈顶比它的高度高,则先出栈再入栈{k+=s.w[s.top];if(k*s.h[s.top]>max) max=k*s.h[s.top];s.top--;}s.h[++s.top]=height;s.w[s.top]=k+width;}k=0;while(s.top!=0)            //把剩余的出栈并计算面积{k+=s.w[s.top];if(k*s.h[s.top]>max) max=k*s.h[s.top];s.top--;}printf("%d\n",max);}return 0;}


 

0 0