zoukankan      html  css  js  c++  java
  • HackerRank

    Pretty classic greedy problem to work on. Here is how to approach it:

    1. "the smallest team is as large as possible." actually means, team members should be distributed as average as possible

    2. When you are to put member with skill-level 'x', you find the team with least members (say n members), whose lowest skill-level is x + 1, and update the record.

    With statement above, you can combine hashmap and min-heap to solve it:

    #include <cmath>
    #include <cstdio>
    #include <vector>
    #include <map>
    #include <set>
    #include <string>
    #include <climits>
    #include <iostream>
    #include <algorithm>
    #include <functional>
    #include <queue>
    #include <unordered_map>
    #include <unordered_set>
    using namespace std;
    
    void go()
    {
        int n; cin >> n;
        vector<int> in(n);
        for (int i = 0; i < n; i++)
            cin >> in[i];
        std::sort(in.begin(), in.end(), std::greater<int>());
    
        //    min-value: sizes in heap
        unordered_map<int, priority_queue<size_t, std::vector<size_t>, std::greater<size_t>>> rec;
        for (auto v : in)
        {
            if (rec.find(v + 1) == rec.end())
            {
                rec[v].push(1);
            }
            else
            {
                auto it = rec.find(v + 1);
                size_t old_minsize = it->second.top();
                it->second.pop();
                if (it->second.size() == 0)
                {
                    rec.erase(v + 1);
                }
                rec[v].push(old_minsize + 1);
            }
        }
    
        //    Get min size
        size_t min_size = std::numeric_limits<size_t>::max();
        for (auto &r : rec)
            min_size = std::min(min_size, r.second.top());
        cout << (min_size == std::numeric_limits<size_t>::max() ? 0:min_size) << endl;
    }
    
    int main()
    {
        int t; cin >> t;
        while (t--)
        {
            go();
        }
        return 0;
    }
  • 相关阅读:
    做汉堡
    第三次作业:五章感想与问题
    第二次作业:结对练习
    自己要的东西
    存在不知道是什么问题的程序
    第一个Sprint冲刺第二天
    第一个Sprint冲刺第一天
    第三个Sprint完结工作 用场景来规划测试工作.
    beta 阶段的 postmortem 报告
    重新评估团队贡献分
  • 原文地址:https://www.cnblogs.com/tonix/p/4543483.html
Copyright © 2011-2022 走看看