Barry Bennett, the coach of the Bings football team, wants to arrange
his team for an important match against the Bangs. He decides on the
formation he wants to play, for example 4-4-2, meaning that there will
be four defenders, four midfielders, and two strikers (and of course,
one goalkeeper). Your task is to determine the players who will play.
For each available player, we know his role (e.g. midfielder). For each
role, the players are selected in ascending order of their numbers. When
the players are selected, you must determine the captain too, who is
the player with the longest record in the team play. In case two players
have the same record, the one with bigger number is chosen. Note that
the captain is chosen among the players that are selected in the
arrange.
The input consists of multiple test cases. The first 22 lines of each
test case contain the data for the 22 available players in this format:
number name role year1 - year'1 year2 - year'2 ...
number
is the player number (unique positive integer less than 100). name
is a string of at most 20 letters. role
is a single character among G, D, M, S, for goalkeeper, defender, midfielder, and striker respectively. Each
yeari - year'i
pair
(yeari
year'i)
shows the player has been a member of the team
between the specified years (inclusive). The years are in four-digit
format. There is at least one and at most 20 such pairs, and the same
year is not repeated more than once in the list. There is a 23-rd line
describing the desired formation, like 4-4-2 in that format.
Note that there are only three numbers in the formation (so, 4-3-2-1 is
not valid), none of them is zero, and their sum is always 10. The input
is terminated by a line containing a single `0'.
For each test case, output a list of 11 players chosen in the arrange.
Each line must contain the player number, his name and his role,
separated by single blank characters. The players must be sorted
according to their role, in the order of goalkeeper, defenders,
midfielders, and strikers. The players with the same role are sorted
according to ascending order of their numbers. There is an exception
that the captain always comes as the first player in the entire list. If
it is not possible to arrange the team conforming to the desired
formation, write a single line containing `IMPOSSIBLE TO ARRANGE' in the output. There should be a blank line after the output for each test case.
9 PlayerA M 2000-2001 2003-2006
2 PlayerB M 2004-2006
10 PlayerC D 2001-2005
1 PlayerD D 2000-2001 2002-2004
11 PlayerE S 2003-2006
8 PlayerF M 2005-2006
22 PlayerG S 2005-2006
25 PlayerH G 2000-2001 2002-2003 2005-2006
6 PlayerI D 2003-2006
26 PlayerJ D 2003-2004 2000-2001
18 PlayerK M 2003-2004
19 PlayerL M 2000-2001 2003-2006
7 PlayerM S 2003-2006 1999-2001
21 PlayerN S 2003-2006
13 PlayerO S 2005-2006
15 PlayerP G 2001-2006
14 PlayerQ D 2003-2004
5 PlayerR S 2000-2005
20 PlayerS G 2000-2002 2003-2003
12 PlayerT M 2004-2005
3 PlayerU D 2000-2005
4 PlayerV M 2001-2004
4-4-2
0
7 PlayerM S
15 PlayerP G
1 PlayerD D
3 PlayerU D
6 PlayerI D
10 PlayerC D
2 PlayerB M
4 PlayerV M
8 PlayerF M
9 PlayerA M
5 PlayerR S
題目描述:
給足球選手的所屬的位置,以及玩的年份,以及一個號碼。
然後每個位置都有需要的個數,按照號碼最小的先選,不會出現重複的。
選完後,要從中選一個隊長,隊長是從中選一個年資最大的,相同時則選擇號碼最大的。
題目解法:
模擬
#include <stdio.h>
#include <iostream>
#include <sstream>
#include <vector>
#include <algorithm>
using namespace std;
struct E {
string name;
int longest, number;
int cat;
E(string a, int b, int c, int d):
name(a), longest(b), number(c), cat(d) {}
bool operator<(const E &a) const {
return number < a.number;
}
};
bool cmp(E a, E b) {
if(a.longest != b.longest)
return a.longest > b.longest;
return a.number > b.number;
}
bool cmp2(E a, E b) {
if(a.cat != b.cat) {
if(a.cat == 'G')
return true;
if(b.cat == 'G')
return false;
return a.cat < b.cat;
}
return a.number < b.number;
}
int main() {
int i, j, k;
while(true) {
char name[105], category[2], tail[1024];
int power;
vector<E> G, D, M, S;
for(i = 0; i < 22; i++) {
scanf("%d %s %s", &power, name, category);
if(power == 0)
return 0;
gets(tail);
stringstream sin(tail);
string token;
int sum = 0;
while(sin >> token) {
int a, b;
sscanf(token.c_str(), "%d-%d", &a, &b);
sum += b-a+1;
}
if(category[0] == 'G')
G.push_back(E(name, sum, power, 'G'));
if(category[0] == 'D')
D.push_back(E(name, sum, power, 'D'));
if(category[0] == 'M')
M.push_back(E(name, sum, power, 'M'));
if(category[0] == 'S')
S.push_back(E(name, sum, power, 'S'));
}
gets(tail);
int d, m, s;
sscanf(tail, "%d-%d-%d", &d, &m, &s);
if(D.size() < d || M.size() < m || S.size() < s || G.size() < 1) {
puts("IMPOSSIBLE TO ARRANGE\n");
continue;
}
sort(G.begin(), G.end());
sort(D.begin(), D.end());
sort(M.begin(), M.end());
sort(S.begin(), S.end());
vector<E> selected;
selected.push_back(G[0]);
for(i = 0; i < d; i++)
selected.push_back(D[i]);
for(i = 0; i < m; i++)
selected.push_back(M[i]);
for(i = 0; i < s; i++)
selected.push_back(S[i]);
sort(selected.begin(), selected.end(), cmp);
E captain = selected[0];
printf("%d %s %c\n", captain.number, captain.name.c_str(), captain.cat);
sort(selected.begin(), selected.end(), cmp2);
for(i = 0; i < selected.size(); i++) {
if(selected[i].name != captain.name)
printf("%d %s %c\n", selected[i].number, selected[i].name.c_str(), selected[i].cat);
}
puts("");
}
return 0;
}
文章定位: