C# - 具有评级的公平团队分配
本文关键字:团队 分配 | 更新日期: 2023-09-27 18:34:39
我有一个运动中球员的排名列表,并希望将它们分配给球队,以便评级的分布尽可能公平(即淘汰拥有大量高评分球员的球队,反之亦然(。
目前我正在这样做,但它似乎没有给出最佳解决方案:
ratingList.Sort();
ratingList.Reverse();
var team1List = ratingList.Where((r, i) => i % 2 != 0).ToList();
var team2List = ratingList.Where((r, i) => i % 2 == 0).ToList();
按降序对它们进行排序后,请尝试此操作
var team1List = ratingList.Where((r, i) => i % 2 != 0).ToList();
var team2List = ratingList.Where((r, i) => i % 2 == 0).ToList();
对于最佳解决方案,我们的想法是并排考虑两次ratingList
ascending
vs descending
并采取上半场(另一个只是镜像(
Ex:
0, 1, 2, 3, 4 | 5, 6, 7, 8, 9
9, 8, 7, 6, 5 | 4, 3, 2, 1, 0
and keep the first half
team1 team2 team1 team2 |
0, 1, 2, 3, | 4 team1
9, 8, 7, 6, | 5 team2
偶数在team1
,赔率在team2
。如果我们有偶数对,最后一对将在两个团队之间重新分配(另请注意,这仅适用于ratingList >= 4
(由您处理更少的费用(。同样对于偶数个评级,我建议排除中间评级,稍后决定如何处理它。
考虑到上述所有因素,解决方案应如下所示
ratingList.Sort();
var count = ratingList.Count();
// if even number of players, keep aside the one in the middle (as rating)
int? middle = null;
if (count % 2 != 0)
{
middle = ratingList[count / 2];
ratingList.RemoveAt(count / 2);
}
var ratingListDesc = ratingList.OrderByDescending(i => i).ToList();
var half = count / 2;
var take = half % 2 != 0 ? half - 1 : half;
var team1List = ratingList.Take(take).Where((r, i) => i % 2 == 0).ToList();
team1List.AddRange(ratingListDesc.Take(take).Where((r, i) => i % 2 == 0));
var team2List = ratingList.Take(take).Where((r, i) => i % 2 != 0).ToList();
team2List.AddRange(ratingListDesc.Take(take).Where((r, i) => i % 2 != 0));
// we just have to redistribute the remaining pair between each team
if (half % 2 != 0)
{
team1List.Add(ratingList[half - 1]);
team2List.Add(ratingListDesc[half - 1]);
}
if (middle.HasValue)
{
// do something, or not ...
}