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. less 快捷操作

    查找操作: /pattern    向前查找包含pattern的行 ?pattern 向后查找包含pattern的行 n 查找下一个pattern 的行 N 查找上一个pattern的行 ESC-u ...

  2. Brainstorm in one sentence

    [1]佚名|台湾学生占领立法院 人會長大三次 第一次是發現世界不是為自己而轉的時候. 第二次是在發現即使再怎麼努力,終究還是有些事令人無能為力的時候. 第三次是在,明知道有些事可能會無能為力,但還是會 ...

  3. 使用Hibernate命名查询

    HQL查询支持将查询所用的HQL语句放入配置文件中,而不是代码中,在Hibernate映射文件的<hibernate-mapping>元素中使用<query>子元素来定义命名查 ...

  4. 2层Folder删除问题,父文件夹删不掉

    在此用的是由内向外删除.文件结构是:父文件夹/子文件夹/文件.用的是java1.6的java.io.FIle#deleteFile(); 在删除的过程中,发现,文件删除的时候没有问题,但是在子文件夹删 ...

  5. POJ-1741 Tree (树上点分治)

    题目大意:一棵带边权无根树,边权代表距离,求距离小于等于k的点对儿数. 题目分析:这两个点之间的路径只有两种可能,要么经过根节点,要么在一棵子树内.定义depth(i)表示点 i 到根节点的距离,be ...

  6. 深入理解html5系列-文本标签

    转:http://blog.csdn.net/lihui130135/article/details/45150501 文章简介:       关于html5相信大家早已经耳熟能详,但是他真正的意义在 ...

  7. DuiLib——第二篇UIBase

    ---------------------------------------------------------------------------------- 分析约定: private o-- ...

  8. dom4j测试

    book.xml <?xml version="1.0" encoding="UTF-8"?><books><book>&l ...

  9. C#脱离Halcon编程开发环境使用方法

    在没有安装Halcon开发程序(HDevelop (SSE2))的电脑上面编程,使C#脱离Halcon编程开发环境使用方法,除了按照Halcon与编程环境必须要做的设置步骤外,还需要做如下两个工作: ...

  10. 基于OkHttp的封装库TigerOkHttp的使用

    在前面熟悉了OkHttp的用法之后,为了简化用法同时适用于我的项目,我针对OkHttp进行了更进一步的封装(源码及其Demo地址在https://github.com/huyongli/TigerOk ...