Powerful Discount Tickets(优先队列)

Powerful Discount Tickets(优先队列)

时间限制: 1 Sec  内存限制: 128 MB

题目描述

Takahashi is going to buy N items one by one.
The price of the i-th item he buys is Ai yen (the currency of Japan).
He has M discount tickets, and he can use any number of them when buying an item.
If Y tickets are used when buying an item priced X yen, he can get the item for X/2Y (rounded down to the nearest integer) yen.
What is the minimum amount of money required to buy all the items?

Constraints
·All values in input are integers.
·1≤N,M≤105
·1≤Ai≤109

输入

Input is given from Standard Input in the following format:

N M
A1 A2 ... AN

 

输出

Print the minimum amount of money required to buy all the items.

样例输入

【样例1】
3 3
2 13 8
【样例2】
4 4
1 9 3 5
【样例3】
1 100000
1000000000
【样例4】
10 1
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000

样例输出 

【样例1】
9
【样例2】
6
【样例3】
0
【样例4】
9500000000

提示

样例1解释
We can buy all the items for 9 yen, as follows:
Buy the 1-st item for 2 yen without tickets.
Buy the 2-nd item for 3 yen with 2 tickets.
Buy the 3-rd item for 4 yen with 1 ticket.
样例3解释
We can buy the item priced 1000000000 yen for 0 yen with 100000 tickets.

思路:找到价值最大的商品,然后把优惠券给它用,直到用完为止,附上AC代码

#include<bits/stdc++.h>
using namespace std;

typedef unsigned long long ll;

int main()
{
	ll n = 0; ll m = 0;
	priority_queue<ll> q;

	cin >> n >> m;
	vector<ll> price(n, 0);
	
	for(ll i = 0; i < n; ++i)
	{
		cin >> price[i];
		q.push(price[i]);
	}
	
	while(m--)
	{
		ll temp = q.top();
		temp /= 2;
		q.push(temp);
		q.pop();
	}
	
	ll res = 0;
	
	while(!q.empty())
	{
		res += q.top();
		q.pop();
	}
	
	cout << res << endl;
	
	return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值