Hatena::Grouptopcoder

hama_DU@TopCoderへの道

2012-04-21SRM343, SRM344 (Practice)

SRM 344 VolleyballTournament

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

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

  • 0点、1点、2点を取った試合数を全探索する。
    • 組み合わせが複数あれば AMBIGUITY を返す
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class VolleyballTournament {

	static final String AMB = "AMBIGUITY";
	
	public String reconstructResults(int wm, int lm, int ws, int ls) {
		ws -= wm * 3;
		ls -= lm * 3;
		int[] mat = {wm, lm};
		int[] set = {ls, ws};
		List<String> rs = new ArrayList<String>();
		for (int x = 0 ; x <= 1 ; x++) {
			int match = mat[x];
			int sets = set[x];
			int[] zot = new int[3];
			
			int rec = 0;
			for (int zero = 0 ; zero <= match ; zero++) {
				for (int one = 0 ; one <= match ; one++) {
					for (int two = 0 ; two <= match ; two++) {
						if (zero + one + two != match) {
							continue;
						}
						if (zero * 0 + one * 1 + two * 2 == sets) {
							rec++;
							zot = new int[]{zero, one, two};
						}
					}
				}
			}
			if (rec >= 2) {
				return AMB;
			}
			
			if (x == 0) {
				for (int z = 0 ; z <= 2 ; z++) {
					for (int d = 0 ; d < zot[z] ; d++) {
						rs.add("3-" + z);
					}
				}
			} else {
				for (int z = 0 ; z <= 2 ; z++) {
					for (int d = 0 ; d < zot[z] ; d++) {
						rs.add(z + "-3");
					}
				}				
			}
		}
		
		Collections.sort(rs);
		String ret = "";
		for (String s : rs) {
			ret += s + ",";
		}
		return ret.substring(0, ret.length() - 1);
	}
}