题目:
给定n个音乐,每个音乐都有一定长度和欣赏值,再给定一个k,求不超过k的情况下,
音乐长度和乘以其中欣赏值的最小值的最大值。
题解:
用piar容器存放数据,用优先队列来去除最小的欣赏值,再找出其中的最大值。
代码:
#include<iostream>
#include<utility>
#include<queue>
#include<algorithm>
using namespace std;
typedef long long ll;
const int MAX = 3e5 + 10;
pair<ll, ll> a[MAX];
priority_queue<ll, vector<ll>, less<ll> >q;//从小到大取出
int main()
{
ios::sync_with_stdio(0);
ll n, k;
cin >> n >> k;
for (int i = 0; i < n; ++i) {
cin >> a[i].second>> a[i].first;
}
//把美丽度从大到小排序
sort(a, a + n);
ll ans = 0, tmp = 0;
for (int i = n; i >= 0; --i) {
tmp += a[i].second;
q.push(-a[i].second); //把每组的美丽度放入优先队列
if (q.size() > k) {//去除最小的美丽度
tmp += q.top();
q.pop();
}
ans = max(ans, tmp * a[i].first);
}
printf("%I64d", ans);
return 0;
}