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. JS中offsetLeft,Left,clientLeft的区别(纯转贴)

    假设 obj 为某个 HTML 控件. obj.offsetTop 指 obj 相对于版面或由 offsetParent 属性指定的父坐标的计算上侧位置,整型,单位像素. obj.offsetLeft ...

  2. (实用篇)微信网页授权(OAuth2.0) PHP 源码简单实现

    提要: 1. 建议对OAuth2.0协议做一个学习. 2. 微信官方文档和微信官网工具要得到充分利用. 比较简单,直接帖源代码了.其中"xxxxxxxxxx"部分,是需要依据自己环 ...

  3. 最小二乘法 python实现

    #-*-coding:UTF-8-*- # Created on 2015年10月20日 # @author: hanahimi import numpy as np import random im ...

  4. rsync 使用示例

    导读 Rsync(remote sync) 是用于同步某一位置文件和目录到另一位置的有效方法.备份的位置可以在本地服务器或远程服务器.本站之前亦有介绍rsync的安装配置和教程,详看<rsync ...

  5. left join测试验证之一

    $ sqlite3 a.dbSQLite version 3.8.1 2013-10-17 12:57:35Enter ".help" for instructionsEnter ...

  6. uva 11020 - Efficient Solutions ——平衡BST

    链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&am ...

  7. poj3159 最短路(差分约束)

    题意:现在需要分糖果,有n个人,现在有些人觉得某个人的糖果数不能比自己多多少个,然后问n最多能在让所有人都满意的情况下比1多多少个. 这道题其实就是差分约束题目,根据题中给出的 a 认为 b 不能比 ...

  8. 【P1203】买花

    我先在已经弱到连高精乘单精都能写错的地步了QAQ 原题: 求一个小于等于N的数M,使得phi(M)/M最小,其中phi(M)是与M互质且比M小的数的个数.例如phi(4)=2,因为1,3和4互质. N ...

  9. C语言 a和&a的区别

    节选自<C语言深度剖析> 首先看个例子 main() { int a[5]={1,2,3,4,5}; int *ptr=(int *)(&a+1); printf("%d ...

  10. (转) Lua string 操作函数

    本文转自: http://www.cnblogs.com/newlist/p/3649388.html table.keys 返回指定表格中的所有键. 格式: keys = table.keys(表格 ...