poj2082 Terrible Sets

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <stack>
using namespace std;
const int maxn=50005;
struct node{
	int w,h;
}data;
int main(){
	int n,ans,totalw,curarea;
	while(scanf("%d",&n)&&n!=-1){
		stack<node>sta;
		ans=-1;
		for(int i=0;i<n;i++){
			scanf("%d %d",&data.w,&data.h);
			if(sta.empty()||data.h>sta.top().h){//如果当前元素的高大于栈顶元素的高,加入栈 
				sta.push(data);
			}
			else{//如果当前元素的高小于等于栈顶元素的高 
				totalw=0;
				curarea=0;
				while(sta.size()&&sta.top().h>data.h){//如果栈不为空且栈顶元素大于当前元素的高 
					totalw+=sta.top().w;//总宽增加 
					curarea=totalw*sta.top().h;//当前面积为总宽乘以栈顶的高度 
					if(curarea>ans){//储存最大值 
						ans=curarea;
					}
					sta.pop();
				}
				totalw+=data.w;//栈中加入一个新元素,高为当前高度,宽为弹出总素的总宽加上当前元素的宽 
				data.w=totalw;
				sta.push(data);
			}
		}
		totalw=0;
		curarea=0;
		while(sta.size()){//最后栈中元素必定是按高递增的 
			totalw+=sta.top().w;
			curarea=totalw*sta.top().h;
			if(curarea>ans){
				ans=curarea;
			}
			sta.pop();
		}
		printf("%d\n",ans);
	}
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值