Tuesday, 29 July 2014

UVa 10194 - Football (aka Soccer)


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.HashMap;


class Main {



// a static inner class implementing Comparable
// the "natural ordering " this class is applied on the objects as teams
static class Team implements Comparable<Team>
{
String teamName;
//match result  attributes for each team
int wins, loses, ties, played, scored, against, points, diff;

//constrcutor
public Team(String name)
{
this.teamName = name;
this.wins = 0;
this.loses = 0;
this.ties = 0;
this.played = 0;
this.scored = 0;
this.against = 0;
this.points = 0;
this.diff = 0;
}

//function to update match result attributes for each team
public void update(int gScored , int gAgainst )
{
++this.played;
//update team result as per win , tie or lost of a match
if(gScored > gAgainst)
{
++this.wins;
this.points += 3 ;
}
else if(gScored == gAgainst)
{
++this.ties;
++this.points;
}
else
{
++this.loses;
}
this.scored += gScored;
this.against += gAgainst;
this.diff = this.scored - this.against;

}



//"natural ordering" comparator for objects of this class as per specs of football tournament

// this would be used by Collections.sort() or Arrays.sort()
//for natural order sorting in ascending order
/*
*@specs
*"
*Teams are ranked according to these rules (in this order):
* Most points earned.
     * Most wins.
   *  Most goal difference (i.e. goals scored - goals against)
     * Most goals scored.
     * Less games played.
     * Lexicographic order. 
     *
*"
*/
public int compareTo(Team t) {
if (this.points != t.points)
return t.points - this.points;
if (this.wins != t.wins)
return t.wins - this.wins;
if (this.diff != t.diff)
return t.diff - this.diff;
if (this.scored != t.scored)
return t.scored - this.scored;
if (this.played != t.played)
return this.played - t.played;
return this.teamName.toLowerCase().compareTo(t.teamName.toLowerCase());

}

}

public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in,
"ISO-8859-1"));
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out,
"ISO-8859-1"));
//flag to show a new line after first output
boolean first = true;
int cases  =  Integer.parseInt(in.readLine());

while(cases-- > 0 )
{
String tournName = in.readLine();
int ttlTeams = Integer.parseInt(in.readLine());

//create an array of Team objects , for teams in tournament
Team teams[] =  new Team[ttlTeams];

//hashmap for team and corresponding id 
HashMap<String , Integer> teamId = new HashMap<>();

//read all team names , fill the hashmap teamId and teams[] array of Team objects
for(int i = 0 ;i < ttlTeams ; ++ i)
{
String teamName = in.readLine();
teamId.put(teamName, i);
teams[i] = new Team(teamName);
}

int matchesPlayed = Integer.parseInt(in.readLine());

//for each match played , evaluate the result as per specs
for( int m = 0 ; m < matchesPlayed ; ++m )
{
String matchSummary = in.readLine();
String parts[] = matchSummary.split("[#@]");
//get the team names , goals and ids
String team1 = parts[0];
String team2 = parts[3];
int g1 = Integer.parseInt(parts[1]);
int g2 = Integer.parseInt(parts[2]);
int id1 = teamId.get(team1);
int id2 = teamId.get(team2);

//update the game result for the team playing this match
teams[id1].update(g1, g2);
teams[id2].update(g2, g1);
}

//sort the team[] array as per natural ordering of specs
Arrays.sort(teams);

//show the newline space between each result
if (first)
{
first = false;
}
else
{
out.println();
}
 
//show the result
out.println(tournName);
for (int i = 0; i < ttlTeams; ++i) {
out.println((i + 1) + ") " + teams[i].teamName + " "
+ teams[i].points + "p, " + teams[i].played + "g ("
+ teams[i].wins + "-" + teams[i].ties + "-"
+ teams[i].loses + "), " + teams[i].diff + "gd ("
+ teams[i].scored + "-" + teams[i].against + ")");
}
}
out.flush();
in.close();
System.exit(0);


}
}

No comments:

Post a Comment