码迷,mamicode.com
首页 > 其他好文 > 详细

HackerRank - "Team Formation"

时间:2015-06-01 13:09:26      阅读:132      评论:0      收藏:0      [点我收藏+]

标签:

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;
}

HackerRank - "Team Formation"

标签:

原文地址:http://www.cnblogs.com/tonix/p/4543483.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!