其他分享
首页 > 其他分享> > N-Finding Team Member

N-Finding Team Member

作者:互联网

题意:

一共有2n个人给(2n-1)*2n/2 对组合值,如果 A 和 B 两个人都是另一个最好的队友(在未配对的参赛者中),那么他们可以组成一个团队,最好的队友表示组合值最大。

题解:

官方
Sort all possible combinations from high strength to low strength. Then iterator all combinations. If two people in a combination still are not contained in any team. then we make these two people as a team.
题意比较难懂,弄懂题意以后,把每个人看成一个点,组合值看成一条表,可以发现每两个人都是联通的且这是无向边(完全图),对于每个人输出自己最好的队友,我们不妨从全局考虑,假设现在没有队伍搭配,那么哪一条边是优先被连的呢?显然是权值最大的一条边,这样两个人一定是最好的队友,随后这两个人就搭配完成,去除这两个人,再去除这一条边,剩下的就是判断次大边是否可连,如果这条边的一个端点已经被去除,意味着这条边是无效的,由于是完全图依次枚举边最终一定可以搭配完成。那么这题我们只需要存下边的信息,包括端点和权值,然后进行结构体排序即可。

#include<bits/stdc++.h>
using namespace std;
struct node {
	int a, b,w;
}e[1000000];
int st[10000];//标记点是否搭配完成
bool cmp(node x, node y)
{
	return x.w > y.w;
}
int main()
{
	int n;
	cin >> n;
	int cnt = 0;
	for(int i=2;i<=2*n;i++)
		for (int j = 1; j <= i-1;j++)
		{  
			int a;
			cin >> a;
			e[cnt].a = i, e[cnt].b = j, e[cnt].w = a;
			//cout << e[cnt].a << " " << e[cnt].b << endl;
			cnt++;
			
		}
	sort(e, e + cnt, cmp);
	
	for (int i = 0; i < cnt; i++)
	{
		if (!st[e[i].a] && !st[e[i].b])//如果两个点都未搭配,则为互相的最佳队友
		{
			st[e[i].a] = e[i].b;
			//cout << e[i].a << " " << e[i].b << endl;
			st[e[i].b] = e[i].a;
		}
	}
	for (int i = 1; i <=2* n; i++)
		cout << st[i] << " ";
}

标签:cnt,题意,int,st,Member,搭配,队友,Team,Finding
来源: https://blog.csdn.net/thexue/article/details/122031956