- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
基本上,我总共有20个玩家,他们每个人有2个属性;
{
"name": "John Doe",
"goals": 20
}
我想要完成的是将球员名单和他们的目标输入到一个算法中,该算法将团队平均分为 2 个团队,但根据他们的目标进行,并以尽可能小的差异来平衡团队可能的。玩家数量总是能被 2 整除,所以这没问题!
例如,假设我们在一个数组/集合中有 4 个(为简洁起见)玩家;
通过算法后,这将产生 2 个独立的数组/集合;
| Team 1 | Team 2 |
|---------------|---------------|
| John Doe | Jane Doe |
| James Doe | Mark Doe |
| | |
| Avg: 4 Goals | Avg: 4 Goals |
| | |
| Goal Total: 8 | Goal Total: 8 |
显然,有时两支球队并不完全相等,但理想情况下,我希望在这种情况下尽可能接近。例如,第 1 队有 8 个目标,第 2 队有 7 个目标。
最佳答案
我之前在评论中提到贪婪的解决方案(在对输入进行排序之后)会起作用。但正如 @Prune
和其他人所指出的,贪婪的解决方案在这里不起作用,而是需要动态规划解决方案。以下是我的实现是C++
:
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <numeric>
#include <sstream>
#include <fstream>
class Pair{
public:
int difference;
std::vector <int> solution;
Pair(){
difference = -1;
solution.clear();
}
Pair(int d, std::vector <int> v) : difference(d), solution(v){}
bool operator < (const Pair &p) const{
return difference < p.difference;
}
};
class Player{
public:
std::string name;
int goals;
Player() = default;
Player(std::string n, int g) : name(n), goals(g) {}
friend std::ostream& operator << (std::ostream& out, const Player &player);
};
std::ostream& operator << (std::ostream& out, const Player &player){
out << "Player Name: " << player.name << " Player Goals: " << player.goals;
return out;
}
constexpr int MAX_PLAYER = 6;
constexpr int MAX_GOAL = 101;
constexpr int INF = 1e9;
Pair dp[MAX_PLAYER + 5][MAX_GOAL + 5];
Pair partition(std::vector <Player> players, int index, int val, std::vector <int> result, int sum){
if(index == players.size() || result.size() == (players.size() / 2)){
int oppostion = sum - val;
if(result.size() == players.size() / 2) return Pair(std::abs(val - oppostion), result);
return Pair(INF, std::vector <int>()); // return a high difference + empty vector
}
if(dp[index][val].difference != -1) {
return dp[index][val];
}
Pair ret1 = partition(players, index + 1, val, result, sum);
result.push_back(index);
Pair ret2 = partition(players, index + 1, val + players[index].goals, result, sum);
//std::cout << " index " << index << " val " << val << "\n";
return dp[index][val] = (ret1 < ret2 ? ret1 : ret2);
}
std::vector <Player> readInput(std::ifstream &ifs){
std::vector <Player> players;
std::string inputLine;
while(std::getline(ifs, inputLine)){
std::istringstream iss(inputLine);
std::string name;
int goals;
iss >> name >> goals;
players.emplace_back(name, goals);
}
return players;
}
auto accumulate_func = [](int accumulator, const Player& player){
return accumulator + player.goals;
};
int main(int argc, char const *argv[])
{
//freopen("out.txt", "w", stdout);
std::ifstream ifs("in.txt");
std::vector <Player> players = readInput(ifs);
int sumGoals = std::accumulate(players.begin(), players.end(), 0, accumulate_func);
std::cout << "Total Goals: " << sumGoals << "\n";
Pair ret = partition(players, 0, 0, std::vector <int> (), sumGoals);
std::cout << "Optimal goal difference " << ret.difference << '\n';
std::vector <Player> teamA;
std::vector <Player> teamB;
std::vector <int> teamAIndices;
for(int index : ret.solution){
teamAIndices.push_back(index);
teamA.push_back(players[index]);
}
for(int i = 0, k = 0 ; i < players.size() ; ++i){
if(i == teamAIndices[k]){
// this player has already been considered
k++;
} else{
teamB.push_back(players[i]);
}
}
std::cout << "-----Team A-----\n";
for(const Player &player : teamA){
std::cout << player << "\n";
}
std::cout << "\n";
std::cout << "-----Team B-----\n";
for(const Player &player : teamB){
std::cout << player << "\n";
}
int goalsTeamA = std::accumulate(teamA.begin(), teamA.end(), 0, accumulate_func);
int goalsTeamB = std::accumulate(teamB.begin(), teamB.end(), 0, accumulate_func);
std::cout << "\nGoals of Team A: " << goalsTeamA << " Goals of Team B: " << goalsTeamB << "\n";
std::cout << "Average goals of Team A: " << goalsTeamA / static_cast <double> (teamA.size()) << " Average goals of Team B: " << goalsTeamB / static_cast <double> (teamB.size()) << "\n";
return 0;
}
输入:
John_Doe 10
Jane_Doe 15
James_Doe 7
Mark_Doe 25
X_Doe 9
Y_Doe 31
Z_Doe 3
A_Doe 1
输出:
Total Goals: 101
Optimal goal difference 1
-----Team A-----
Player Name: John_Doe Player Goals: 10
Player Name: X_Doe Player Goals: 9
Player Name: Y_Doe Player Goals: 31
Player Name: A_Doe Player Goals: 1
-----Team B-----
Player Name: Jane_Doe Player Goals: 15
Player Name: James_Doe Player Goals: 7
Player Name: Mark_Doe Player Goals: 25
Player Name: Z_Doe Player Goals: 3
Goals of Team A: 51 Goals of Team B: 50
Average goals of Team A: 12.75 Average goals of Team B: 12.5
我在玩家的名字和姓氏之间加了下划线,因为我不想过多地处理输入解析(由空格分隔的可变数量的名称部分......)。您可以将 _
替换为空格。
对于帖子中提到的输入,它给出:
Total Goals: 16
Optimal goal difference 0
-----Team A-----
Player Name: John_Doe Player Goals: 5
Player Name: James_Doe Player Goals: 3
-----Team B-----
Player Name: Jane_Doe Player Goals: 1
Player Name: Mark_Doe Player Goals: 7
Goals of Team A: 8 Goals of Team B: 8
Average goals of Team A: 4 Average goals of Team B: 4
@Prune
在评论中给出的输入:
John_Doe 20
Jane_Doe 12
James_Doe 10
Mark_Doe 8
X_Doe 6
Y_Doe 5
Z_Doe 4
A_Doe 3
输出:
Total Goals: 68
Optimal goal difference 0
-----Team A-----
Player Name: Jane_Doe Player Goals: 12
Player Name: James_Doe Player Goals: 10
Player Name: Mark_Doe Player Goals: 8
Player Name: Z_Doe Player Goals: 4
-----Team B-----
Player Name: John_Doe Player Goals: 20
Player Name: X_Doe Player Goals: 6
Player Name: Y_Doe Player Goals: 5
Player Name: A_Doe Player Goals: 3
Goals of Team A: 34 Goals of Team B: 34
Average goals of Team A: 8.5 Average goals of Team B: 8.5
根据输入更改MAX_PLAYER
和MAX_GOALS
。
关于algorithm - 如何根据变量将 1 个集合平衡到多个其他集合中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57555277/
编辑:我似乎问错了这个问题。 我正在尝试寻找一种方法来查询一个集合是否在另一个集合中可用。例如: SELECT * FROM something WHERE (1, 3) IN (1, 2, 3, 4
这两种方法似乎 produce the same results ,但我一直很难真正说服人们第二种方法有效,因为它显然并不为人所知。 // Create some data var foo = { '
我一直在学习Kotlin,并且遇到过Collections API。在Kotlin之前,我一直在学习Java,并且我知道Java中有很多不同类型的Collections API。例如,我们使用List
为什么我会得到不同的行为: Collection col2 = new ArrayList(col); 集合 col2 = new ArrayList(); col2.addAll(col) 我正在与
所以我有一个代表专辑信息的 JSON 对象。给定“function updateRecords(id, prop, value)”我希望能够更新每个条目。正确的完成代码如下。 我得到了指示,粗体部分,
我想存储一个对象集合,这些对象根据它们所代表的值进行键控。这些键可以重复。例如: [4] => Bob [5] => Mary [5] => Sue [9] => Steve [10] =>
在检查 ArrayList API 时,我注意到一些看起来很奇怪的东西。 确实,这里是 ArrayList 构造函数实现,其中 Collection 作为参数传递: public ArrayList(
我正在为 API 编写一个 swagger 定义文件。 API 是用于 GET 请求的 /path/to/my/api: get: summary: My Custom API d
我知道scala.collection包中有两个非常有用的对象,可以帮助我们实现这个目标: JavaConverters(如果我想明确说明并准确说明我要转换的内容) JavaConversions(如
我已经阅读了无数其他帖子,但似乎无法弄清楚发生了什么,所以是时候寻求帮助了。 我正在尝试将包含集合的域实体映射到也包含集合的 dtos。 这是一个原始示例; (我提前为代码墙道歉,我尽量保持简短):
我正在创建一个具有 ArrayList 的类,因此当我调用构造函数时,它会初始化该数组: public class ElementsList { private ArrayList list;
我正在阅读事件指南和指南的开头,它说: You can also add an event listener to any element in the this.$ collection using
我是 Python 新手,想知道如何使用键在字典中存储不同数据类型的列表 例如 - {[Key1,int1,int1,String1] , [Key2,int2,int2,String2], [Key
int[] mylist = { 2, 4, 5 }; IEnumerable list1 = mylist; list1.ToList().Add(1); // why 1 does not get
我在 UI 表单中的每一行之后将以下内容添加到 HashMap 集合中 声明 Map> map = new HashMap>(); List valSetOne = new ArrayList();
我正在开发我的第一个 Java 项目,我有一个问题。问题应该很简单(虽然代码不是那么短,但没有理由被吓倒:))。我创建了一个基本的角色扮演游戏,并且有一个定义每个角色的抽象类“Character”。在
我正在开发一款应用程序,可以为用户收集推文、Facebook 状态和 Facebook 照片。目前,用户确切地设定了他们希望这种收获发生的时间和时间,并且蜘蛛会在此期间拉取数据。 when 和 to
有谁知道在 C# 中是否有与 Java 的 Set 集合等效的好方法?我知道您可以通过填充但忽略值来使用 Dictionary 或 HashTable 在某种程度上模仿集合,但这不是一种非常优雅的方式
EXISTS 该函数返回 集合中第一个元素的索引,如果集合为空,返回NULLNULLNULL Collecti
RDF集合是通过属性 rdf:parseType="Collection" 来描述仅包含指定成员的组 rdf:parseType="Collection" 属
我是一名优秀的程序员,十分优秀!