Powerful Discount Tickets
AtCoder 水题
-
第一次写 AtCoder 题解
-
题目传送门
- 这篇题解的代码很短,思路也很清晰,适合初学者食用
题目的意思其实可以转换为:使用一张抵扣券可以使一件物品的价格变成原来的一半(向下取整)。
而题目又要求总价最小,所以每次挑最贵的物品打折即可。
这里使用优先队列(小根堆)实现维护序列最大值。每次取出队首元素即为最大值,将其变为原来的一半,再插入进去,重复
AC 代码:
#include <bits/stdc++.h>
#define int long long
using namespace std;
priority_queue<int> q;
signed main()
{
int n,m,x,ans=0;
cin>>n>>m;
for(int i=1;i<=n;i++)cin>>x,q.push(x);
while(m--)q.push(q.top()/2),q.pop();
while(!q.empty())ans+=q.top(),q.pop();
cout<<ans;
return 0;
}