[网易笔试]Difficult Player Grouping

题目链接:http://hihocoder.com/contest/ntest2015septdev/problem/4

The 3v3 mode is a new PvP mode in Battle to the West. In this mode, a player needs to choose a hero to play first. And then all online players are randomly grouped in sixes. Each group has exactly 6 players and corresponds to a new game. In the new game, the 6 players are further randomly divided into 2 opposing teams, each of which has 3 players.

To make the gameplay fair and fun, no two players choosing the same hero should appear in a single team. As a developer of this game, you may design the way of grouping all online players, and the way of dividing players into teams. Then a problem arises. What is the maximum number of games (or groups) that could possibly be generated from online players satisfying all above constraints?

To make the problem clear, we assume that there are H distinct heroes, named from hero1 to heroH. A player must choose exactly one from the H heroes. The number of players choosing heroi is ai. Given H and ai, you need to write a program to find the maximum number of games that could be grouped by these players.

输入
The input contains several test cases.

The first line contains T(T ≤ 100), the number of the test cases.

The following T lines each start with an integer H(1 ≤ H ≤ 1000), which is the number of possible choices for hero. Then H integers (a1 … aH) follow, where ai (ai > 0) is the number of players who choose the i-th hero. It is guaranteed that the sum of ai will no more than 200000.

输出
For each test case, please output the maximum number of games that could be grouped by the players.

提示
For the 1st test case, there are 6 online players choosing 6 different heroes. So 1 game can be formed.

For the 2nd test case, we can divide the 6 heroes into 2 teams: (1, 2, 3) and (1, 2, 3). So no 2 heroes appear in a single team, and 1 game can be formed.

There are 7 players in the 3rd test case, but a game has exactly 6 players. The extra player is in no team.

For the 4th test case, there are 3 players choosing the hero3. So no game could be formed.

样例输入
4
6 1 1 1 1 1 1
3 2 2 2
3 2 2 3
3 1 2 3
样例输出
1
1
1
0

#include <iostream>  
#include <vector>
#include <string>
#include <iomanip>
using namespace std;

bool check(int *a, int len)
{
    int n = 0;
    int i = 0;
    while (i < len && n < 3)
    {
        while (i < len && a[i] <= 0)
            ++i;
        if (i >= len)
            return false;
        a[i] -= 1;
        ++n;
        ++i;
    }
    if (n == 3)
        return true;
    else
        return false;
}

int main()
{
    int n;
    cin >> n;
    while (n > 0)
    {
        int m;
        cin >> m;
        int *a = new int[m];
        for (int i = 0; i < m; ++i)
        {
            cin >> a[i];
        }
        int i = 0;
        while (true)
        {
            if (check(a, m) && check(a, m))
                i++;
            else
                break;
        }
        cout << i << endl;
        delete a;
        --n;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值