-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtug-of-war.cpp
148 lines (124 loc) · 3.07 KB
/
tug-of-war.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include <iostream>
#include<stdlib.h>
#include <queue>
#include <cmath>
using namespace std;
//struct definition of team information
typedef struct team {
char name;
int number;
int weight;
//make sure sub amount less than 1
//lager amount team give lightest player to the other
friend bool operator <(team t1, team t2) {
return t1.weight>t2.weight; //lighter priority
}
}players;
//array record the number and weight
const int W = 5000;
const int N = 100;
int arr[N + 1][W + 1];
int maxWeight(int a, int b) {
return a>b ? a : b;
}
int main()
{
int pnum, pwgt;
int sum = 0; //total weight
cout << "please enter the amount of player:" << endl;
cin >> pnum;
cout << "please enter the weight of each player:" << endl;
//new the players
players *tPlayer = new players[pnum + 1];
//initialize the team information
for (int i = 1; i <= pnum; i++) {
int w = 0;
cin >> w; //weight
//tPlayer[i].name = 'A';
tPlayer[i].number = i;
tPlayer[i].weight = w;
tPlayer[i].name = 'B';
sum += w;
}
int aver = sum / 2;
//initialize the array first row and column
for (int j = 0; j <= N; j++) arr[j][0] = 0;
for (int j = 0; j <= W; j++) arr[0][j] = 0;
for (int i = 1; i<=pnum; i++) {
for (int j = 1; j<= aver; j++)
{
arr[i][j] = arr[i - 1][j];
if (tPlayer[i].weight <= j) {
arr[i][j] = maxWeight(arr[i][j], arr[i - 1][j - tPlayer[i].weight] + tPlayer[i].weight); //forum
}
}
}
//pick player of team a
int rest = aver;
for (int i = pnum; i >= 1; i--)
{
if ( rest>=tPlayer[i].weight )
{
if ((arr[i][rest] - arr[i - 1][rest - tPlayer[i].weight]) == tPlayer[i].weight)
{
tPlayer[i].name = 'A';
rest = rest - tPlayer[i].weight;
}
}
}
//result after round one
//for(int i=1;i<=pnum;i++){
// cout<<"team"<<tPlayer[i].name;
// cout<<"Number"<<tPlayer[i].number<<" "<<"Weight"<<tPlayer[i].weight<<endl;
// }
//new players
priority_queue<team> teamA;
priority_queue<team> teamB;
//evaluate the team players
for (int i = 1; i <= pnum; i++) {
if (tPlayer[i].name == 'A') {
teamA.push(tPlayer[i]);
}
else
teamB.push(tPlayer[i]);
}
int countA = teamA.size();
int countB = teamB.size();
//make sure the sub less than 1
while (abs(countA - countB)>1) {
if (countA>countB)
{
teamB.push(teamA.top()); //give the lightest to B
teamA.pop();
countA--;
countB++;
}
if (countA<countB)
{
teamA.push(teamB.top()); //give the lightest to A
teamB.pop();
countA++;
countB--;
}
}
int sumA = 0;
cout << "team A" << endl;
while (!teamA.empty()) {
cout << "Number" << teamA.top().number << " " << "Weight" << teamA.top().weight << endl;
sumA += teamA.top().weight;
teamA.pop();
}
cout << "team A total weight" << sumA << endl;
int sumB = 0;
cout << "team B" << endl;
while (!teamB.empty()) {
cout << "Number" << teamB.top().number << " " << "Weight" << teamB.top().weight << endl;
sumB += teamB.top().weight;
teamB.pop();
}
cout << "team B total weight" << sumB << endl;
cout << "The SUB of team A and B is:" << abs(sumA - sumB) << endl;
delete[] tPlayer;
system("pause");
return 0;
}