和我的上一篇博客(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; }