1 / 10

This title is orange because of Halloween.

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;

alida
Télécharger la présentation

This title is orange because of Halloween.

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. This title is orange because of Halloween. It is totally not because it’s always orange.

  2. 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

  3. 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);

  4. 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); } }

  5. Greedy Algorithms • All hail Johnny. • Allow him to enlighten you on this fascinating topic.

  6. 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

  7. 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!

  8. 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

  9. 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

  10. 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).

More Related