toj. Job Scheduling

思路应该是按开始时间进行排序,再用优先队列

#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define INF 0x3f3f3f3f
#define inf -0x3f3f3f3f
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define mem0(a) memset(a,0,sizeof(a))
#define mem1(a) memset(a,-1,sizeof(a))
#define mem(a, b) memset(a, b, sizeof(a))
const long long MOD=1e9+7;
typedef long long ll;
struct node{
    int r;
    int p;
}a[100100];

bool cmp(node a,node b){
    if(a.r==b.r)
        return a.p<b.p;
    return a.r<b.r;
}

int main(){
    int n,j,k;
    while(scanf("%d",&n)!=EOF){
        for(int i=0;i<n;i++)
            scanf("%d%d",&a[i].p,&a[i].r);
        sort(a,a+n,cmp);
        priority_queue<int,vector<int>,greater<int> >Q;
        int i=0,now;
        int ans=0;
        while(i<n){
            now=a[i].r;
            Q.push(a[i].p);
            for(j=i+1;j<n;j++){
                if(now==a[j].r)
                    Q.push(a[j].p);
                else
                    break;
            }
            if(j==n)
                break;
            k=j;
            while(now<a[k].r){
                if(!Q.empty()){
                    int tmp=Q.top();
                    Q.pop();
                    if(now+tmp<=a[k].r){
                        now=now+tmp;
                        ans=(ans+now)%MOD;
                    }
                    else{
                        tmp=tmp-(a[k].r-now);
                        now=a[k].r;
                        Q.push(tmp);
                    }
                }
                else
                    break;
            }
            i=k;
        }
        while(!Q.empty()){
                int tmp=Q.top();
                Q.pop();
                now=now+tmp;
                ans=(ans+now)%MOD;
        }
        printf("%d\n",ans);
    }
    return 0;
}

你可能感兴趣的:(toj. Job Scheduling)