AT4864 题解

题目传送门

显然是贪心题。

对于每张优惠券,我们应该给当前最大的物品使用。

如果使用普通的数组,每次都找最大值太慢了。

因此,我们使用传说神器:优先队列

其他题解都没有说优先队列的用法,那么我来告诉大家。

送上满分代码:

#include <iostream>
#include <cstdio>
#include <queue>
#include <stack>
#include <vector>
#include <algorithm>
using namespace std;
priority_queue <int> Q;
int main()
{
	//以下为输入。 
	int n, m;
	scanf("%d%d", &n, &m);
	for (int i = 1; i <= n; i++)
	{
		int x;
		scanf("%d", &x);
		Q.push(x);
	}
	//以下是使用优惠券的过程。 
	for (int i = 1; i <= m; i++)
	{
		int t = Q.top();
		Q.pop();
		Q.push(t / 2);
		/*
		注意不能写成下面这样。
		Q.push(Q.top() / 2);
		Q.pop();
		*/
	}
	//计算结果并输出。 
	long long sum = 0;
	while (!Q.empty())
	{
		sum += Q.top();
		Q.pop();
	}
	printf("%lld\n", sum);  //千万别忘了换行。 
	return 0;		
}

首发:2022-02-03 16:49:13

posted @ 2022-08-25 00:05  liangbowen  阅读(18)  评论(0编辑  收藏  举报