Skip to content

Instantly share code, notes, and snippets.

@53cr
Created October 24, 2009 23:12
Show Gist options
  • Select an option

  • Save 53cr/217790 to your computer and use it in GitHub Desktop.

Select an option

Save 53cr/217790 to your computer and use it in GitHub Desktop.
import java.util.*;
public class Main
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
int cases = in.nextInt();
String data;
ArrayList teamScores[] = new ArrayList[11];
for (int i=1;i<=10;++i) {
teamScores[i] = new ArrayList<TeamScore>();
}
int trial, score;
String name;
int points;
while (in.hasNextLine()) {
name = in.next();
trial = in.nextInt();
score = in.nextInt();
in.nextLine();
teamScores[trial].add(new TeamScore(name, trial, score));
}
for (int i=1; i<=10; ++i) {
Collections.sort(teamScores[i], new TeamScoreComparator());
int size = teamScores[i].size();
if (size > 0) {
int groupSize = (size-1)/9;
((TeamScore)teamScores[i].get(0)).points = 10;
for (int j=1; j<size; ++j) {
if (groupSize == 0) {
points = 9-(j-1);
} else {
points = 9 - (j-1)/groupSize;
}
if (points < 1) points = 1;
((TeamScore)teamScores[i].get(j)).points = points;
}
// Go back trhough and be fair.
for (int j=1; j<size; ++j) {
int thisPoints = ((TeamScore)teamScores[i].get(j)).points;
int lastPoints = ((TeamScore)teamScores[i].get(j-1)).points;
int thisScore = ((TeamScore)teamScores[i].get(j)).score;
int lastScore = ((TeamScore)teamScores[i].get(j-1)).score;
if (thisScore == lastScore && thisPoints < lastPoints) {
((TeamScore)teamScores[i].get(j)).points = lastPoints;
}
}
}
}
ArrayList<FinalEntry> finalAL = new ArrayList<FinalEntry>();
HashMap<String,Integer> people = new HashMap<String,Integer>();
for (int i=1; i<=10; ++i) {
for (int j=0; j<teamScores[i].size(); ++j) {
name = ((TeamScore)teamScores[i].get(j)).name;
points = ((TeamScore)teamScores[i].get(j)).points;
if (people.containsKey(name)) {
people.put(name, new Integer(points + people.get(name)));
} else {
people.put(name, new Integer(points));
}
}
}
Iterator it = people.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pairs = (Map.Entry)it.next();
finalAL.add(new FinalEntry((String)pairs.getKey(), (Integer)pairs.getValue()));
}
Collections.sort(finalAL, new FinalComparator());
for (int j=0; j<finalAL.size(); ++j) {
System.out.printf("%s %d\n",(finalAL.get(j)).name, (finalAL.get(j)).points);
}
}
}
class TeamScoreComparator implements Comparator<TeamScore> {
public TeamScoreComparator() {}
public int compare(TeamScore score1, TeamScore score2) {
return (score2).compareTo((score1));
}
}
class FinalComparator implements Comparator<FinalEntry> {
public FinalComparator() {}
public int compare(FinalEntry x1, FinalEntry x2) {
Integer d = (new Integer((x2).points));
int bleh = d.compareTo(((x1).points));
if (bleh == 0) {
return ((x1).name).compareTo((x2).name);
} else return bleh;
}
}
class TeamScore implements Comparable {
public String name;
public int trial;
public int score;
public int points;
public TeamScore(String name, int trial, int score) {
this.name = name;
this.trial = trial;
this.score = score;
this.points = 0;
}
public int compareTo(Object other) {
return (new Integer(this.score)).compareTo(((TeamScore)other).score);
}
}
class FinalEntry {
public String name;
public Integer points;
public FinalEntry(String name, Integer points) {
this.name = name;
this.points = points;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment