优先队列运用 TOJ 4123 Job Scheduling

 

链接:http://acm.tju.edu.cn/toj/showp4123.html
4123.    Job Scheduling

 


Time Limit: 1.0 Seconds    Memory Limit: 65536K
Total Runs: 130    Accepted Runs: 29


Given N jobs, each denoted by a 2-tuples integer (pi, ri) where pi is the processing time and ri is the release time.
You must construct a schedule for these jobs on a single machine obeying:
(1) at most one job is processed at each point in time;
(2) no job is processed before its release time. Let Ci denote the time at which job i is finished processing, then the goal is to find the schedule that minimizes C1+C2+...+Cn.

INPUT

First line will be a positive integer N (1≤N≤100000) indicating the number of jobs.
Then N lines follow each containing a job (pi, ri), where 0≤pi≤10000 and 0≤ri≤10000.

OUTPUT

The minimum of C1+C2+...+Cn. Please mod the answer by 1e9+7.

Sample Input

 


3
1 0
3 1
1 2

 

Sample Output

 


9


Hint: Time 0: start Job 1. 
Time 1: finish Job 1 and start Job 2.
Time 2: pause Job 2 and start Job 3. 
Time 3: finish Job 3 and start Job 2.
Time 5: finish Job 2.
    C1+C2+C3=1+5+3=9.

 


Source: TJU School Competition 2015

这道题当时想到了最优队列实现但是自己比较搓,不太会实现,当时又卡了另一题所以就没过。

这题的主要思想在每个时间点选择可以开工的工作里所剩完成时间最短的。

 

#include<queue>
#include<iostream>
#include<algorithm>
#include<cstdio>
using namespace std;
#define N 100005
#define MOD 1000000007

struct Job{
	int a,b;
}nodd[N];
int x,ans;
int cmp(Job a1,Job a2){
	if(a1.b==a2.b) return a1.a<a2.a;
	return a1.b<a2.b;	
}

int main(){
	int i,j,k;
	int now,temp;
	while(~scanf("%d",&x)){
		ans=0;
		for(i=0;i<x;i++)
		    scanf("%d %d",&nodd[i].a,&nodd[i].b);
        sort(nodd,nodd+x,cmp);
        i=0;
        now=0;
        priority_queue<int,vector<int>,greater<int> > q;
        while(i<x){
        	now=nodd[i].b;
        	q.push(nodd[i].a);
        	for(j=i+1;j<x;j++){
        		if(nodd[j].b==now) q.push(nodd[j].a);
        		else break;
			}
			if(j==x) break;
			else{
				k=j;
				while(now<nodd[k].b){
					if(!q.empty()){
						temp=q.top();
						q.pop();
						if(now+temp<=nodd[k].b){  //可以在下一个不同允许时间前的工作完成的 
							now+=temp;
							ans=(ans+now)%MOD;
						}else{                    //完成其中一部分,没完成的继续入列
							temp=temp-(nodd[k].b-now);
							now=nodd[k].b;
							q.push(temp);
						}						
					}else break;	
				}
				i=j;
			}
		}
		while(!q.empty()){
			temp=q.top();
			q.pop();
			now+=temp;
			ans=(ans+now)%MOD;
		}
		printf("%d\n",ans);
	}
}


 

转载于:https://www.cnblogs.com/Basasuya/p/8433780.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值