题目4 : Difficult Player Grouping
题目大意:
一个3V3的游戏,共有H个不同的hero,每个玩家需要选择一个hero
,一次对战需要两个team,每个team有三个玩家,同时要求一个team内的三个玩家选择的hero是不同的。
现在给出 H 个hero的使用情况,H { n1 n2 n3 n4 ......... nH }
其中H表示共有H个不同的hero, n1表示共有n1个玩家选择了第一个hero。
要求出同时可以进行的最大对战数量。
样例:
样例输入
4
6 1 1 1 1 1 1
3 2 2 2
3 2 2 3
3 1 2 3
样例输出
1
1
1
0
思路:
显然这个题目也不是求组合数,仅仅看同一时间最多安排出来几个对战。 一个贪心的策略:每次取玩家数量最多的三个hero(优先级队列),
a,b,c,构成一组,将其玩家数量-1,若余量仍然>0,再放回优先级队列中。
代码:
#include <vector>
#include <map>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <queue>
#include <iostream>
using namespace std;
int main()
{
int n;
int K;
scanf("%d", &K);
int tmp;
while (K--) {
priority_queue<int> q;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", &tmp);
q.push(tmp);
}
int ans = 0;
while (q.size() >= 3) {
int a,b,c;
ans++;
a = q.top(); q.pop();
b = q.top(); q.pop();
c = q.top(); q.pop();
if (a > 1) q.push(--a);
if (b > 1) q.push(--b);
if (c > 1) q.push(--c);
}
printf("%d\n", ans / 2);
}
return 0;
}
扫码关注作者,定期分享技术、算法类文章