优先队列运用 TOJ 4123 Job Scheduling
链接: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的更多相关文章
- [luoguP2949] [USACO09OPEN]工作调度Work Scheduling(贪心 + 优先队列)
传送门 这个题类似于建筑抢修. 先按照时间排序. 如果当前时间小于任务截止时间就选, 否则,看看当前任务价值是否比已选的任务的最小价值大, 如果是,就替换. 可以用优先队列. ——代码 #includ ...
- OJ 26217 :Work Scheduling(贪心+优先队列)
约翰有太多的工作要做.为了让农场高效运转,他必须靠他的工作赚钱,每项工作花一个单位时间. 他的工作日从0时刻开始,有10^8个单位时间.在任一时刻,他都可以选择编号1~N的N(1 <= N &l ...
- 队列(Queue)--环形队列、优先队列和双向队列
1. 队列概述 队列和堆栈都是有序列表,属于抽象型数据类型(ADT),所有加入和删除的动作都发生在不同的两端,并符合First In, First Out(先进先出)的特性. 特性: ·FIFO ·拥 ...
- hdu1716排列2(stl:next_permutation+优先队列)
排列2 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submis ...
- HDU 4123 Bob’s Race 树形dp+单调队列
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4123 Time Limit: 5000/2000 MS (Java/Others) Memory L ...
- 洛谷 P2949 [USACO09OPEN]工作调度Work Scheduling
P2949 [USACO09OPEN]工作调度Work Scheduling 题目描述 Farmer John has so very many jobs to do! In order to run ...
- CSUOJ 1603 Scheduling the final examination
1603: Scheduling the final examination Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 49 Solved: 1 ...
- 洛谷 P2949 [USACO09OPEN]工作调度Work Scheduling 题解
P2949 [USACO09OPEN]工作调度Work Scheduling 题目描述 Farmer John has so very many jobs to do! In order to run ...
- 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 ...
随机推荐
- JDBC(二)
三层架构的一些基本报结构如下: domain包:下面是一些实体bean,属性为private,提供属性相对应的set和get方法.一般对应于数据库中的一张数据表,属性对应于数据表中的列. dao包,数 ...
- Vue.js搭建路由报错 router.map is not a function,Cannot read property ‘component’ of undefined
错误: 解决办法: 2.0已经没有map了,使用npm install vue-router@0.7.13 命令兼容1.0版本vue 但是安装完之后会出现一个错误: Cannot read prope ...
- ctags的使用
1. 生成tags文件 为当前目录下的所有C程序文件生成对应的tags文件: $ ctags *.c 为同一个目录下的所有文件建立tags如下 $ ctags –R (较常用) 这个命令 ...
- [2-SAT]【学习笔记】【未完】
这种一看就很2的东西.... 参考资料: 由对称性解2-sat问题 2-sat解法浅析 $SAT$理论: $2-SAT$ 两种形式: $x \in \hat B$ $x \lor y(x,\ y \i ...
- POJ1556 The Doors [线段相交 DP]
The Doors Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 8334 Accepted: 3218 Descrip ...
- C# 使用GDI制作垂直进度条(由下往上)
使用GDI+绘进度条的方式多种多样,可以封装一个UserControl,也可以直接使用一个控件来绘制(Label.Image.Panel等),甚至可以直接在winForm上画一个,关键代码没几行(这里 ...
- SpringMVC之HelloWorld实例
1.1 Helloworld实例的操作步骤 1. 加入jar包 2. 配置dispatcherServlet 3. 加入Spring配置文件 4. 编写请求处理器 并表示为处理器 5. 编写视图 1 ...
- 关于node的基础理论,书上看来的
最近看了一本书,说了一些Node.js的东西,现在来记录一下,让自己记得更牢靠一点. 在书上,是这样介绍的:Node.js模型是源于Ruby的Event Machine 和 Python的Twiste ...
- chrome_options
用法 from selenium.webdriver.chrome.options import Options chorme_option=Options() chorme_option.add ...
- httping:测量网站延迟
遇到网络问题的时候,我们一般会先通过 ping 这个工具来了解基本的情况.httping 与 ping 类似,不过它不是发送 ICMP 请求,而是发送 HTTP 请求.利用 httping,我们可以测 ...