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. PostgresSQL 学习资料记录处

    PostgresSQL 学习资料记录处  博客:http://francs3.blog.163.com PostgreSQL9.4 中文手册:http://www.postgres.cn/docs/9 ...

  2. mysql中模糊查询的四种用法介绍

    下面介绍mysql中模糊查询的四种用法: 1,%:表示任意0个或多个字符.可匹配任意类型和长度的字符,有些情况下若是中文,请使用两个百分号(%%)表示. 比如 SELECT * FROM [user] ...

  3. leetcode 121. Best Time to Buy and Sell Stock ----- java

    Say you have an array for which the ith element is the price of a given stock on day i. If you were ...

  4. URAL 1291 Gear-wheels(BFS)

    Gear-wheels Time limit: 1.0 secondMemory limit: 64 MB - Arny! What happened with coordinator? Bad wo ...

  5. 1029c语言文法定义与c程序的推导过程

    program → external_declaration | program external_declaration <源程序>→ <外部声明> | <源程序> ...

  6. Linux定时任务Crontab执行PHP脚本

    http://blog.chinaunix.net/uid-7552018-id-182133.html crontab执行php脚本 http://www.jb51.net/article/2913 ...

  7. 论文笔记之:Playing for Data: Ground Truth from Computer Games

    Playing for Data: Ground Truth from Computer Games ECCV 2016 Project Page:http://download.visinf.tu- ...

  8. cluster,network

    How cluster works https://technet.microsoft.com/en-us/library/cc738051(v=ws.10).aspx http://blogs.te ...

  9. C++重载,重写,重定义

    1.重载:重载是在一个类中,函数名一样,参数类型或参数个数不同的一系列函数需要注意的是,与返回类型无关. class Test { public: void show(); void show(int ...

  10. 二十四种设计模式:中介者模式(Mediator Pattern)

    中介者模式(Mediator Pattern) 介绍用一个中介对象来封装一系列的对象交互.中介者使各对象不需要显式地相互引用,从而使其耦合松散,而且可以独立地改变它们之间的交互. 示例有一个Messa ...