Something to learn: http://blog.csdn.net/yuwenshi/article/details/36666453

Shortest Job First Algorithm - kinda greedy: we do shorter job first BUT we only consider arrived jobs.

#include <iostream>
#include <fstream>
#include <iterator>
#include <vector>
#include <stack>
#include <cstring>
#include <climits>
#include <algorithm>
#include <unordered_map>
#include <unordered_set>
#include <set>
#include <queue>
using namespace std; struct Rec
{
Rec(int s, int d) : start(s), duration(d){}
int start;
int duration; bool operator < (const Rec& p) const {
return start < p.start;
}
}; struct Cmp
{
bool operator()(const Rec& p1, const Rec& p2) {
return p1.duration > p2.duration;
}
}; int main()
{ int n; cin >> n; // Get input and sort by arriving time
vector<Rec> in;
for(int i = ; i < n; i ++)
{
int s, t; cin >> s >> t;
in.push_back(Rec(s, t));
}
sort(in.begin(), in.end()); // Shortest Job First algorithm
long long ans = , end = ;
priority_queue<Rec, vector<Rec>, Cmp> q; int i = ;
while ( i < n || !q.empty())
{
if (q.empty()) // some gap with NO customers
{
end = max(end, (long long)(in[i].start));
}
// add all arrived customers
while(i < n && in[i].start <= end)
{
q.push(in[i]);
i ++;
}
Rec r = q.top();
end += r.duration;
ans += end - r.start;
q.pop();
}
cout << ans / n << endl;
return ;
}

HackerRank "Minimum Average Waiting Time" !的更多相关文章

  1. HackerRank "Minimum Penalty Path"

    It is about how to choose btw. BFS and DFS. My init thought was to DFS - TLE\MLE. And its editorial ...

  2. Java性能提示(全)

    http://www.onjava.com/pub/a/onjava/2001/05/30/optimization.htmlComparing the performance of LinkedLi ...

  3. fio2.1.10--HOWTO

    1.0 Overview and history    ------------------------ fio was originally written to save me the hassl ...

  4. 【OS】NMON的简介和使用

    [OS]NMON的简介和使用 目前NMON已开源,以sourceforge为根据地,网址是http://nmon.sourceforge.net. 1. 目的 本文介绍操作系统监控工具Nmon的概念. ...

  5. 数据库每分钟运行监控SQL

    每1分钟运行一次,记录正在运行的SQL,监控数据 放在ReportServer库的t_WhoIsActive表中,保留最近30天的数据! USE [ReportServer] GO /****** O ...

  6. Gym - 101845K 排序+概率

    The UNAL programming coaches have lost a bet, they bet the 6 UNAL teams would occupy the first six p ...

  7. Uniform synchronization between multiple kernels running on single computer systems

    The present invention allocates resources in a multi-operating system computing system, thereby avoi ...

  8. Erlang C1500K长连接推送服务-内存

    上篇 Erlang C1500K长连接推送服务-性能 提到:150w连接,使用了23GB内存,每个连接占用15KB,约一半是内核使用. 大概分析一下: 1. Erlang 节点 12GB,内部因为有内 ...

  9. What is /proc/slabinfo?

    /proc/slabinfo gives information about memory usage on the slab level. Linux kernels uses slab pools ...

随机推荐

  1. metronic后台模板学习 -- 所用外部插件列表

    插件名称 描述 URL jQuery 1.11.0 js库,不用介绍了 http://www.jquery.com jQuery Migrate plugin 1.2.1 jQuery 老版本过渡迁移 ...

  2. HDFS+MapReduce+Hive+HBase十分钟快速入门

    1.     前言 本文的目的是让一个从未接触Hadoop的人,在很短的时间内快速上手,掌握编译.安装和简单的使用. 2.     Hadoop家族 截止2009-8-19日,整个Hadoop家族由以 ...

  3. C语言常用排序全解(转)

    目的:重温经典排序思想,并用C语言指针实现排序算法================================================*/ /*====================== ...

  4. Hive cli源码阅读和梳理

    对Cli的重新认识*). hive cli有两种模式, 本地模式: 采用持有的driver对象来处理, 远程模式: 通过连接HiveServer来实现, 由此可见之前的架构图中的描述还是模糊且带有误导 ...

  5. php 滑动验证码

    自己研究: jQuery拖拽滑动验证码插件 slideunlock.js 原理:(别人说) 响应时间,拖拽速度,时间,位置,轨迹,重试次数等.这些因素能够构成一个采样结果或者辨识特性. 只获取到滑动时 ...

  6. ehcache 缓存

    一:详细配置步骤 1,添加ehcache.xml文件 将ehcache.xml文件添加到src路径下面.ehcache.xml文件内容如下 <ehcache> <diskStore  ...

  7. magic矩阵 分类: 数学 2015-07-31 22:56 2人阅读 评论(0) 收藏

    魔方矩阵 魔方矩阵是有相同的行数和列数,并在每行每列.对角线上的和都相等.你能构造任何大小(除了2x2)的魔方矩阵. 1.历史       魔方又称幻方.纵横图.九宫图,最早记录于我国古代的洛书.据说 ...

  8. Discuz论坛搭建过程

    1.系统环境 操作系统版本:CentOS Linux  5.7 内核版本:2.6.18-274.el5 arch:x86_64 apache版本:Apache/2.4.6 (Unix) mysql版本 ...

  9. 关于ZF2中一点感悟,service_manager

    在zf2中,在serviceLoctor中自定义的内容,可以通$serviceLocator->get('config')['key'],如果是在serivce_manger中定义的服务名,其实 ...

  10. Python-描述符

    Python中包含了许多内建的语言特性,它们使得代码简洁且易于理解.这些特性包括列表/集合/字典推导式,属性(property).以及装饰器(decorator).对于大部分特性来说,这些“中级”的语 ...