链接:http://acm.tju.edu.cn/toj/showp4123.html4123.   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);
}
}

优先队列运用 TOJ 4123 Job Scheduling的更多相关文章

  1. [luoguP2949] [USACO09OPEN]工作调度Work Scheduling(贪心 + 优先队列)

    传送门 这个题类似于建筑抢修. 先按照时间排序. 如果当前时间小于任务截止时间就选, 否则,看看当前任务价值是否比已选的任务的最小价值大, 如果是,就替换. 可以用优先队列. ——代码 #includ ...

  2. OJ 26217 :Work Scheduling(贪心+优先队列)

    约翰有太多的工作要做.为了让农场高效运转,他必须靠他的工作赚钱,每项工作花一个单位时间. 他的工作日从0时刻开始,有10^8个单位时间.在任一时刻,他都可以选择编号1~N的N(1 <= N &l ...

  3. 队列(Queue)--环形队列、优先队列和双向队列

    1. 队列概述 队列和堆栈都是有序列表,属于抽象型数据类型(ADT),所有加入和删除的动作都发生在不同的两端,并符合First In, First Out(先进先出)的特性. 特性: ·FIFO ·拥 ...

  4. hdu1716排列2(stl:next_permutation+优先队列)

    排列2 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submis ...

  5. HDU 4123 Bob’s Race 树形dp+单调队列

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4123 Time Limit: 5000/2000 MS (Java/Others) Memory L ...

  6. 洛谷 P2949 [USACO09OPEN]工作调度Work Scheduling

    P2949 [USACO09OPEN]工作调度Work Scheduling 题目描述 Farmer John has so very many jobs to do! In order to run ...

  7. CSUOJ 1603 Scheduling the final examination

    1603: Scheduling the final examination Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 49  Solved: 1 ...

  8. 洛谷 P2949 [USACO09OPEN]工作调度Work Scheduling 题解

    P2949 [USACO09OPEN]工作调度Work Scheduling 题目描述 Farmer John has so very many jobs to do! In order to run ...

  9. HDU 4123 Bob’s Race 树的直径+ST表

    Bob’s Race Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=41 ...

随机推荐

  1. BZOJ 1004: [HNOI2008]Cards [Polya 生成函数DP]

    传送门 题意:三种颜色,规定使用每种颜色次数$r,g,b$,给出一个置换群,求多少种不等价着色 $m \le 60,\ r,g,b \le 20$ 咦,规定次数? <组合数学>上不是有生成 ...

  2. 软件开发:网站&视频&书籍推荐(不断更新)

    利用书籍进行系统学习,凭借博客/新闻等资料开阔眼界,辅之以代码及项目实战,并勤加以总结,方可进步. 常用网站: Leetcode刷题:https://leetcode.com/ ,练习数据结构和算法必 ...

  3. IOS教程视频汇总

    使用StoryBoard做iOS UI界面跳转

  4. 【深度学习】批归一化(Batch Normalization)

    BN是由Google于2015年提出,这是一个深度神经网络训练的技巧,它不仅可以加快了模型的收敛速度,而且更重要的是在一定程度缓解了深层网络中"梯度弥散"的问题,从而使得训练深层网 ...

  5. java环境搭建 windows

    windows搭建Java环境 1.下载java开发工具jdk安装包 下载地址:http://www.oracle.com/technetwork/java/javase/downloads/inde ...

  6. javamail+ical4j发送会议提醒

    本篇讲述小编在使用ical4j时对其的理解与使用,留作笔记的同时希望能帮助到大家! 初学者可以先了解下ical4j的基本信息: iCalender编程基础,了解与使用ical4j:https://ww ...

  7. MySQL创建用户与授权

    一. 创建用户 命令: CREATE USER 'username'@'host' IDENTIFIED BY 'password'; 说明: username:你将创建的用户名 host:指定该用户 ...

  8. PHPUnit-附录 A. 断言 (assert)

    [http://www.phpunit.cn/manual/5.7/zh_cn/appendixes.assertions.html] 本附录列举可用的各种断言方法. assertArrayHasKe ...

  9. centos 7 双网卡建网桥脚本实现

    #!/bin/bash interface1=`ls /sys/class/net|grep en|awk 'NR==1{print}'` interface2=`ls /sys/class/net| ...

  10. dedecms在任意页面调用任意栏目文章

    dedecms在任意页面调用任意栏目文章,我们用arclist标签即可实现.如果是调用多个栏目文章可以给typeid多个值. 我们以调用ID为1和ID为30的两个栏目下5篇文章为例: {dede:ar ...