*CF777E.Hanoi Factory(栈+贪心)

题目链接:https://vjudge.net/contest/376896#problem/J
题意:n个货物,每个货物都有内径a,外径b,高度h,他们堆放在一起的条件是上面货物的外径>下面货物的内径,小于上面货物的外径。问,最大堆放的高度是多少
解题思路:将货物按外径b从大到小排序,外径b相同的按内径a从小到大排序,注意内径一定要按从小到大排序,因为b相同的时候上下两个货物肯定能够堆放在一起,让上面的那个货物的a尽可能的小,可以使得后面货物选择的空间更大。
货物的堆放用栈实现,后进先出,如果当前要放的货物无法放入,则去掉货物堆最上面的那个货物,直到能够放入该货物

#include<iostream>
#include<cstdio>
#include<stack>
#include<algorithm>
using namespace std;
#define ll long long
int n;
ll sum;
ll ans;
struct node{
	int a;
	int b;
	ll h;
	bool operator < (const node tmp) const{
		return b==tmp.b?a>tmp.a:b>tmp.b;
	}
}stock[110000];
stack<node> s;
//bool cmp(node t1,node t2){
//	return t1.b==t2.b?t1.a>t2.a:t1.b>t2.b;
//}
int main(){
	cin>>n;
	for(int i=1;i<=n;i++)
		scanf("%d%d%lld",&stock[i].a,&stock[i].b,&stock[i].h);
	sort(stock+1,stock+1+n);
	s.push(stock[1]);
	sum+=stock[1].h;
	ans=sum;
	for(int i=2;i<=n;i++){
		while(!s.empty()&&s.top().a>=stock[i].b){
			sum-=s.top().h;
			s.pop();
		}
		s.push(stock[i]);
		sum+=stock[i].h;
		ans=max(sum,ans);
	}
	printf("%lld\n",ans);
	return 0;
}
		
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Buyi.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值