链接: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. Android端生成META-INF信息文件的Gradle插件 RapidMetaInfPlugin

    来源博客:Wang Jie's Blog 本文链接:<http://blog.wangjiegulu.com/2018/02/05/Android端生成META-INF信息文件的Gradle插件 ...

  2. bzoj 4546: codechef XRQRS [可持久化Trie]

    4546: codechef XRQRS 可持久化Trie codechef上过了,bzoj上蜜汁re,看别人说要开5.2e5才行. #include <iostream> #includ ...

  3. [SCOI2010]幸运数字 [容斥原理 dfs]

    题意:"幸运号码"是十进制表示中只包含数字6和8的那些号码,求\([l,r]:r \le 10^10\)之间"幸运号码"的倍数个数 发现幸运号码貌似很少唉,去掉 ...

  4. 关于对MVC和MVVM的思考

    前言:最近公司交给我一个web项目,其采用的框架是java中的zkoss,它不用于以往我平时用的mvc,它采用的mvvm模式,因为以前只理解过mvc,经常使用譬如SpringMvc.Struts2等框 ...

  5. git命令行工作的正确姿势

    git命令行创建并提交新分支到mater分支的常规步骤 git branch new_branch git status 查看修改的文件 git add changed_files git commi ...

  6. 洛谷 P2073 送花【Treap】题解+AC代码

    题目背景 小明准备给小红送一束花,以表达他对小红的爱意.他在花店看中了一些花,准备用它们包成花束. 题目描述 这些花都很漂亮,每朵花有一个美丽值W,价格为C. 小明一开始有一个空的花束,他不断地向里面 ...

  7. python进阶学习笔记(一)

    python进阶部分要学习的内容: 学习目标: 1.函数式编程 1.1,什么是函数式编程 函数式编程是一种抽象计算的编程模式 不同语言的抽象层次不同: 函数式编程的特点: python支持的函数式编程 ...

  8. 【学习笔记】Hibernate 一对一关联映射 组件映射 二级缓存 集合缓存

    啊讲道理放假这十天不到啊 感觉生活中充满了绝望 这就又开学了 好吧好吧继续学习笔记?还是什么的 一对一关联映射 这次我们仍然准备了两个表 一个是用户表Users 一个是档案表Resume 他们的关系是 ...

  9. 01_JavaSE之OOP--面向对象(类和面向对象的简单认识)

    面向对象(一) 一.面向对象概述 谈到面向对象就不得不谈谈面向过程,面向对象也是由面向过程发展而来. 面向过程思想概述 面向过程,简而言之就是分步骤,过程化的去解决问题,代表语言有:Pascal,C等 ...

  10. 以kaggle-titanic数据为基础的完整的机器学习

    1. 引入所有需要的包 # -*- coding:utf-8 -*- # 忽略警告 import warnings warnings.filterwarnings('ignore') # 引入数据处理 ...