超市 (0x15 二叉堆)

超市

题意

超市里有 N 件商品,每件商品都有利润 p i p_i pi 和过期时间 d i d_i di,每天只能卖一件商品,过期商品不能再卖。

求合理安排每天卖的商品的情况下,可以得到的最大收益是多少。

思路

首先将所有商品按照过期时间非严格单调递增排序,然后遍历每一件商品。将 S 定义为已经选择要卖的商品的集合,如果将当前遍历到的商品 node[i] 加入集合后,集合的大小小于等于 node[i]的过期时间 d i d_i di 那么符合继续遍历接下来的商品。否则,将当前集合中价值最小的商品移除,直到与 d i d_i di相等为止

代码
#include<bits/stdc++.h>
#include<queue>
// #define int long long
#define INF 0x3f3f3f3f
#define mod 1000000007
#define MOD 998244353
#define rep(i, st, ed) for (int (i) = (st); (i) <= (ed);++(i))
#define pre(i, ed, st) for (int (i) = (ed); (i) >= (st);--(i))

using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PII;
template<typename T> inline T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
template<typename T> inline T lowbit(T x) { return x & -x; }

const int N = 1e4 + 10;
int n;

struct Node {
	int p, d;
	
	bool operator<(const Node &x)const {
		return d < x.d;
	}
}node[N];

void solve() {
	while (scanf("%d", &n) != EOF) {
		int res = 0;
		priority_queue<PII, vector<PII>, greater<PII>>pq;

		for (int i = 1;i <= n;++i){
			cin >> node[i].p >> node[i].d;
		}
		
		sort(node + 1, node + 1 + n);
		for (int i = 1; i <= n; ++i) {
			pq.push({node[i].p , node[i].d});
			if (pq.size() <= node[i].d)continue;
			else {
				while(pq.size() && pq.size() > node[i].d)pq.pop();
			}
		}

		while (pq.size()) {
			res += pq.top().first;
			pq.pop();
		}

		cout << res << endl;
	}
}

signed main() {
	// int t; cin >> t;
	// while (t--)
	solve();

	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

zzqwtc

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

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

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

打赏作者

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

抵扣说明:

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

余额充值