problem dicusssion
play

Problem Dicusssion Part 1: Some problems Lucca Siaudzionis and Jack - PowerPoint PPT Presentation

Problem Dicusssion Part 1: Some problems Lucca Siaudzionis and Jack Spalding-Jamieson 2020/03/17 University of British Columbia Announcements Reminder that A4 is due Sunday March 22nd. 1 Coins in a Row There are N ( N 10 6 ) piles of


  1. Problem Dicusssion Part 1: Some problems Lucca Siaudzionis and Jack Spalding-Jamieson 2020/03/17 University of British Columbia

  2. Announcements • Reminder that A4 is due Sunday March 22nd. 1

  3. Coins in a Row There are N ( N ≤ 10 6 ) piles of coins in a row. Each pile has a non-negative integer amount of coins. Two players take turns. At each turn, a player may take all the coins from a pile that is in either end of the row. Assuming both players want to maximize the number of coins, and the they both play optimally, how much will their coins be worth in the end of the game? Source: “Looking for a Challenge?” 2

  4. Coins in a Row • This is problem H in A0. • An easier version of this problem (with N ≤ 1000) is problem G in A2. 3

  5. Coins in a Row – Greedy? A greedy solution that is easy to come up with is to always pick the largest of the two ends. Unfortunately, it fails very easily: • 5 7 10 6 • First player would take 6, then second player would take 10, then first player would take 7. In the end, first player would have 6 + 7 = 13 and second would have 5 + 10 = 15. • The best move, however, is for the first player to take 5 and then 10, taking 15 coins total. 4

  6. Coins in a Row – Insight • Let G be the game. • Let val ( G ) be (first player score) - (second player score) , assuming both play optimally. • Define this as the value of the game. • If Σ is the total amount of coins in the game, we have: • First player score = (Σ + val ( G )) / 2 • Second player score = (Σ − val ( G )) / 2 5

  7. Coins in a Row – Insight • There is a straightforward DP for this: • val ( G [ i , i ]) = P [ i ] • val ( G [ i , j ]) = max( P [ i ] − val ( G [ i + 1 , j ]) , P [ j ] − val ( G [ i , j − 1]), if i � = j • This is O ( N 2 ), and it solves problem G from assignment 2. • Unfortunately, it doesn’t solve our current problem. 6

  8. Coins in a Row – Insight • We’ll use two theorems for this. • We will not prove them because the proofs are boring and technical, but you can find them here: https://www.mimuw.edu.pl/~idziaszek/termity/termity.pdf 7

  9. Coins in a Row – Insight Theorem One: Greedy Move Principle • Say the pile with the most amount of coins has M coins, and it is in one of the ends of the row. • Let G ′ be the game after removing that pile. • Then, val ( G ) = M − val ( G ′ ) • In other words, if the biggest pile is in one of the ends, it is optimal to take it . 8

  10. Coins in a Row – Insight Theorem Two: Fusion Principle • Let x , M , y be three adjacent piles in which x , y ≤ M . Note that M doesn’t have to be the biggest pile in the game, it just has to be bigger than its neighbours. • Let G ′ be the game in which those three piles were replaced by a single pile with value x − M + y . • Then, val ( G ) = val ( G ′ ). 9

  11. Coins in a Row – Insight We can apply theorem two several times in a row in constantly reduce the game: • 5 7 10 6 9 9 1 7 11 8 • 5 7 10 6 1 7 11 8 • 5 7 10 6 1 7 11 8 • 5 7 10 6 1 4 • 5 7 10 6 1 4 • 5 3 1 4 • 5 3 1 4 10

  12. Coins a Row – Insight What happens if we apply theorem two as many times as possible, until we no longer can do it? • Then, there is no set of adjacent piles x , M , y such that x , y ≤ M . • Hence, the biggest pile must be in a corner. • After removing the biggest pile, the second one must be in a corner as well... In other words, we can just apply theorem one until the game ends! 11

  13. Cache Loading You’re working with a computer that has a cache of 1 ≤ n ≤ 5 · 10 5 different addresses, numbered 1 to n . There are also m different files stored on your hard-drive. The i th file is a byte array x i of length s i . You are going to do 1 ≤ q ≤ 5 · 10 5 operations. Each operation is one of three kinds: • Load file i into the cache starting at position index p . This overwrites any previously stored value in the cache. • Print the byte that is stored in address p of the cache. • Update one of the files: For the l th through r th indeces of the file, perform the operation x := ( x + 1) mod 256. Source: 2019 Pacific NW ICPC Regional 12

  14. Cache Loading - Insight This is almost a simple segment tree problem,where the thing stored at the nodes of the segment tree is: • A reference to the left-most node of the ”file” (first index in the cache). • An index marking which file is stored. We need to use offline processing to make sure we read the right version of the file for the second type of operation. In addition, we will store one additional thing at each node: the version of the file. We then process all the queries in order, but whenever we see a type 2 query, we record what should be printed out (since we can’t access it). We then process these offline, by going through all the type 3 queries again (making new queries for each version of each file, corresponding to the type 2 queries from before). 13

  15. Afternoon Tea Lucca starts with a cup half-full of milk and half-full of tea. The cup is perfectly mixed. There are two types of operations: • Type M : Lucca drinks half of the cup, fills it with milk, and mixes it. • Type T : Lucca drinks half of the cup, fills it with tea, and mixes it. At the end of N ≤ 100 , 000 operations, will Lucca have consumed more milk or tea? Source: “Looking for a Challenge?” (this book is amazing) 14

  16. Afternoon Tea – Easier Warm-Up Let’s switch the problem just a bit: assume, after all operations, Lucca drinks whatever is left in the cup. How is this easier? • Keeping track of all the drinks consumed is hard. • However, keeping track of all the drinks poured into the cup is much easier. • If Lucca drinks the cup in the end, these amounts will be the same! 15

  17. Afternoon Tea – Insight • Keeping track of how much milk/tea was poured into the cup is easy. • Now, we need to figure out how much was left in the cup in the end. • The answer is in their difference! • Every time we pour something into a cup, let’s find out how much of that will be left in the cup in the end. 16

  18. Afternoon Tea – Insight • Assume WLOG that the cup has 2 N +1 mL. • Initially, there are 2 N mL of milk and 2 N mL of tea. • Those initial quantities will be diluted N times. Each dilution reduces the amount by half. • In the end, those initial quantities will have become 2 N / 2 N = 1 mL. 17

  19. Afternoon Tea – Insight • At any operation, we will add 2 N mL of the cup of either milk or tea (depending on the operation type). • If there are K operations left to be made, this amount will become 2 N − K after all the dilutions. • We can keep track of this for every operation. 18

  20. Afternoon Tea – Insight • We can represent the amount of milk/tea left in the cup as the sum of powers of two. • The amount of milk/tea poured into the cup will also be a power of two. • We can keep track of just the indices of this power, and implement this carefully. 19

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend