超市POJ1456

超市POJ1456

思路

题干在这:POJ1456
先按照过期时间由小到大排序,然后建立一个优先队列(利润的小根堆),挨个商品询问。如果商品过期时间大于队列中元素,直接入队,否则只留下和堆顶之中利润较大的。询问完后堆中利润之和就是答案

ac代码

include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std;
struct thing {
	int profit;
	int deadline;
	friend bool operator < (const thing& a,const thing& b) {
		return a.profit > b.profit;
	}
}a[10010];
bool cmpy(thing& a, thing& b) {
	return a.deadline < b.deadline;
}
priority_queue<thing> sold;
int main() {
	int n;
	while (scanf_s("%d", &n) > 0) {
		for (int i = 0; i < n; i++)
			scanf_s("%d%d", &a[i].profit, &a[i].deadline);
		sort(a, a + n, cmpy);
		int tot = 0;
		for (int i = 0; i < n; i++) {
			if (a[i].deadline > int(sold.size()))
				sold.push(a[i]);
			else if(a[i].profit>sold.top().profit){
				sold.pop();
				sold.push(a[i]);
			}
		}
		int sum = 0;
		while (sold.size()) {
			sum += sold.top().profit;
			sold.pop();
		}
		cout << sum << endl;
	}
}
发布了24 篇原创文章 · 获赞 0 · 访问量 348

猜你喜欢

转载自blog.csdn.net/qq_45616764/article/details/104241892