https://ac.nowcoder.com/acm/problem/50439
将人从大到小排列,这样子的好处在于万一队列新进来了人,当前人数大于这个人能接受的人数上限,可以从队列里删除人,且根据贪心肯定是删除队列里人值最小的人,那么就可以应用优先队列。
反之没有贪心策略。
#include <bits/stdc++.h>
#define ll long long
const int maxn = 1e6 + 10;
using namespace std;
struct node{
ll val,up;
bool operator < (const node &tmp) const{
return up > tmp.up;
}
}arr[maxn];
priority_queue<ll,vector<ll>,greater<ll> > q;
ll n;
int main()
{
scanf("%lld",&n);
for(int i = 1;i <= n;i++)
{
scanf("%lld%lld", &arr[i].val, &arr[i].up);
}
sort(arr+1,arr+1+n);
ll tmp = 0,ans = 0;
for(int i = 1;i <= n;i++)
{
q.push(arr[i].val);
tmp += arr[i].val;
while((ll)q.size() > arr[i].up)
{
tmp -= q.top();
q.pop();
}
ans = max(ans, tmp);
}
cout << ans << endl;
return 0;
}