/*------------------------------------------------------------------------------ * problem010.cc: Colley Calculation **----------------------------------------------------------------------------*/ /* Copyright (c) 2009 Peter Bui. All Rights Reserved. * * Peter Bui * **------------------------------------------------------------------------------ * Includes **----------------------------------------------------------------------------*/ #include #include #include #include #include #include #include #include #include using namespace std; /*------------------------------------------------------------------------------ * Data Structures **----------------------------------------------------------------------------*/ struct team { char *name; double rating; list wins; list losses; }; /*------------------------------------------------------------------------------ * Type Definitions **----------------------------------------------------------------------------*/ typedef list TeamsList; typedef vector TeamsVector; typedef map TeamsMap; /*------------------------------------------------------------------------------ * Constants **----------------------------------------------------------------------------*/ #define NITERATIONS 100 /*------------------------------------------------------------------------------ * Functions **----------------------------------------------------------------------------*/ static bool compare_teams(struct team *a, struct team *b) { return (a->rating > b->rating); } /*----------------------------------------------------------------------------*/ static void compute_initial_colley_ratings(TeamsMap &m) { TeamsMap::iterator it; struct team *tp; double nwin, ntot; for (it = m.begin(); it != m.end(); it++) { tp = it->second; nwin = tp->wins.size(); ntot = tp->wins.size() + tp->losses.size(); tp->rating = (double)(1.0 + nwin) / (double)(2.0 + ntot); } } /*----------------------------------------------------------------------------*/ static void compute_colley_ratings(TeamsMap &m) { TeamsMap::iterator mit; TeamsList::iterator lit; struct team *tp; double nwin, ntot; for (mit = m.begin(); mit != m.end(); mit++) { tp = mit->second; nwin = ((int)tp->wins.size() - (int)tp->losses.size()) / 2.0; ntot = tp->wins.size() + tp->losses.size(); for (lit = tp->wins.begin(); lit != tp->wins.end(); lit++) nwin += (*lit)->rating; for (lit = tp->losses.begin(); lit != tp->losses.end(); lit++) nwin += (*lit)->rating; tp->rating = (double)(1.0 + nwin) / (double)(2.0 + ntot); } } /*----------------------------------------------------------------------------*/ static void print_teams(TeamsVector &v) { for (size_t i = 0; i < v.size(); i++) printf("%d. %s\t%0.4lf\n", i + 1, v[i]->name, v[i]->rating); } /*------------------------------------------------------------------------------ * Main Execution **----------------------------------------------------------------------------*/ int main(int argc, char* argv[]) { TeamsMap teams; TeamsVector teams_set; string team_name, team_a, team_b; size_t nteams, score_a, score_b; struct team *tp; cin >> nteams; for (size_t i = 0; i < nteams; i++) { cin >> team_name; tp = new struct team(); tp->name = strdup(team_name.c_str()); tp->rating = 0.0; teams[team_name] = tp; teams_set.push_back(tp); } while (cin >> team_a >> score_a >> team_b >> score_b) { if (score_a > score_b) { teams[team_a]->wins.push_back(teams[team_b]); teams[team_b]->losses.push_back(teams[team_a]); } else { teams[team_a]->losses.push_back(teams[team_b]); teams[team_b]->wins.push_back(teams[team_a]); } } compute_initial_colley_ratings(teams); for (size_t i = 0; i < NITERATIONS; i++) compute_colley_ratings(teams); sort(teams_set.begin(), teams_set.end(), compare_teams); print_teams(teams_set); return (EXIT_SUCCESS); } /*------------------------------------------------------------------------------ * vim: sts=4 sw=4 ts=8 ft=cpp **----------------------------------------------------------------------------*/