一、题目描述

Constraints :Time Limit: 2 secs, Memory Limit: 32 MB

Description:

  We often hear that computer is a magic, a great invention, or even a marvel. But actually computer is just a tool people use everyday. It is a machine that can help people to process many jobs effectively. Moreover, without computer, you can not play ICPC. So, guys, let’s study some stuff about computer here.
  One computer has one CPU (Central Processing Unit). CPU can be idle or processing one job at any time. Jobs come randomly and are stored in the memory until finished. CPU will process jobs according to some strategies. The processing job can be interrupted and saved back so that CPU can be available for other jobs. 
  Each job has a release time and a processing time. Assume that we know the schedule of all jobs, please generate a program to minimize the sum of completion times of all jobs using a strategy which assigns and interrupts jobs properly.
  For example, suppose there are two jobs to be completed. Job 1 is released at time 1 and needs 4 time units to process. Job 2’s release time and processing time is 3 and 1. Figures below show three solutions:


  Figure 1 shows a solution with the total complete time 4 + 6 = 10, and the result of Figure 2 and 3 are both 5 + 6 = 11. In fact, Figure 1 shows the optimal solution 
Please note that all of the jobs will be released, interrupted and assigned in integer time unit.

Input:

  Input may consist of multiple test cases.
  Every test case begins with a line that contains one integer n (1<= n <= 50000) denoting the number of jobs. Each of the following n lines contains 2 integers: ri and pi, (1 <= ri <= 10^9, 1 <= pi <= 10000) denoting the release time and processing time of job i.
  Input is terminated by EOF.

Output:

  For every test case, print one line with an integer denoting the minimum sum of completion times.

Sample Input

  2
  1 4
  3 1
Sample Output

  10

二、问题解决

  1、题意:单机任务调度,模拟CPU工作方式,CPU每个时刻只能处理一个任务,给出每个任务的release时间和process时间,设计调度算法,使所有任务完成时间总和最小。

  2、思路:贪心思想,每次处理process时间最短的任务。

  具体步骤:用循环模拟CPU,每个循环表示一个CPU时间。全局维护一个待处理的最小任务优先队列que,即剩余process时间短的任务优先。每次循环中:(1)取出que中top元素,将其剩余process时间减1,即CPU此时刻处理该任务,减到0则记录该任务完成时间;(2)将该时间刻release的任务加入等候队列que中。

  3、代码:基于上面的思路,可以写出下面的代码。

 // Problem#: 1717
#include <iostream>
#include <cstdio>
#include <queue>
#include <algorithm>
using namespace std;
const int MAX = ;
struct Job
{
int r,p;
bool operator < (const Job &job) const {
if(r == job.r) return p < job.p;
else return r < job.r;
}
};
Job jobs[MAX]; int main() {
int n;
while(scanf("%d",&n) != EOF) {
for(int i = ; i < n; ++i) {
scanf("%d%d", &jobs[i].r, &jobs[i].p);
}
sort(jobs,jobs+n); priority_queue<int,vector<int>,greater<int> > que;
long long ans = ;
int cnt = , cur = , tmp; que.push(jobs[].p);
for(long long i = jobs[].r; ;++i) {
tmp = que.top(); que.pop();
if (tmp- == ) {
ans = ans+i;
cnt += ;
if(cnt == n) break;
}
else que.push(tmp-);
while(cur < n && jobs[cur].r <= i) {
que.push(jobs[cur++].p);
}
}
printf("%lld\n",ans);
}
return ;
}

暴力代码

  4、进一步分析:虽然思路对了,但上面代码明显会超时。

  这时考虑加快cpu工作速度,即不是模拟CPU每个时刻处理的任务,而是考虑每个任务能否一次性完成。大概思路是这样,取出que中最小任务时间,判断在下一个任务release时刻(距离上一任务process时刻会有一段时间间隔),该任务能否完成,若能完成,就可以缩短时间间隔,继续判断下一任务;若不能完成,则将该任务process时间减去时间间隔,重新加入que,并将下一任务加入que。

代码如下:

 // Problem#: 1717
#include <iostream>
#include <cstdio>
#include <queue>
#include <algorithm>
using namespace std;
const int MAX = ;
struct Job
{
int r,p;
bool operator < (const Job &job) const {
if(r == job.r) return p < job.p;
else return r < job.r;
}
};
Job jobs[MAX]; int main() {
int n;
while(scanf("%d",&n) != EOF) {
for(int i = ; i < n; ++i) {
scanf("%d%d", &jobs[i].r, &jobs[i].p);
}
sort(jobs,jobs+n); priority_queue<int,vector<int>,greater<int> > que;
long long ans = ;
int i = , cnt = , cur, tmp;
int pre = jobs[].r; //前一任务处理时刻
int cur_time = jobs[].r; //当前时刻 que.push(jobs[].p);
while(true) {
cur = que.top(); que.pop();
if (i >= n) {
cur_time += cur;
ans += cur_time;
cnt += ;
if (cnt == n) break;
continue;
}
tmp = jobs[i].r - pre;
if (cur <= tmp) {
cur_time += cur;
ans += cur_time;
cnt += ;
pre = cur_time;
if (cnt == n) break;
if (!que.empty()) continue; //时间间隔缩短,能否继续处理que中剩余任务
}else {
que.push(cur-tmp);
cur_time = jobs[i].r;
}
pre = jobs[i].r;
cur_time = pre;
while(i < n && jobs[i].r == pre)
que.push(jobs[i++].p);
} printf("%lld\n",ans);
}
return ;
}

结果超出我的想象,折磨了2个小时,感觉智商远远不够用啊!

SOJ 1717 Computer (单机任务调度)的更多相关文章

  1. [SOJ] 1282. Computer games (KMP)

    坑爹题 1282. Computer Game Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description Brian is an ...

  2. Quartz2.0以上版本的单机和集群

    (一)Quartz单机 1.Quartz简介 Quartz是一个完全由java编写的开源作业调度框架,能实现业务的定时调度.Quartz主要有三个核心调度器.任务和触发器: ①任务-JobDetail ...

  3. 转载:Hadoop安装教程_单机/伪分布式配置_Hadoop2.6.0/Ubuntu14.04

    原文 http://www.powerxing.com/install-hadoop/ 当开始着手实践 Hadoop 时,安装 Hadoop 往往会成为新手的一道门槛.尽管安装其实很简单,书上有写到, ...

  4. 【niubi-job——一个分布式的任务调度框架】----框架设计原理以及实现

    引言 niubi-job的框架设计是非常简单实用的一套设计,去掉了很多其它调度框架中,锦上添花但并非必须的组件,例如MQ消息通讯组件(kafka等).它的框架设计核心思想是,让每一个jar包可以相对之 ...

  5. 【niubi-job——一个分布式的任务调度框架】----安装教程

    niubi-job是什么 niubi-job是LZ耗时三个星期,费尽心血打造的一个具备高可靠性以及水平扩展能力的分布式任务调度框架,采用quartz作为底层的任务调度管理器,zookeeper做集群的 ...

  6. What Your Computer Does While You Wait

    转: CPU的等待有多久? 原文标题:What Your Computer Does While You Wait 原文地址:http://duartes.org/gustavo/blog/ [注:本 ...

  7. Hadoop安装教程_单机/伪分布式配置_Hadoop2.6.0/Ubuntu14.04

    摘自: http://www.cnblogs.com/kinglau/p/3796164.html http://www.powerxing.com/install-hadoop/ 当开始着手实践 H ...

  8. Hadoop安装教程_单机/伪分布式配置

    环境 本教程使用 CentOS 6.4 32位 作为系统环境,请自行安装系统(可参考使用VirtualBox安装CentOS).如果用的是 Ubuntu 系统,请查看相应的 Ubuntu安装Hadoo ...

  9. 新一代分布式任务调度框架:当当elastic-job开源项目的10项特性

    作者简介: 张亮,当当网架构师.当当技术委员会成员.消息中间件组负责人.对架构设计.分布式.优雅代码等领域兴趣浓厚.目前主导当当应用框架ddframe研发,并负责推广及撰写技术白皮书.   一.为什么 ...

随机推荐

  1. 一次DB服务器性能低下引发的对Nonpaged Pool Leak问题的诊断

    1. 问题表象+分析 最开始是DB访问性能下降,某个不用Cache.直接到DB的查询10s+都不返回.上去一看,DB Server内存97%,可用内存才100多M. Windows毕竟不是iOS,不留 ...

  2. 仓储管理系统500bug记录一下mysql 8小时超时解决办法

    HTTP Status 500 - Request processing failed; nested exception is org.springframework.dao.TransientDa ...

  3. C++重载流运算符,将存储结构体的vector直接写入文件

    我们知道,当vector很大的时候,如果使用循环的方式将其中的元素写入文件将非常费时,因此有没有办法将vector一次性写入文件呢? 采用流运算符重载的方法可以做到,不仅基本类型的vector可以一次 ...

  4. [windows操作系统]目录和文件相关操作

    1.导出目录的树形结构到文本文件 tree /F d:\dir1 > d:\tree.txt 就是将d:\dir1的目录结构以树状形式输出报告到文件tree.txt中. 效果是这样的:

  5. [linux-内核][转]内核日志及printk结构浅析

    这段时间复习了一下内核调试系统,注意看了一下printk的实现以及内核日志的相关知识,这里做一下总结. 1.问题的引出: 做DPDK项目时,调试rte_kni.ko时,发现printk并不会向我们想想 ...

  6. 通过声明Attribute属性改变不同类的输出效果

    ConsoleApplication--控制台应用程序 首先创建基类: using System; using System.Collections.Generic; using System.Lin ...

  7. Linux代码的重用与强行卸载Linux驱动

    (一)Linux代码的重用 重用=静态重用(将要重用的代码放到其他的文件的头文件中声明)+动态重用(使用另外一个Linux驱动中的资源,例如函数.变量.宏等) 1.编译是由多个文件组成的Linux驱动 ...

  8. 自定义iOS7导航栏背景,标题和返回按钮文字颜色

    在iOS7下,默认导航栏背景,颜色是这样的,接下来我们就进行自定义,如果你仅仅是更改一下背景和颜色,代码会很简单,不需要很复杂的自定义View来替代leftBarItem 更改导航栏的背景和文字Col ...

  9. 初探NIOS II之hello_world

    平台背景: 操作系统:win7  64bit 开发板:DE2-115 Quartus ii:15.0及配套的NIOS ii开发平台 一.硬件系统的建立 1.在Quartus里新建工程,这是很基本的就不 ...

  10. Java 验证码工具类

    package com.wuyu.util; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import ...