TopcoderArchive
Archive
Algorithm problem

SoccerChampionship

Problem statement, definition, constraints, and public examples.

Problem Statement

Most soccer leagues use the following points system: each team plays against each other team exactly once, and receives 3 points for a win and 1 point for a tie (losing a game gives nothing). At the end of a tournament, teams are sorted by points. The team with the most points wins. If several teams are tied for the most number of points, the team with the smallest index among them takes the top spot.

You are a soccer fan and you know the results of all the games which have already been played. It is near the end of the season, and you want to know the worst possible position that your team can finish in.

You will be given a String[], games, containing the results of all previous games. Each element of games will be formatted as "n1 n2 R", where n1 and n2 are 0-based indexes of teams, and R is a character representing the result. If R is equal to '1', this means that team n1 has won, '2' means n2 was the winner, and '0' represents a tie. Also, you will be given an int teams - the total number of teams in the tournament. The third parameter, index, gives you the index of your favorite team. You should return the worst possible place (1 for first place) your team can occupy after all games have been played.

Definition

Class:
SoccerChampionship
Method:
worstPossible
Parameters:
String[], int, int
Returns:
int
Method signature:
int worstPossible(String[] gamesPlayed, int teams, int index)
(be sure your method is public)

Notes

  • Each team plays against each other team exactly once.

Constraints

  • There will be at most 10 unplayed games.
  • gamesPlayed will contain between 0 and (teams*(teams - 1)/2) elements, inclusive.
  • Each element of gamesPlayed will be formatted as "n1 n2 R", where n1 and n2 are different integers between 0 and teams - 1, inclusive, with no extra leading zeros.
  • teams will be between 2 and 10, inclusive.
  • index will be between 0 and (teams - 1), inclusive.
  • Each R is '1', '2' or '0'.
  • gamesPlayed will not contain two games between the same pair of teams.

Examples

  1. {"0 1 0", "2 0 0"}
    3
    0
    Returns: 2
    Any of two other teams can beat your team, but not at once. So your team will never be lower than 2nd place.
  2. {"2 1 0", "2 0 0"}
    3
    2
    Returns: 3
    If teams 0 and 1 will also make a draw, all teams will have 2 points in the end of tournament, and team number 2 takes last place because it has the biggest index.
  3. {"0 1 0", "1 2 1", "2 0 0"}
    3
    1
    Returns: 1
    The tournament is over, so just check where you team ended.
  4. {"1 0 2", "2 0 1", "2 1 1", "3 0 0", "3 1 0", "3 2 1", "4 0 0", "4 1 0",
     "4 2 1", "4 3 2", "5 0 0", "5 1 1", "5 2 2", "5 3 1", "5 4 2", "6 0 1",
     "6 1 0", "6 2 1", "6 3 2", "6 4 0", "6 5 2", "7 0 0", "7 1 0", "7 2 1",
     "7 3 0", "7 4 1", "7 5 0", "7 6 1", "8 0 0", "8 1 0", "8 2 0", "8 3 1",
     "8 4 1", "8 5 0", "8 6 2", "8 7 0", "9 0 1", "9 1 2", "9 2 1", "9 3 0",
     "9 4 2", "9 5 0", "9 7 2", "9 8 0"}
    10
    7
    Returns: 1
    Your team has secured the top spot already.
  5. {"1 0 2", "2 0 1", "3 0 1", "3 1 1", "3 2 2", "4 0 0", "4 1 0", "4 2 0",
     "4 3 1", "5 0 1", "5 1 1", "5 2 0", "5 3 1", "5 4 0", "6 0 0", "6 1 0",
     "6 3 0", "6 4 0", "6 5 0", "7 1 1", "7 2 1", "7 3 0", "7 4 2", "7 5 2",
     "7 6 2"}
    8
    4
    Returns: 3
Back to all problems