1 / 12

USACOW:

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 ^= s.nextInt () % (k + 1 ); System. out .println (x == 0 ? "John" : "Bessie" );. USACO 2011 November Contest.

hedy
Download Presentation

USACOW:

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. USACOW: The Aftermath

  2. PotW Solution Scanner s = newScanner(System.in); intn = 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 2011November Contest • Officially over now (hopefully everyone took it) • Preliminary results can be found at: http://usaco.org/index.php?page=nov11results • Yes, we can see everyone's scores! • Problems in all divisions and their test data are also available. • 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 • If difference between resulting number and base 3 input is 1 or 2 times power of 3

  5. Bronze Solution BufferedReader f = newBufferedReader(newFileReader("digits.in")); PrintWriter out = newPrintWriter(newBufferedWriter(newFileWriter("digits.out"))); String base2Str = f.readLine(); String base3Str = f.readLine(); intbase2 = 0, base3 = 0; for(inti = base2Str.length() - 1, digit = 1; i >= 0; digit *= 2, i--) base2 += digit * (base2Str.charAt(i) - '0'); for(inti = base3Str.length() - 1, digit = 1; i >= 0; digit *= 3, i--) base3 += digit * (base3Str.charAt(i) - '0'); for(intpowerOf2 = 1, digitIndex = base2Str.length() - 1; digitIndex>= 0; powerOf2 *= 2, digitIndex--) { intpossibleN = (base2Str.charAt(digitIndex) == '1') ? base2 - powerOf2 : base2 + powerOf2; intdiff = Math.abs(possibleN - base3); if(isPowerOf3(diff)) { out.println(possibleN); out.close(); System.exit(0); } } privatestaticbooleanisPowerOf3(intnum) { while(num % 3 == 0) num/= 3; returnnum == 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<Pair> {intx, id;      Pair(int x_,int id_){x = x_; id = id_;}publicintcompareTo(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);intnum = s.nextInt(), idc = 0; // idc is number of unique idsArrayList<Pair> list = newArrayList<Pair>();HashMap<Integer,Integer> map = newHashMap<Integer,Integer>();for(inti = 0; i < num; i++) {intx = 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(newPair(x,map.get(id))); }Collections.sort(list); // sort cows by x coordinateint[] cnt = newint[idc]; // bins cows by idintexistc = 0, left = 0, right = 0, best = 1100000000;while(left < num && right < num) {while(right < num && existc < idc) { // until all ids are present intcur = list.get(right++).id; // sweep right, adding cows if(cnt[cur]++ == 0) existc++; } if(existc== idc) { // if this is valid, modify best accordingly intd = list.get(right-1).x - list.get(left).x; if(d < best) best = d; } intcur = 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(n2) 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. • Note that this answer is essentially the reverse of the number of inversions of the prefix sum list • From this point on, use either: • Mergesort - used to count # of inversions • 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);intnum = s.nextInt(), thresh = s.nextInt(), sum = num + 1;int[] bit = newint[2 * num + 2];longans = 0;for(inti = 0; i < num; i++) {for (int j = sum; j < bit.length; j += (j & -j))    bit[j]++;if (s.nextInt() >= 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 substring2                           4                  6 • For 15 pts: length < 100 • For 25 pts: length < 1,000 • For 35 pts: length < 1,000,000

More Related