UVA – 10194 – Football (aka Soccer)

Problem Statement : http://uva.onlinejudge.org/external/101/10194.html

Gochas :

  • Take care of the sorting function it’s so tricky
  • take care of the problem statement it states that the lexographic order is insensitive.
#include <iostream>
#include <string>
#include <string.h>
#include <map>
#include <vector>
#include <stdio.h>
#include <algorithm>
#include <cstring>
using namespace std;
struct Team {
	string name;
	int pts;
	int in, out;
	int wins, loss, tie;
	Team() {}
	Team(string name_) {
		name = name_;
		pts = in = out = wins = loss = tie = 0;
	}
	int games() { return wins + loss + tie; }
};
typedef map<string, Team>::iterator It;
bool operator <(const Team& a, const Team& b) {
	if (a.pts - b.pts) return a.pts > b.pts;
	if (a.wins - b.wins) return a.wins > b.wins;
	if ((a.in - a.out) - (b.in - b.out)) return (a.in - a.out) > (b.in - b.out);
	if (a.in - b.in) return a.in > b.in;
	int ag = a.wins + a.loss + a.tie, bg = b.wins + b.loss + b.tie;
	if (ag - bg) return ag < bg;
	string A =a.name, B=b.name;
	transform(A.begin(), A.end(), A.begin(), ::tolower);
	transform(B.begin(), B.end(), B.begin(), ::tolower);
	return A < B;
}

void parse(char* line, char* TA, int& ga, int& gb, char * TB) {
	char* ta, *tb;
	char* tA = strtok(line, "@"), tB = strtok(NULL, "@");
	ta = strtok(tA, "#");
	char *gA = strtok(NULL, "#");
	ga = atoi(gA);
	tb = strtok(tB, "#");
	gb = atoi(tb);
	tb = strtok(NULL, "#");
	memcpy(TA, ta, strlen(ta) + 1);
	memcpy(TB, tb, strlen(tb) + 1);
}

size_t find(vector<Team> a, string name) {
	for (size_t i = 0; i < a.size(); i++)
		if (a[i].name == name) return i;
	return -1;
}
int main() {
	int T, N, ga, gb, started = 0;
	char tour[200], team[50], team2[50];

	scanf("%d\n", &T);
	while (T-- && gets(tour) && scanf("%d\n", &N) == 1) {
		if (started++)
			printf("\n");
		printf("%s\n", tour);
		map<string, Team> m;
		vector<Team> as;
		while (N-- && gets(team))as.push_back(Team(string(team)));
		scanf("%d\n", &N);
		while (N--) {
			char line[10005];
			gets(line);
			parse(line, team, ga, gb, team2);
			int ai = find(as, string(team)), bi = find(as, string(team2));
			Team &a = as[ai], &b = as[bi];
			a.in += ga, a.out += gb, b.in += gb, b.out += ga;
			if (ga > gb)  a.pts += 3, a.wins++, b.loss++;
			if (ga < gb)  b.pts += 3, b.wins++, a.loss++;
			if (ga == gb) a.pts++, b.pts++, a.tie++, b.tie++;
		}
		sort(as.begin(), as.end());
		for (size_t i = 0; i < as.size(); i++) {
			printf("%d) %s %dp, %dg (%d-%d-%d), %dgd (%d-%d)\n", (i + 1),
					as[i].name.c_str(), as[i].pts, as[i].games(), as[i].wins,
					as[i].tie, as[i].loss, as[i].in - as[i].out, as[i].in,
					as[i].out);
		}
	}
	return 0;
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: