链接: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. Java垃圾回收机制[转]

    原文地址:http://blog.csdn.net/zsuguangh/article/details/6429592 综合了若干人的blog- 1. 垃圾回收的意义 在C++中,对象所占的内存在程序 ...

  2. DNA序列局部比对(Smith–Waterman algorithm)

    生物信息原理作业第三弹:DNA序列局部比对,利用Smith–Waterman算法,python3.6代码实现. 实例以及原理均来自https://en.wikipedia.org/wiki/Smith ...

  3. socketlog的安装和使用

    socketlog的使用范围  socketlog比较适用于ajax调试和api的调试,经典应用莫不过于微信众多的api使用调试,使用socketlog可以很条理清楚的查看到api传递的参数,加载的性 ...

  4. HTTPS的原理解析

    http://www.cnblogs.com/alisecurity/p/5939336.html 外加文档

  5. yum安装man命令程序错误

    第一次安装CentOS后,man命令无法使用,查了一下,发现没有安装man命令程序.尝试安装man yum install man 结果如下: Loaded plugins: fastestmirro ...

  6. 原码,反码,补码 与(&) 或(|) 非(~) 异或(^) 左移 << 右移 >> 无符号右移 >>>

    原码 数字在计算机中以二进制表示,8位的字长,最高位是符号位, 正数为0,负数为1.比如,3为0000 0011: -3为1000 0011. 注意,Java中int为32位.3的16进制表示为3,- ...

  7. PHP开发中多种方案实现高并发下的抢购、秒杀功能

    抢购.秒杀是如今很常见的一个应用场景,主要需要解决的问题有两个: 1 高并发对数据库产生的压力 2 竞争状态下如何解决库存的正确减少("超卖"问题) 对于第一个问题,已经很容易想到 ...

  8. PendingIntent

    PendingIntent表示一种即将发生的意图,和Intent的区别在于:PendingIntent是在将来的某个不确定的时刻发生,而Intent是立刻发生 典型使用场景是给RemoteViews添 ...

  9. Python 实现单例模式的一些思考

    一.问题:Python中如何实现单例模式 单例模式指一个类只能实例化一个对象. 二.解决方案: 所有资料参考于: http://python.jobbole.com/87294/ https://ww ...

  10. uva11400 动态规划

    没种电压灯泡要么全换,要么不换.状态d(i)表示前i种灯泡的最低价格. 转移方程: dp[i]=min(dp[i],dp[j]+(s[i]-s[j])*d[i].c+d[i].k); AC代码: #i ...