2015网易游戏笔试题04

3 篇文章 0 订阅
3 篇文章 0 订阅

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

扫码关注作者,定期分享技术、算法类文章
这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值