Difficult Player Grouping(2015网易游戏笔试题)

Question

时间限制: 20000ms
单点时限: 2000ms
内存限制: 256MB

描述

Battle to the West (乱斗西游) is a MOBA(multiplayer online battle arena) mobile game developed by Netease Games. It has grown fast in popularity since the release at Oct 2014. And for its high quality and popularity, it was named “AppStore Best of 2014” by Apple.

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.

提示

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

  2. 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.

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

  4. 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

My Solution

#include 
#include 
#include 
#include 
#include 

using namespace std;

void adjust(vector& v, int idx)
{
	while (idx > 0)
	{
		if (v[idx] < v[idx - 1])	// jiaohuan
		{
			int tmp = v[idx];
			v[idx] = v[idx - 1];
			v[idx - 1] = tmp;
		}
		else
		{
			break;
		}
	}
}

long long groups(vector& v)
{
	sort(begin(v), end(v));
	int len = v.size();
	long long count = 0;;
	if (len < 3) return 0;

	while ((len = v.size()) >= 3)
	{
		vector::iterator iter = v.end() - 3;

		count++;
		*iter = *iter - 1;
		*(iter + 1) = *(iter + 1) - 1;
		*(iter + 2) = *(iter + 2) - 1;

		// 删除并重新排序
		if (*(iter + 2) <= 0) v.erase(iter + 2);
		else if (*(iter + 1) <= 0)
		{
			v.erase(iter + 1);
			v.erase(iter);
			adjust(v, v.size() - 1);
		}
		else if (*iter <= 0)	
		{
			v.erase(iter);
			adjust(v, v.size() - 2);
			adjust(v, v.size() - 1);
		}	
	}
	return count / 2;
}

int main()
{
	int t, countH;
	vector> lines;
	cin >> t;
	for (int i = 0; i < t; ++i)
	{
		cin>>countH;
		vector line;
		for (int j = 0; j < countH; ++j)
		{
			int tmp;
			cin >> tmp;
			line.push_back(tmp);
		}
		lines.push_back(line);
	}

	for (auto& line : lines)
	{
		cout << groups(line) << endl;
	}
    return 0;
}



你可能感兴趣的:(编程,算法)