zoukankan      html  css  js  c++  java
  • 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 = 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;
    }
  • 相关阅读:
    【STSRM12】整除
    【STSRM12】夏令营
    【BZOJ】1954: Pku3764 The xor-longest Path
    Faster-rcnn实现目标检测
    反向传播算法的理解
    人工智能发展的看法
    FCN的理解
    卷积神经网络的部分知识理解
    不常见算法的介绍
    如何对模型进行优化
  • 原文地址:https://www.cnblogs.com/tonix/p/4976473.html
Copyright © 2011-2022 走看看