zoukankan      html  css  js  c++  java
  • 优先队列运用 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
    ",ans);
    	}
    }


  • 相关阅读:
    swift 第十四课 可视化view: @IBDesignable 、@IBInspectable
    swift 第十三课 GCD 的介绍和使用
    swift 第十二课 as 的使用方法
    swift 第十一课 结构体定义model类
    swift 第十课 cocopod 网络请求 Alamofire
    swift 第九课 用tableview 做一个下拉菜单Menu
    swift 第八课 CollectView的 添加 footerView 、headerView
    swift 第七课 xib 约束的优先级
    swift 第六课 scrollview xib 的使用
    swift 第五课 定义model类 和 导航栏隐藏返回标题
  • 原文地址:https://www.cnblogs.com/Basasuya/p/8433780.html
Copyright © 2011-2022 走看看