Presentation is loading. Please wait.

Presentation is loading. Please wait.

USACOW: The Aftermath. PotW Solution Scanner s = new Scanner(System.in); int n = s.nextInt(), k = s.nextInt(), x = 0; for (int i = 0; i < n; i++) x ^=

Similar presentations


Presentation on theme: "USACOW: The Aftermath. PotW Solution Scanner s = new Scanner(System.in); int n = s.nextInt(), k = s.nextInt(), x = 0; for (int i = 0; i < n; i++) x ^="— Presentation transcript:

1 USACOW: The Aftermath

2 PotW Solution Scanner s = new Scanner(System.in); int n = s.nextInt(), k = s.nextInt(), x = 0; for (int i = 0; i < n; i++) x ^= s.nextInt() % (k + 1); System.out.println(x == 0 ? "John" : "Bessie");

3 USACO 2011 November Contest Officially over now (hopefully everyone took it) Preliminary results can be found at: http://usaco.org/index.php?page=nov11results http://usaco.org/index.php?page=nov11results o Yes, we can see everyone's scores! Problems in all divisions and their test data are also available. o Yay, more practice problems to do!

4 Bronze Problem: digits A number, N, is converted to base 2 and base 3, but one digit is written incorrectly in each case. Given these two (incorrect) results, find N. N < 1,000,000,000 Test all possible changes to the base 2 number and see if resulting number can be reached with one digit change to the base 3 number o If difference between resulting number and base 3 input is 1 or 2 times power of 3

5 Bronze Solution BufferedReader f = new BufferedReader(new FileReader("digits.in")); PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("digits.out"))); String base2Str = f.readLine(); String base3Str = f.readLine(); int base2 = 0, base3 = 0; for (int i = base2Str.length() - 1, digit = 1; i >= 0; digit *= 2, i--) base2 += digit * (base2Str.charAt(i) - '0'); for (int i = base3Str.length() - 1, digit = 1; i >= 0; digit *= 3, i--) base3 += digit * (base3Str.charAt(i) - '0'); for (int powerOf2 = 1, digitIndex = base2Str.length() - 1; digitIndex >= 0; powerOf2 *= 2, digitIndex--) { int possibleN = (base2Str.charAt(digitIndex) == '1') ? base2 - powerOf2 : base2 + powerOf2; int diff = Math.abs(possibleN - base3); if (isPowerOf3(diff)) { out.println(possibleN); out.close(); System.exit(0); } private static boolean isPowerOf3(int num) { while (num % 3 == 0) num /= 3; return num == 1 || num == 2; }

6 Silver Problem: lineup N cows are standing at various positions along a line. Each cow has an integer position and an integer “breed ID” Given each cow’s position and breed number, determine the minimum size (range of positions) of a photograph capturing at least one of each breed of cow.

7 Silver Problem (cont.) class Pair implements Comparable { int x, id; Pair(int x_,int id_){x = x_; id = id_;} public int compareTo(Pair o) { if(x != o.x) return (x < o.x) ? -1 : 1; return (id < o.id) ? -1 : 1; } } Utility class for sorting pairs of numbers Implement comparable Sort by first, then by second Very often useful

8 Silver Solution Scanner s = new Scanner(System.in); int num = s.nextInt(), idc = 0; // idc is number of unique ids ArrayList list = new ArrayList (); HashMap map = new HashMap (); for(int i = 0; i < num; i++) { int x = s.nextInt(), id = s.nextInt(); // scan in x coordinates and ids if(!map.containsKey(id)) // compress ids using HashSet map.put(id,idc++); list.add(new Pair(x,map.get(id))); } Collections.sort(list); // sort cows by x coordinate int[] cnt = new int[idc]; // bins cows by id int existc = 0, left = 0, right = 0, best = 1100000000; while(left < num && right < num) { while(right < num && existc < idc) { // until all ids are present int cur = list.get(right++).id; // sweep right, adding cows if(cnt[cur]++ == 0) existc++; } if(existc == idc) { // if this is valid, modify best accordingly int d = list.get(right-1).x - list.get(left).x; if(d < best) best = d; } int cur = list.get(left++).id; // increment left, erase cow if(cnt[cur]-- == 1) existc--; } System.out.println(best);

9 Gold Problem: median Problem: Given n≤100,000 numbers, count how many continuous subsequences have median≥k, where the median's index is rounded up if the subsequence has even length. Obsrv. #1: O(n 2 ) is too slow for full credit Obsrv. #2: This problem is equivalent to counting how many subsequences have at least half their numbers greater than k. Obsrv. #3: Note that if you replace k with 0, you can then replace every number in the list ≥k with 1, and <k with -1.

10 Gold Problem (cont.) Obsrv. #4: Take prefix sums of the modified list - e.g. {-1,1,-1,-1,1} turns into {-1,0,-1,-2,-1} Obsrv. #5: Now you just have to determine, for each number in the prefix sum list, how many numbers to the left of it are ≤ it. The problem has been greatly simplified. o Note that this answer is essentially the reverse of the number of inversions of the prefix sum list From this point on, use either: o Mergesort - used to count # of inversions o Binary Indexed or Fenwick Tree - take advantage of each prefix sum lying within [-n,n]

11 Gold Solution bit : stores the Binary Indexed Tree j & -j isolates the last significant digit - this operation forms the core of BITs sum : prefix sum - artificially shifted by num + 1 to become nonnegative sum++ or sum-- depending on s.nextInt() ans : stores final answer - queries from the BIT are added to it Scanner s = new Scanner(System.in); int num = s.nextInt(), thresh = s.nextInt(), sum = num + 1; int[] bit = new int[2 * num + 2]; long ans = 0; for (int i = 0; i = thresh) sum++; else sum--; for (int j = sum; j > 0; j -= (j & -j)) ans += bit[j]; } System.out.println(ans);

12 PotW: Cow Whitewashing One day, at school, Bessie wrote a string of matching '('s and ')'s. As soon as she got home, however, she noticed that she had definitely made some mistakes! She plans on covering this up by simply erasing some of the parentheses on the left and right, while aiming for the longest substring. The substring she obtains must be a valid sequence of parentheses - each '(' must match to a ')' to its right, and vice versa. Tell Bessie the length of the longest valid substring. Sample Input - strings of parentheses ()) ()() )(()))(()()) Sample Output - length of longest valid substring 2 4 6 For 15 pts: length < 100 For 25 pts: length < 1,000 For 35 pts: length < 1,000,000


Download ppt "USACOW: The Aftermath. PotW Solution Scanner s = new Scanner(System.in); int n = s.nextInt(), k = s.nextInt(), x = 0; for (int i = 0; i < n; i++) x ^="

Similar presentations


Ads by Google