Hatena::Grouptopcoder

hama_DU@TopCoderへの道

2012-03-24SRM364 (Practice)

SRM 364 Paintball

|  SRM 364 Paintball - hama_DU@TopCoderへの道 を含むブックマーク はてなブックマーク -  SRM 364 Paintball - hama_DU@TopCoderへの道

http://www.topcoder.com/stat?c=problem_statement&pm=8060

  • やるだけっぽい
    • バグらないようにオブジェクト指向っぽく実装
  • サンプル合わない。
    • 仲間を倒した場合得点マイナスになるのか
    • 直した
  • サンプル通った。出した。
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Paintball {

	public class Team implements Comparable<Team> {
		String name;
		List<Player> players;
		int score = 0;
		Team(String n){
			name = n;
			players = new ArrayList<Player>();
		}
		public List<String> teamString(){
			List<String> ret = new ArrayList<String>();
			ret.add(name + " " + score);
			for (Player p : players) {
				ret.add("  " + p.name + " " + p.score);
			}
			return ret;
		}
		public int compareTo(Team o) {
			if (score == o.score) {
				return name.compareTo(o.name);
			}
			return o.score - score;
		}
		public void doSort() {
			Collections.sort(players);
		}
	}
	
	public class Player implements Comparable<Player> {
		Team team;
		String name;
		int score;
		Player(String n, Team t) {
			score = 0;
			name = n;
			team = t;
			t.players.add(this);
		}
		void splattered(Player another) {
			if (team.name.equals(another.team.name)) {
				score -= 1;
				team.score -= 1;
			} else {
				score += 1;
				another.score -= 1;
				team.score += 1;
				another.team.score -= 1;
			}
		}
		public int compareTo(Player arg0) {
			if (score == arg0.score) {
				return name.compareTo(arg0.name);
			}
			return arg0.score - score;
		}
	}
	
	
	public String[] getLeaderboard(String[] players, String[] messages) {
		Map<String, Team> tmap = new HashMap<String, Team>();
		List<Team> teams = new ArrayList<Team>();
		Map<String, Player> pmap = new HashMap<String, Player>();
		for (String p : players) {
			String[] data = p.split(" ");
			if (!tmap.containsKey(data[1])) {
				tmap.put(data[1], new Team(data[1]));
				teams.add(tmap.get(data[1]));
			}
			Player pl = new Player(data[0], tmap.get(data[1]));
			pmap.put(pl.name, pl);
		}
		
		
		for (String mes : messages) {
			String[] data = mes.split(" ");
			Player a = pmap.get(data[0]);
			Player b = pmap.get(data[2]);
			a.splattered(b);
		}
		
		
		Collections.sort(teams);
		List<String> standings = new ArrayList<String>();
		for (Team t : teams) {
			t.doSort();
			standings.addAll(t.teamString());
		}
		String[] ret = new String[standings.size()];
		for (int i = 0 ; i < standings.size() ; i++) {
			ret[i] = standings.get(i);
		}
		return ret;
	}
}