这道题目居然是分组,那么我们就分组,用一个unordered_map来模拟组,其k值是一个组在中需要的能力值,必如一个组中的人为1、2、3,那么这个组中需要的值为4,对应的key值为4,其value为一个小根堆优先队列,队列里的数值为一个组的人数;
具体步骤如下,我们接收数据后,按能力值按从小到大排序;
记unordered_map为ha,st[i]为第i个人的能力值;
所以ha[k].top为需要能力为k的队伍中人数最小的队伍
我们从0~n-1遍历每个人;
(1)如果ha[ st[i] ]对应的优先队列不为空,那么我们就将其pop堆顶,在这之前我们用一个t记录top,然后ha[ st[i]+1 ] .push(t+1)
(2)如果为空,那么创建一个ha[st[i]+1];
最后我们遍历每一个优先队列的top记录最小值即可
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<unordered_map>
using namespace std;
const int N=100010;
vector<int> list;
int n;
int ans=1e9;
unordered_map<int,priority_queue<int,vector<int>,greater<int>>> ha;
int main(){
cin>>n;
for(int i=1;i<=n;i++){
int x; cin>>x;
list.push_back(x);
}
sort(list.begin(),list.end());
for(int i=0;i<n;i++){
if(ha[list[i]].size()){
int t=ha[list[i]].top();
ha[list[i]].pop();
ha[list[i]+1].push(t+1);
}else{
ha[list[i]+1].push(1);
}
}
for(auto t:ha){
if(t.second.size())
ans=min(t.second.top(),ans);
}
cout<<ans;
}
本文介绍了一种使用unordered_map和优先队列实现的分组匹配算法,该算法通过排序能力和匹配需求来有效地完成团队构建任务。
593

被折叠的 条评论
为什么被折叠?



