Powerful Discount Tickets
Problem Statement
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/2^y
(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
Input is given from Standard Input in the following format:
N M
A1 A2 … AN
Output
Print the minimum amount of money required to buy all the items.
Sample Input 1
3 3
2 13 8
Sample Output 1
9
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.
Sample Input 2
4 4
1 9 3 5
Sample Output 2
6
Sample Input 3
1 100000
1000000000
Sample Output 3
0
We can buy the item priced 1000000000 yen for 0 yen with 100000 tickets.
Sample Input 4
10 1
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000
Sample Output 4
9500000000
题意就是有n个物品,你有m张优惠券,如果你用y张优惠券买一个价格为x的物品,你就可以用x/(2^y)(即每用一张优惠券价格降至原来的一半)的价格买到这个物品。要求用这m张优惠券买下所有物品花的钱尽可能少。
思路很明显,每次让最高价格的物品除以2就行了(rounded down to the nearest integer,所以每次直接除就行,本来就要求向下取整的),用优先队列就可以(默认从小到大)。
#include<bits/stdc++.h>
using namespace std;
priority_queue<int>q;
int main()
{
long long n,m,a;
cin >> n >> m;
for(int i = 0;i < n;i ++){
cin >> a;
q.push(a);
}
long long sum = 0;
int t;
for(int i = 0;i < m;i ++){
t = q.top()/2;
q.pop();
q.push(t);
}
while(!q.empty()){
sum += q.top();
q.pop();
}
cout << sum << endl;
return 0;
}
priority_queue<Type, Container, Functional>
//升序队列
priority_queue <int,vector<int>,greater<int> > q;
//降序队列
priority_queue <int,vector<int>,less<int> >q;