/* * Solution to "Party, Party, Party" * (Not very efficient, but maybe it doesn't need to be.) */ import java.util.*; // Container class for party: just a "start" and an "end" time class Pair implements Comparable { public int s, e; public Pair(int s, int e) { this.s = s; this.e = e; } public int compareTo(Pair o) { if (this.s < o.s) return -1; else if (this.s > o.s) return 1; else if (this.e < o.e) return -1; else if (this.e > o.e) return 1; else return 0; } } public class Party { public static Scanner in; public static int p; // number of parties going on public static int caseNum; // day number public static Pair party[]; // all the parties being held public static void main(String[] args) { in = new Scanner(System.in); caseNum = 0; while ((p = in.nextInt()) > 0) { caseNum++; // Input the party information: party = new Pair[p]; for (int i = 0; i < p; i++) { int s = in.nextInt(); int e = in.nextInt(); party[i] = new Pair(s,e); } // Sort parties, first by start time, and for identical start times, // by end time. (Latter condition not really needed for my solution.) Arrays.sort(party); // It's going to be convenient to delete parties from the list, so // convert from array to ArrayList: List l = Arrays.asList(party); ArrayList al = new ArrayList(l); // Debug: // for (int i = 0; i < p; i++) // System.out.printf("%d %d\n",party[i].s,party[i].e); int loc = 0; // Always begin searching list at position "loc" int answer = 0; // number of parties Emma can attend // For each hour, run through the list and see if there is a party // then. //for (int time = 8; time <= 23; time++) { for (int half = 0; half < 33; half++) { double time = 8 + half*.5; if (loc >= al.size()) break; // Now search for the eligible party with earliest ending time: int j = loc; int min = 25; int minLoc = j; while (j < al.size()) { Pair x = al.get(j); if (x.s > time) break; // This is not an eligible party -- hasn't started yet if (x.e > time && x.e < min) { minLoc = j; min = x.e; } j++; } // If we found an eligible party, count it and remove it from the list: if (min < 25) { answer++; // Debug: // System.out.println("("+al.get(minLoc).s+","+al.get(minLoc).e+")"); al.remove(minLoc); } } System.out.println("On day " + caseNum + " Emma can attend as many as " + answer + " parties."); } } }