1 / 11

528491

528491. Not quite random. PotW Solution. Scanner scan = new Scanner( System.in ); int x = scan.nextInt (); LinkedList <Integer > q = new LinkedList <Integer>(); for ( int i = 0; i < x; i ++ ) q.add ( scan.nextInt () ); int answer = 0; int i = 1; while ( ! q.isEmpty () ) {

baruch
Download Presentation

528491

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. 528491 Not quite random

  2. PotW Solution Scanner scan = new Scanner( System.in ); intx = scan.nextInt(); LinkedList<Integer> q = new LinkedList<Integer>(); for ( inti = 0; i < x; i++ ) q.add( scan.nextInt() ); intanswer = 0; inti = 1; while ( !q.isEmpty() ) { intcur = q.poll(); answer++; if ( cur != i ) q.add( cur ); else i++; } System.out.println( answer );

  3. SIGN UP FOR HARKER • (if you haven’t already) • http://bit.ly/harkerproco12 • Deadline for registration is this Saturday (3/10) • Put N/A under chaperone info • Contest is Saturday, 3/17 • 9AM-4PM(actual contest is ~1.5 hours) • Worth 30 points PotW credit • If you need any more incentive

  4. TAKE MARCH USACO • Today’s the last day! • 5 points of PotW credit (who would’ve guessed?)  Space filler

  5. Some random facts about randomness

  6. Randomness • Numbers in themselves are not actually random • Distributions of numbers can be random, however • The discrete uniform distribution: • A random integer between [a, b] is chosen • Two possible ways of obtaining “random” numbers • Artificially generated • Read from outside sources • Lava lamps • Atmospheric noise • Nuclear fission

  7. Random number generation • For most purposes, pseudorandom works fine • Computers, without any additional input from the environment, cannot generate truly random numbers • Can use mathematical formulas or tables to generate “random” numbers • E.g. Linear Congruential Generator (LCG): • (c * x + a) (mod m) over x • This formula has a period of at most m, so it is fairly weak • Also suffers from points lying on common planes • For randomized algorithms • Even weak pseudorandom generators can work fine • If an evil person is able to manipulate the program in real-time, then the algorithm may suffer • E.g. if pivots are artificially chosen during quicksort

  8. Randomized Algorithms • Certain classes of problems can be solved more easily using randomized algorithms • E.g. Sorting, Monte Carlo, Max-flow min-cut can all be approached with randomized algorithms • Quicksort: • Under each recursive step of the algorithm, a pivot must be chosen to partition the array into two halves • Optimally, the pivot should be the median of the values • Instead, choosing a random pivot offers good runtime, on average • Randomized algorithms can also assist in solving “unsolvable” NP-hard problems • Finding the optimal solution may take exponential time, but we can generate approximations with randomized algorithms • E.g. Traveling salesman, Simulated annealing, the PotW

  9. PotW– Bovinekiin, Cowborn • Returning from your most recent adventures, you have noticed that there has been some pretty ridiculous inflation in the shop. Given N < 50 pieces of equipment available in the shop, maximize the total power of items you can buy with your X < 1012 hard-earned coins. Each piece of equipment costs C < 1010 and gives you P < 1010 power, and you can only buy each at most once. • Worth 40 points. If your answer is within 5% of the answer our awesome program gives, it will be deemed correct. • Time limit: 2 seconds

  10. Sample Input/Output • Sample Input:6 9001(N, X)8980 3(C, P)8940 210 320 430 540 6 • Sample Output:2 3 4 5 (indices of the bought items, not necessarily in order)

  11. Hints • The test data will not be especially tricky • Thus, 5% is fairly lenient • One possible solution: • Randomly shuffle the items • Buy as many items as possible left to right • Randomly swap an item that you have just bought with an item that you have not bought • Try step 2 and accept the swap if it produces better results • Repeat 90001 times, reshuffling if stalled for too long

More Related