題目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;
}
掃碼關注作者,定期分享技術、算法類文章