OJ 26217 : Work Scheduling (greedy + priority queue)

John has too much work to do. In order for the farm to function efficiently, he must make money from his work, each of which takes one unit of time. His workday starts at time 0 and has 10^8 units of time. At any moment, he can choose any one of the N (1 <= N <= 10^6) jobs numbered 1~N to complete. Since he can only do one job per unit of time, and each job has a deadline, it is very difficult for him to have time to complete all N jobs, although it is still possible. For the i-th job, there is a deadline D_i(1 <= D_i <= 10^9), if he can complete this job, then he can profit P_i( 1<=P_i<=10^9 ). In giving What is the maximum profit that John can make under the given job profit and deadline.
Description

Farmer John has so very many jobs to do! In order to run the farm efficiently, he must make money on the jobs he does, each one of which takes just one time unit.

His work day starts at time 0 and has 1,000,000,000 time units (!). He currently can choose from any of N (1 ≤ N ≤ 100,000) jobs conveniently numbered 1..N for work to do. It is possible but extremely unlikely that he has time for all Njobs since he can only work on one job during any time unit and the deadlines tend to fall so that he can not perform all the tasks.

Job i has deadline Di (1 ≤ Di ≤ 1,000,000,000). If he finishes job i by then, he makes a profit of Pi (1 ≤ Pi ≤ 1,000,000,000).

What is the maximum total profit that FJ can earn from a given list of jobs and deadlines? The answer might not fit into a 32-bit integer.

Input

Multipel test cases. For each case :

* Line 1: A single integer: N

* Lines 2..N+1: Line i+1 contains two space-separated integers: Di and Pi

Output

For each case, output one line : A single number on a line by itself that is the maximum possible profit FJ can earn.

Sample Input

3
2 10
1 5
1 7

Sample Output

17

Hint

Complete job 3 (1, 7) at time 1 and complete job 1 (2, 10) at time 2 to maximize the earnings (7 + 10 → 17).

 The greedy strategy for this problem is:

  Add all the values ​​first. Then synchronously count a now statistical time.

  If the current time has passed the realization, then we subtract the smallest value.

  Then the smallest value can be maintained with a heap.

AC code:

#include<stdio.h>
#include<queue>
#include<vector>
#include<algorithm>
using namespace std;
#define ll long long
#define MAX 100008
struct nod
{
    ll d;
    ll p;
}a[MAX];
bool cmp (nod a,nod b)
{
    if(a.d!=b.d)
        return a.d<b.d;
        return a.p<b.p;
}
intmain ()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        priority_queue<int,vector<int>,greater<int> >q;
        for(int i=0 ; i<n ; i++)
            scanf("%d%d",&a[i].d,&a[i].p);
        sort(a,a+n,cmp);
        ll sum=0;
        ll now=0;
        for(int i=0 ; i<n ; i++)
        {
            now++;
            sum+=a[i].p;
            q.push(a[i].p);
            if(now>a[i].d)
            {
                sum-=q.top();
                q.pop();
                now--;
            }
        }
        printf("%lld\n",sum);
    }
}
View Code

 

Guess you like

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