51nod 1163 最高的奖励(贪心+优先队列)

时间:2023-03-09 12:58:51
51nod 1163 最高的奖励(贪心+优先队列)

题目链接:51nod 1163 最高的奖励

看着这题我立马就想到昨天也做了一道贪心加优先队列的题了奥。

按任务最晚结束时间从小到大排序,依次选择任务,如果该任务最晚结束时间比当前时间点晚,则将该任务的奖励值压入队列,否则将队列中最小的任务的奖励值替换,优先队列按奖励值小的优先。

 #include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
typedef long long ll;
const int N = ;
struct task{
int e;
int w;
}a[N];
bool cmp(task x, task y){
return x.e < y.e;
}
priority_queue<int, vector<int>, greater<int> >q;
int main(){
int n, i, j;
ll ans = ;
scanf("%d", &n);
for(i = ; i < n; ++i)
scanf("%d%d", &a[i].e, &a[i].w);
sort(a, a+n, cmp);
for(i = ; i < n; ++i){
if(a[i].e > q.size()){
//如果该任务最晚结束时间比当前时间点晚
q.push(a[i].w);
ans += a[i].w;
}
else{
//可能替换为奖励更高的任务
ans += a[i].w;
q.push(a[i].w);
ans -= q.top();
q.pop();
}
}
printf("%lld\n", ans);
return ;
}