Qualification Problem Set 2 September 7-8, 2004 Summary This set contained a rather straightforward, but long hard problem. Antimatter, known for his impressive speed, won this set handily, though perhaps because he got to warm up on set 1. He beat out two yellow coders oldbig, and Olexiy, who took second and third, each over 100 points behind.
The ProblemsFileFilterUsed as: Division One - Level One:
As always, knowledge of the libraries in your language of choice makes solving problems much easier. In this problem, all you needed was the .endsWith() (or its equivalent) method, and you were pretty much set. Even if you didn't know about that one, it only takes a single loop to check of one string ends with another. ResultsTableUsed as: Division One - Level Three:
In this problem, I think that its best to make some sort of a class or struct
for each competitor. If you do this, you can write a method to compare two
instances of the class or struct, and I think it makes your code more elegant,
and hence less error prone. Also, if you have a class for competitors, you only
need to write a comparator, and the built in sorting functions will do the
rest. import java.util.*; public class ResultsTable{ int[] sort; String order; class Rec implements Comparable{ int[] score; int[] cnt; String t; public Rec(int n){ score = new int[n]; cnt = new int[n]; Arrays.fill(cnt,-1); } public String toString(){ String ret = t; for(int i = 0; i<cnt.length ;i++){ if(cnt[i] == -1)ret += " -"; else ret += " "+score[i]; } return ret; } public int compareTo(Object o){ Rec r = (Rec)o; for(int i = 0; i<sort.length; i++){ if(Math.abs(sort[i]) == 1){ return t.compareTo(r.t)*sort[i]; } int idx = Math.abs(sort[i])-2; if(cnt[idx] == -1 && r.cnt[idx] == -1)continue; else if (cnt[idx] == -1) return sort[i]; else if (r.cnt[idx] == -1) return -sort[i]; else if(r.score[idx] != score[idx])return (r.score[idx] - score[idx])*(order.charAt(idx)=='H'?1:-1)*sort[i]; } return 0; } } public String[] generateTable(String[] results, int[] sort, String order){ this.sort = sort; this.order = order; HashMap hm = new HashMap(); for(int i = 0; i<results.length; i++){ String[] sp = results[i].split(" "); int met = Integer.parseInt(sp[1])-1; int cnt = Integer.parseInt(sp[2]); int score = Integer.parseInt(sp[3]); Rec r = (Rec)hm.get(sp[0]); if(r == null)r = new Rec(order.length()); if(cnt > r.cnt[met]){ r.cnt[met] = cnt; r.score[met] = score; } r.t = sp[0]; hm.put(sp[0],r); } Rec[] recs = (Rec[])hm.values().toArray(new Rec[0]); Arrays.sort(recs); String[] ret = new String[recs.length]; for(int i = 0; i<ret.length; i++)ret[i] = recs[i].toString(); return ret; } } |
|