HackerRank "Minimum Average Waiting Time" !
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" !的更多相关文章
- 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 ...
- Java性能提示(全)
http://www.onjava.com/pub/a/onjava/2001/05/30/optimization.htmlComparing the performance of LinkedLi ...
- fio2.1.10--HOWTO
1.0 Overview and history ------------------------ fio was originally written to save me the hassl ...
- 【OS】NMON的简介和使用
[OS]NMON的简介和使用 目前NMON已开源,以sourceforge为根据地,网址是http://nmon.sourceforge.net. 1. 目的 本文介绍操作系统监控工具Nmon的概念. ...
- 数据库每分钟运行监控SQL
每1分钟运行一次,记录正在运行的SQL,监控数据 放在ReportServer库的t_WhoIsActive表中,保留最近30天的数据! USE [ReportServer] GO /****** O ...
- Gym - 101845K 排序+概率
The UNAL programming coaches have lost a bet, they bet the 6 UNAL teams would occupy the first six p ...
- Uniform synchronization between multiple kernels running on single computer systems
The present invention allocates resources in a multi-operating system computing system, thereby avoi ...
- Erlang C1500K长连接推送服务-内存
上篇 Erlang C1500K长连接推送服务-性能 提到:150w连接,使用了23GB内存,每个连接占用15KB,约一半是内核使用. 大概分析一下: 1. Erlang 节点 12GB,内部因为有内 ...
- What is /proc/slabinfo?
/proc/slabinfo gives information about memory usage on the slab level. Linux kernels uses slab pools ...
随机推荐
- 一步一步使用sklearn
http://kukuruku.co/hub/python/introduction-to-machine-learning-with-python-andscikit-learn Hello, %u ...
- 【转帖】ios上取得设备唯一标志的解决方案
原文地址:http://lqzit.iteye.com/blog/2070306 注意:keychina设置完之后,项目目录里的“项目名.entitlements”文件不是手动创建出的,而是在按照如下 ...
- UVa 10900 - So you want to be a 2n-aire?
题目大意: 一个答题赢奖金的问题,玩家初始的金额为1,给出n,表示有n道题目,t表示说答对一道题目的概率在t到1之间,每次面对一道题,可以选择结束游戏,获得当前奖金:回答下一道问题,答对的概率p在t到 ...
- Win下循环进入目录启动执行某任务
cd tldlq for /D %%s in (*) do ( cd %%s start "" LoginGate.exe start "" GameGate. ...
- javascript 基础学习教程
http://www.itxueyuan.org/javascript/jiaocheng_2/
- js 小数取整的函数
1.丢弃小数部分,保留整数部分 js:parseInt(7/2) 2.向上取整,有小数就整数部分加1 js: Math.ceil(7/2) 3,四舍五入. js: Math.round(7/2) 4, ...
- abbyy cup a
link: http://codeforces.com/contest/331/problem/A2 /* ID: zypz4571 LANG: C++ TASK: abby_a.cpp */ #in ...
- spark_updateStateByKey
java核心代码 JavaPairDStream<String, Integer> wordCounts = pair.updateStateByKey(new Function2< ...
- POJ 1195 Mobile phones(二维树状数组)
Mobile phones Time Limit: 5000MS Mem ...
- JS版百度地图API
地图的构建非常简单,官方的API文档也写得很清晰,我只做一总结: 一起jquery,17jquery 一.引入JS :这个很容易理解,既然是调用JS版的百度地图,肯定得引用外部的JS文件了,而这个文件 ...