HackerRank "Minimum Average Waiting Time" !
2015-11-19 07:20
971 查看
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.
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 = 0; 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 = 0, end = 0; priority_queue<Rec, vector<Rec>, Cmp> q; int i = 0; 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 0; }
相关文章推荐
- MVC中使用WebMail 发送注册验证信息
- 职责链模式(Chain of Responsibility Pattern)
- Genymotion 2.5.4: Unable to create virtual device : Failed to import OVA file
- CrossDomain.xml的作用及其简单用法
- 窥探 kernel --- 初识进程调度器 http://blog.csdn.net/muge0913/article/details/7561323
- OCR / Vote disk Maintenance Operations: (ADD/REMOVE/REPLACE/MOVE) (Doc ID 428681.1)
- su: cannot set user id: Resource temporarily unavailable
- P2P system:How Chord tackles failures
- SPF记录详解
- git Updates were rejected because the remote contains work that you do 解决办法
- Sprint 1 Review & Daily Scrum - 11/18
- http://blog.csdn.net/shimiso/article/details/21830313
- A3 NO SELECT The login is O EXAMINE The login is not saf your mail client: http://mail.163.com/dashi
- POJ 3250 Bad Hair Day
- 深入理解Linux内核-第七章 进程调度 http://blog.csdn.net/sailor_8318/article/details/2460177
- 我目前工作的综述
- saltstack数据系统之Grains
- 为什么 main 方法是 public static void?
- Data-only container madness
- How to schedule task daily + onStart() in Play 2.0.4?