poj2082单调栈

来源:互联网 发布:5g网络的股票 编辑:程序博客网 时间:2024/06/01 14:25

和我的上一篇博客(poj2559单调栈)基本做法一致,就不多说了

#include<stdio.h>#include<stack>using namespace std;struct sets{    int high,wide;}data;int main(){    int n;    while(scanf("%d",&n),n!=-1)    {        stack<sets>A;        int totalwide=0,tempans=0;        scanf("%d%d",&data.wide,&data.high);        A.push(data);        int ans=data.high*data.wide;                for(int i=1;i<n;i++){            scanf("%d%d",&data.wide,&data.high);            if(data.high>=A.top().high)                A.push(data);            else{                totalwide=tempans=0;                while(!A.empty()&&A.top().high>data.high){                    totalwide+=A.top().wide;                    tempans=totalwide*A.top().high;                    if(tempans>ans)                        ans=tempans;                    A.pop();                }                totalwide+=data.wide;                data.wide=totalwide;                A.push(data);            }        }        totalwide=tempans=0;        while(!A.empty()){            totalwide+=A.top().wide;            tempans=totalwide*A.top().high;            A.pop();            if(tempans>ans)                ans=tempans;        }        printf("%d\n",ans);    }    return 0;}


0 0
原创粉丝点击