最高的奖励

有N个任务,每个任务有一个最晚结束时间以及一个对应的奖励。在结束时间之前完成该任务,就可以获得对应的奖励。完成每一个任务所需的时间都是1个单位时间。有时候完成所有任务是不可能的,因为时间上可能会有冲突,这需要你来取舍。求能够获得的最高奖励。


Input
第1行:一个数N,表示任务的数量(2 <= N <= 50000) 

第2 - N + 1行,每行2个数,中间用空格分隔,表示任务的最晚结束时间Ei以及对应的奖励Wi。(1 <= Ei <= 10^9,1 <= Wi <= 10^9)


Output

输出能够获得的最高奖励。


Sample Input
7
4 20
2 60
4 70
3 40
1 30
4 50

6 10


Sample Output

230

Ps:本题要运用贪心思想,要在完成时间前获得奖励最高,具体看代码表现。

AC代码:

#include<bits/stdc++.h>
#define ll long long
using namespace std;
struct node
{
int x,y;
} ;
int cmp1(node a,node b)
{
if(a.x==b.x)
return a.y>b.y;
return a.x<b.x;
}
struct cmp{
bool operator ()(node a,node b)
{
return a.y>b.y;
}
};
int main()
{
int n;
node a[50005];
cin>>n;
for(int i=0;i<n;i++)
cin>>a[i].x>>a[i].y;
priority_queue<node,vector<node>,cmp>q;
sort(a,a+n,cmp1);
int t=0;
for(int i=0;i<n;i++)
{
if(t<a[i].x)
{
q.push(a[i]);
t++;
}
else if(t==a[i].x)
{
if(a[i].y>q.top().y)
{
q.pop();
q.push(a[i]);
}
}
}
ll sum=0;
while(!q.empty())
{
sum+=q.top().y;
q.pop();
}
cout<<sum<<endl;
return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41292370/article/details/80049101