100 likes | 261 Views
This title is orange because of Halloween. It is totally not because it’s always orange. Last three weeks’ PotW !. BufferedReader in = new BufferedReader ( new InputStreamReader (System. in )); StringBuffer s = new StringBuffer ( in.readLine ()); int count = 0;
E N D
This title is orange because of Halloween. It is totally not because it’s always orange.
Last three weeks’ PotW! BufferedReader in = newBufferedReader(newInputStreamReader(System.in)); StringBuffer s = new StringBuffer(in.readLine()); intcount = 0; for ( ; s.length() > 1; count++) { if(s.charAt(s.length() - 1) == '0') // if even s.deleteCharAt(s.length() - 1); // remove last char else// if odd { intlast0 = s.lastIndexOf("0"); if(last0 == -1) // no '0' remaining { System.out.println(count + s.length() + 1); // shortcut return; } // continued on next slide
PotW solution (cont.) else// there are still '0's remaining { s.setCharAt(last0, '1'); // replace last '0' with 1 last0++; // replace trailing '1's with '0' for( ; last0 < s.length(); last0++) s.setCharAt(last0, '0'); } } // end else (if odd) } // end for loop if (s.charAt(0) == '0') count++; System.out.println(count);
PotW Solution (hax ver.) importjava.util.Scanner; publicclassYoungBetsy { publicstaticvoid main(String[] args) { Scanner s = new Scanner(System.in); String n = s.next(); intloc = n.lastIndexOf('1'); if(loc != 0) System.out.println(n.split("0", -1).length + loc + 1); else System.out.println(n.length() - 1); } }
Greedy Algorithms • All hail Johnny. • Allow him to enlighten you on this fascinating topic.
Greedy Algorithms • At each step of the algorithm, make the locally optimal choice • This only works for a fairly limited set of problems • Only use greedy algorithms if you can prove their validity • Examples: • Change making • Segment covering • Dijkstra's algorithm for shortest paths • Kruskal's and Prim's algorithms for minimum spanning tree Source: Wikipedia
Change Making (working case) • Change making: • Try to make change for a certain amount using the minimum number of coins/bills • At each step of the algorithm, use the highest denomination that fits under current amount • Guaranteed to work if each denomination is divisible by the next highest • Bills: $1, $2, $4 • Amount: $11 to $7 to $3 to $1 to $0 • {$1,$2,$4,$4} • Works!
Change Making (fail case) • Doesn't always work! • Bills: $1, $3, $4 • Amount: $6 • $6 to $2 to $1 to $0 • {$1,$1,$4} • Not as good as {$3,$3}! • You would have to use a knapsack algorithm in this case
Segment Covering • Find maximum number of non-intersecting segments • e.g. try to attend largest number of activites • Sort all segments by endpoint • Go through segments one by one and check if they can be added to the current segment set without intersections • If so, add it! • Note that you only have to maintain the latest endpoint in the current set - you don't need arrays or anything Source: Topcoder
Partyin’, Partyin’ (yeah!)fun, fun, fun, funlooking forward to the weekend • It's Monday, and Halloween, so naturally, the cows are out partying. Bessie has a list of N<100,000 parties and their start/end times (in 24 hr format), and she wants to maximize the number of parties she attends, k. She must attend the entirety of each party. • Note that the parties are sorted by end time (VERY important). • Sample Input: 4 0:00 0:05 0:00 0:10 0:05 0:15 0:15 0:20 • Sample Output: (print out k on a single line) 3 • This is worth 20 pts, but for 15 extra pts, also print out the maximum amount of time she can spend attending k parties (fairly difficult).