highest reward

There are N tasks, each task has a latest end time and a corresponding reward. Complete the task before the end time to get the corresponding reward. The time required to complete each task is 1 unit of time. Sometimes it's impossible to complete all the tasks because there may be a conflict in time, which requires you to make trade-offs. Find the highest reward you can get.


Input
line 1: a number N, representing the number of tasks (2 <= N <= 50000) 

Lines 2 - N + 1, with 2 numbers in each line, separated by spaces, indicate the latest end time Ei of the task and the corresponding reward Wi. (1 <= Ei <= 10^9, 1 <= Wi <= 10^9)


Output

Output the highest reward you can get.


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

6 10


Sample Output

230

Ps: This question needs to use greedy thinking and get the highest reward before the completion time, depending on the code performance.

AC code:

#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;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324690222&siteId=291194637