0%

ACM ICPC 2016北京赛站网络赛 D - Pick Your Players

Description

You are the manager of a small soccer team. After seeing the shameless behavior of your team during the match, you are mad at all of the current players. Therefore, you have made a huge decision: put these players on the substitution bench, and buy the entire starting line-up from other teams.

You have received a list of available players from your assistant manager. Each player has three properties: Position, Value, and Cost. The Position of a player is one of the four kinds: Goalkeeper, Defender, Midfielder and Forward. The Value shows the ability of the player (the higher, the better), and the Cost is the money you need to spend to buy him.

You are going to pick some players to buy from the list, in order to form the starting line-up. Several rules should be followed:

  1. The starting line-up consists of exactly eleven players.

  2. There should be exactly one Goalkeeper, at least three but at most five Defenders, at least two but at most five Midfielders, and at least one but at most three Forwards in the starting line-up.

  3. There should be exactly one captain in the starting line-up.The captain must be chosen from the eleven players.

  4. The total value of the starting line-up is the sum of the Values of picked players plus the Value of the captain. In another word, the Value of the captain is counted twice.

  5. The total cost of the starting line-up is the sum of the Costs of picked players, which should not exceed the given cost limitation.

Now you have to give a plan to your boss before taking the actual actions. In the plan, you should report three numbers which your boss really interests in: Vt, Ct, N, where Vt is the maximum total value you can get; Ct is the minimum total cost when the total value is Vt; And N is the number of different ways to pick players when the total value is Vt and the total cost is Ct. Since your boss does not care the precise number of N if it is larger than 1,000,000,000, just report N = 1,000,000,000 when that happens.

Note that if two or more ways that pick the same eleven players and are only different in captain chosen, they should be regarded as the same.

Input

There are several test cases in the input.

The first line contains an integer T (1 <= T <= 10) -- the number of test cases.

For each case:

The first line contains an integer M (11 <= M <= 500) -- the number of players on the list.

Then follows M lines, each line contains a string P and two integers V and C (0 <= V <= 1000, 0 <= C <= 1000), separated by a single space, describing the properties of a player. P is the position of the player, which is one of the strings “Goalkeeper”, “Defender”, “Midfielder”, and “Forward”; V is the Value of the player and C is the Cost of the player.

The last line contains an integer L (0 <= L <= 1000) -- the cost limitation.

Output

For each test case, output three integers Vt, Ct and N on a single line, separated by a single space. We assure that there is at least one possible way to pick your players.

Note

In the sample, you should pick all five Defenders, four Midfielders with Value 178, 20, 64 and 109, one Forward with Value 6, and one of two Goalkeepers with Value 57. The Midfielder with Value 178 should be the captain.

Sample

input

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
1
15
Defender 23 45
Midfielder 178 85
Goalkeeper 57 50
Goalkeeper 57 50
Defender 0 45
Forward 6 60
Midfielder 20 50
Goalkeeper 0 50
Midfielder 64 65
Midfielder 109 70
Forward 211 100
Defender 0 40
Defender 29 45
Midfielder 57 60
Defender 52 45
600
output
1
716 600 2

Key

来自ICPCCamp.Post的题解: >题意:你需要买一个足球队(11个球员),每个球员有位置、价值。花费,有以下限制: 位置分为前锋(1-3人)、中腰(2-5)、后卫(3-5)、守门员(1) 每个人有value,总的value 是每个人的value加起来 ,选一个队长,队长的加两次 每个人有个 cost,总花费不能超过给定值 求:最大的 value,相应的最小的 cost,相应的购买方案数(大于1e9输出1e9) 10组数据,500个候选人,value和cost:V and C (0 <= V <= 1000, 0 <= C <= 1000),花费上界:1000 题解:考虑dp(i,cost,j,k,r,w)=(value,way)表示考虑前ii个球员,费用和为cost,选了j个前锋kk个中腰r个后卫w个守门员这个状态,价值和是value,方案数是way。然后按照dp字面意思转移就可以了。然后第一维的ii这里是为了看起来方便,其实是不用开的。 为了方便处理队长,把球员们按照价值从大到小排序,挑的第一个人当队长就可以了。

不太清楚从如果按照题解大到小排序的话选队长为什么一定是对的(虽然它的确是这样),所以我选择从小到大排序,至少这样我会做一点。。。这样的话每个当前运动员都要考虑做一次队长,效率是差一点点。不过即便如此还是没做出来。。。。老是WA,甚至开始怀疑hihocoder上的测试数据是错的(这题有事没事debug了1个月了,老是WA,WA到怀疑人生)。 总这贴这里以后不管了。。。

Code(WA)

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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#include<algorithm>
#include<iostream>
#include<cstdio>
#include<cstring>
#define dfor(i,l,r) for(int i=l;i>=r;--i)
using namespace std;
typedef long long lld;
const int maxn = 1000;
const int maxl = 1000000000;
const int mg = 1, md = 5, mm = 5, mf = 3;
int arr[2][6][6][4][maxn][3]; // G, D, M, F , cost, value/num/captain's value


struct member {
char P;
int V, C;
}lst[maxn + 10];

int T, M;
lld L;
char ipt_tmp[16];

bool cmp(const member &arg1, const member &arg2) {
if (arg1.V == arg2.V) return arg1.C < arg2.C;
return arg1.V < arg2.V;
}

int main()
{
std::ios::sync_with_stdio(false);
cin >> T;
while (T--) {
memset(arr, 0, sizeof(arr));
cin >> M;
for (int i = 0; i != M; ++i) {
member &now = lst[i];
cin >> ipt_tmp >> now.V >> now.C;
now.P = *ipt_tmp;
}
cin >> L;
sort(lst, lst + M, cmp);
for (int i = 0; i != M; ++i) {
const member &nowp = lst[i];
if (nowp.P == 'G') { // exactly one Goalkeeper
dfor(g, mg, 1)dfor(d, md, 0)dfor(m, mm, 0)dfor(f, mf, 0) {
int(&predp)[maxn][3] = arr[g - 1][d][m][f];
int(&nowdp)[maxn][3] = arr[g][d][m][f];
if (g + d + m + f > 11) continue;
dfor(c, L, nowp.C) {
int(&nowc)[3] = nowdp[c];
int(&prec)[3] = predp[c - nowp.C];
if (prec[1] == 0) continue;
if (nowc[0] + nowc[2] <= prec[0] + nowp.V + nowp.V) {
if (nowc[0] + nowc[2] < prec[0] + nowp.V + nowp.V) {
nowc[0] = prec[0] + nowp.V;
nowc[1] = prec[1];
}
else {
if ((nowc[1] += prec[1]) > maxl) {
nowc[1] = maxl;
}
}
nowc[2] = nowp.V;
}
}
}
if (nowp.V + nowp.V >= arr[1][0][0][0][nowp.C][0] + arr[1][0][0][0][nowp.C][2] && nowp.C < L) {
if (nowp.V + nowp.V > arr[1][0][0][0][nowp.C][0] + arr[1][0][0][0][nowp.C][2]) {
arr[1][0][0][0][nowp.C][0] = nowp.V;
arr[1][0][0][0][nowp.C][1] = 1;
arr[1][0][0][0][nowp.C][2] = nowp.V;
}
else {
++arr[1][0][0][0][nowp.C][1];
}
}
}
else if (nowp.P == 'D') { // at least 3 but at most 5 Defenders
dfor(d, md, 1)dfor(g, mg, 0)dfor(m, mm, 0)dfor(f, mf, 0) {
int(&predp)[maxn][3] = arr[g][d - 1][m][f];
int(&nowdp)[maxn][3] = arr[g][d][m][f];
if (g + d + m + f > 11) continue;
dfor(c, L, nowp.C) {
int(&nowc)[3] = nowdp[c];
int(&prec)[3] = predp[c - nowp.C];
if (prec[1] == 0) continue;
if (nowc[0] + nowc[2] <= prec[0] + nowp.V + nowp.V) {
if (nowc[0] + nowc[2] < prec[0] + nowp.V + nowp.V) {
nowc[0] = prec[0] + nowp.V;
nowc[1] = prec[1];
}
else {
if ((nowc[1] += prec[1]) > maxl) {
nowc[1] = maxl;
}
}
nowc[2] = nowp.V;
}
}
}
if (nowp.V + nowp.V >= arr[0][1][0][0][nowp.C][0] + arr[0][1][0][0][nowp.C][2] && nowp.C < L) {
if (nowp.V + nowp.V > arr[0][1][0][0][nowp.C][0] + arr[0][1][0][0][nowp.C][2]) {
arr[0][1][0][0][nowp.C][0] = nowp.V;
arr[0][1][0][0][nowp.C][1] = 1;
arr[0][1][0][0][nowp.C][2] = nowp.V;
}
else {
++arr[0][1][0][0][nowp.C][1];
}
}
}
else if (nowp.P == 'M') { // at least 2 but at most 5 Midfielders
dfor(m, mm, 1)dfor(g, mg, 0)dfor(d, md, 0)dfor(f, mf, 0) {
int(&predp)[maxn][3] = arr[g][d][m - 1][f];
int(&nowdp)[maxn][3] = arr[g][d][m][f];
if (g + d + m + f > 11) continue;
dfor(c, L, nowp.C) {
int(&nowc)[3] = nowdp[c];
int(&prec)[3] = predp[c - nowp.C];
if (prec[1] == 0) continue;
if (nowc[0] + nowc[2] <= prec[0] + nowp.V + nowp.V) {
if (nowc[0] + nowc[2] < prec[0] + nowp.V + nowp.V) {
nowc[0] = prec[0] + nowp.V;
nowc[1] = prec[1];
}
else {
if ((nowc[1] += prec[1]) > maxl) {
nowc[1] = maxl;
}
}
nowc[2] = nowp.V;
}
}
}
if (nowp.V + nowp.V >= arr[0][0][1][0][nowp.C][0] + arr[0][0][1][0][nowp.C][2] && nowp.C < L) {
if (nowp.V + nowp.V > arr[0][0][1][0][nowp.C][0] + arr[0][0][1][0][nowp.C][2]) {
arr[0][0][1][0][nowp.C][0] = nowp.V;
arr[0][0][1][0][nowp.C][1] = 1;
arr[0][0][1][0][nowp.C][2] = nowp.V;
}
else {
++arr[0][0][1][0][nowp.C][1];
}
}
}
else { // at least 1 but at most 3 Forwards
dfor(f, mf, 1)dfor(g, mg, 0)dfor(d, md, 0)dfor(m, mm, 0) {
int(&predp)[maxn][3] = arr[g][d][m][f - 1];
int(&nowdp)[maxn][3] = arr[g][d][m][f];
if (g + d + m + f > 11) continue;
dfor(c, L, nowp.C) {
int(&nowc)[3] = nowdp[c];
int(&prec)[3] = predp[c - nowp.C];
if (prec[1] == 0) continue;
if (nowc[0] + nowc[2] <= prec[0] + nowp.V + nowp.V) {
if (nowc[0] + nowc[2] < prec[0] + nowp.V + nowp.V) {
nowc[0] = prec[0] + nowp.V;
nowc[1] = prec[1];
}
else {
if ((nowc[1] += prec[1]) > maxl) {
nowc[1] = maxl;
}
}
nowc[2] = nowp.V;
}
}
}
if (nowp.V + nowp.V >= arr[0][0][0][1][nowp.C][0] + arr[0][0][0][1][nowp.C][2] && nowp.C < L) {
if (nowp.V + nowp.V > arr[0][0][0][1][nowp.C][0] + arr[0][0][0][1][nowp.C][2]) {
arr[0][0][0][1][nowp.C][0] = nowp.V;
arr[0][0][0][1][nowp.C][1] = 1;
arr[0][0][0][1][nowp.C][2] = nowp.V;
}
else {
++arr[0][0][0][1][nowp.C][1];
}
}
}
}

// DP finished
int max_value = -1, min_cost = maxn, sum_num = 0;
dfor(g, mg, 1)dfor(d, md, 3)dfor(m, mm, 2)dfor(f, mf, 1) {
if (g + d + m + f != 11) continue;
dfor(c, L, 0) {
int(&now)[3] = arr[g][d][m][f][c];
//if (now[0]) {
// cerr << " val: " << now[0] + now[2] << "\tnum: " << now[1] << "\tcost: " << c << '\t';
// cerr << "g: " << g << " d: " << d << " m: " << m << " f: " << f << endl;
//}
if (now[0] + now[2] > max_value) {
max_value = now[0] + now[2];
min_cost = c;
sum_num = now[1];
}
else if (now[0] + now[2] == max_value) {
if (min_cost > c) {
min_cost = c;
sum_num = now[1];
}
else if (min_cost == c) {
sum_num += now[1];
}
}
}
}

cout << max_value << ' ' << min_cost << ' ' << sum_num << " \n"[T];
}
return 0;
}