• 沒有找到結果。

Algorithm Design and Analysis Homework #3

N/A
N/A
Protected

Academic year: 2022

Share "Algorithm Design and Analysis Homework #3"

Copied!
6
0
0

加載中.... (立即查看全文)

全文

(1)

Algorithm Design and Analysis Homework #3

Due: 14:20, November 1, 2012

H om ework subm ission instructions

• Submit your programming assignment (problem 1) to the Judgegirl System (http://katrina.csie.ntu.edu.tw/judgegirl/). Also, use SVN to commit your report (named “report.pdf”) in the same folder of your code.

• Submit the answer of the writing problems and the programming report by one of the following ways:

1. Submit an electronic copy to the CEIBA system before the deadline; or 2. Submit a hard copy right before the class on the day of the deadline.

If you submit by both ways, the TA will randomly select only one of them for grading.

• Please make sure that you write down your name and school ID in the header of your documents. Otherwise, the TA will not grade them.

• If you choose to submit the answers of the writing problems via CEIBA, please combine the answers of all writing problems into only one file in the pdf format, with the file name in the format of “hw3 [ID].pdf” (e.g. “hw2 b99902010.pdf”).

Otherwise, the TA will not grade them.

•What should be included in the report:

1. Explain how your program works in detail.

2. Derive the time complexity of your program and briefly explain why. No need for formal proof.

3. The reference of your report.

• If you are stuck with any problem, you are welcome to ask the TAs via e-mails or in person during the office hour. The TA may discuss with you and give you some useful hints. :D

(2)

Problem 1 – Slime King

Description

You are an evil summoner and want to destroy the world with your secret weapons…Slimes.

Figure 1. On the left: a level 10 Slime; on the right, a level 1 Slime.

You have just summoned 𝑁 Slimes, and are going to merge all of them to create a strong Slime King.

𝐿𝑖 represents the level (the size) of Slime 𝑆𝑖. You may force Slime 𝑆𝑖 to eat another Slime 𝑆𝑗, which will take 𝐿𝑗 minutes (for 𝑆𝑖 to digest). After that, Slime 𝑆𝑗

will cease to exist, and the level of Slime 𝑆𝑖 will be increased by 𝐿𝑗.

The level of any Slime cannot be increased without your magic, and therefore you will never let two or more Slimes eat at the same time. Moreover, after a Slime eats for 𝐾 times, it will lose the ability to eat another Slime.

You cannot wait to destroy the world, and therefore decide to merge all your Slimes in the fastest way.

Input

The first line contains the two number 𝑁 and 𝑄.(1 ≤ 𝑁, 𝑄 ≤ 100,000)

The second line contains 𝑁 integers 𝐿1, 𝐿2, … , 𝐿𝑁, where two consecutive integers are separated by a space character ‘ ‘. (1 ≤ 𝐿𝑖 ≤ 1,000,000,000)

The third line contains 𝑄 integers 𝐾1, 𝐾2, … , 𝐾𝑄, where two consecutive integers are separated by a space. (1 ≤ 𝐾𝑖 ≤ 100,000)

Output

(3)

For each 𝐾𝑖 (1 ≤ 𝑖 ≤ 𝑄), output a line containing an integer, representing the minimum required time (in minutes) to merge all of the Slimes if 𝐾 = 𝐾𝑖 .

Sample Input 5 2

2 3 4 1 1 2 3

Sample Output

9 8

Hint

For sample #1: 𝑆2 eat 𝑆4(1 min); 𝑆2 eat 𝑆5(1 min); 𝑆3 eat 𝑆1(2 min); 𝑆3 eat 𝑆2(5 min).

For sample #2: 𝑆3 eat 𝑆1(2 min); 𝑆3 eat 𝑆2(3 min); 𝑆4 eat 𝑆5(1 min); 𝑆3 eat 𝑆4(2 min).

Remember to use long long data type.

There are 8 test cases in which Q ≤ 100 among the 10 test cases.

Grade

Judge result (10%) Report & code (10%)

(4)

Problem 2 - Gift

Summer Rift is a modern city. Its roads run either north-south or east-west and therefore the whole city looks like a grid table. As a result, every intersection in Summer Rift can be identified by a pair of integers (𝑥, 𝑦), 1 ≤ 𝑥, 𝑦 ≤ 𝑁.

Garen is a Summer Rift citizen and lives at intersection (1,1). One day, he decides to visit Lux, who lives at intersection (𝑁, 𝑁). Garen don’t want to be late, so he needs to follow the shortest route from (1,1) to (𝑁, 𝑁). In other words, if Garen is at intersection (𝑥, 𝑦) on his way to Lux’s place, he can only go to (𝑥 + 1, 𝑦) or (𝑥, 𝑦 + 1) in the next step.

Moreover, he decides to buy three different gifts for Lux; that means on the way he must pass through at least a flower shop, a bookstore, and a candy shop, respectively. Also, Garen doesn’t care about the order of visiting different shops. The shops can only be located at intersections.

Now, you are given a map of Summer Rift. The map tells you for each intersection whether there is a flower shop, a bookstore, a candy shop, or no shop at all. At most there can only be one shop at each intersection. With the map at your hands, please tell Garen the number of routes he can choose to go to Lux’s place and be able to buy all 3 needed gifts.

Hint: How many dimensions should you use when creating a Dynamic-Programming table? Well, you can think about how many “things” we should remember, such as where you are, how many televisions that have not been placed, etc.

Picture 1. examples of valid(left) and invalid routes(right).

1) (5%) We know that there is 𝐶𝑁−12𝑁−2 shortest routes from (1,1) to (𝑁, 𝑁).

Design an 𝑂(𝑁2)-time algorithm to compute 𝐶𝑁−12𝑁−2. (Hint: Pascal’s Triangle) 2) (10%) Design an 𝑂(𝑁2)-time algorithm to compute the number of routes Garen

can choose. You need to briefly explain how and why your algorithms work.

(5)

Problem 3 – Go Go Go Greedy Girl!

── It’s a long story. Those you’ve defeated have come back.

Here comes Gabrielle. She trusts that all of the problems in the world can be solved by greedy algorithms. Therefore, while taking the ADA course, she tries to use a greedy algorithm on every problem in her homework! The following is her answer sheet. However, Gabrielle doesn’t give any proof for her algorithms. You, as a TA of the ADA course, need to determine whether her algorithms are correct or not. For each problem, if her algorithm is correct, please give a formal proof to her greedy algorithm. Otherwise, give a counterexample that makes her algorithm fail.

1) (10%) Dividing and Matching by Segments

On a line, there’re 𝑛 amber points 𝑎1, 𝑎2, … , 𝑎𝑛 and 𝑛 blue points 𝑏1, 𝑏2, … 𝑏𝑛. Your task is to match each amber point to a blue point by a line segment, and each blue point is connected to exactly one amber point. The total length of those n segments should be minimized.

Gabrielle: Sort both 𝑎𝑖 and 𝑏𝑖 and re-index them. So that 𝑎1 < 𝑎2 < ⋯ < 𝑎𝑛 and 𝑏1 < 𝑏2 < ⋯ < 𝑏𝑛. Directly connect 𝑎𝑘 to 𝑏𝑘 for all 𝑘 = 1,2 … 𝑛.

2) (10%) 連串矩陣相乘問題

You are given 𝑛 matrices (𝐴1, 𝐴2, … , 𝐴𝑛), where 𝐴𝑖′s size is 𝑝i−1× 𝑝𝑖. Multiplying a 𝑎 × 𝑏 matrix with a 𝑏 × 𝑐 matrix needs 𝑎 × 𝑏 × 𝑐 operations, and results in a 𝑎 × 𝑐 matrix. Determine an order to multiply 𝐴1𝐴2… 𝐴𝑛 so that the number of operations is minimized.

Gabrielle: Find the maximum value of 𝑝1, … 𝑝𝑛−1, says, 𝑝𝑘. Then, multiply 𝐴𝑘 with 𝐴𝑘+1 first. Repeat the process (find the maximal 𝑝𝑘 then multiply 𝐴𝑘 with 𝐴𝑘+1) to the remaining matrices until there is only one matrix left. If there are multiple 𝑝𝑖 which are maximal, choose the one whose index is the smallest.

3) (10%) Open!!

There are 𝑛 days in this semester. Every day you receive a coupon. You will also need to buy a few items every day. The coupon allows you to buy one item for free. You can either use a coupon immediately on the day it was issued, or save it and use it later. Assume you already know that you will buy 𝑛𝑑 ≥ 1 items with the prices 𝑃𝑑 = {𝑝𝑑,1, … 𝑝𝑑,𝑛𝑑} on day 𝑑, 1 ≤ d ≤ n. Determine the minimum cost you will pay to purchase the items after these 𝑛 days.

Gabrielle: First, assume you have to pay for everything. Then, consider day 𝑑 = 𝑛, 𝑛 − 1, … ,1 in the given order, and for each day choose an item that you still have to pay in 𝑃𝑑 ∪ 𝑃𝑑+1∪ … ∪ 𝑃𝑛 with the maximal price. Use a coupon to pay for this item.

(6)

4) (10%) Breaking a String

A programmer wants to combine 𝑚 strings (𝑆1, 𝑆2, … , 𝑆𝑚) into a string 𝑆1𝑆2… 𝑆𝑚. A certain string-processing language can combine two strings 𝑆 and 𝑇 into a string 𝑆𝑇 of 𝑛 characters, which takes 𝑛 unit of time.

Compute the lowest cost of a sequence combination.

Gabrielle: Always choose two consecutive strings 𝑆𝑖𝑆𝑖+1 that contain the lowest number of characters and combine them. If there is more than one possible choice, choose the one whose 𝑖 is the smallest.

Although her greedy algorithm(s) is/are wrong for some problem(s), she still believes that these problem(s) may be solved by other smarter greedy strategies.

Thus, she doesn’t feel disappointed to greedy algorithms. Moreover, she is trying to open her XiGui using a greedy algorithm now! Keep going, greedy Gabrielle!

Problem 4 – Coin Changing

Assume that there are 𝑛 kinds of available coins. The denominations are 1 = 𝑎1 < 𝑎2 < ⋯ < 𝑎𝑛 dollars, respectively, and each 𝑎𝑖 is an integer. Consider you are making change for 𝑚 dollars using the fewest number of coins.

1) (10%) Describe a greedy algorithm to make change when 𝑛 = 4 and the denominations are 1, 5, 10 and 50. Prove that your algorithm yields an optimal solution.

More generally, suppose that the set of denomination satisfies 𝑎𝑘−1 divides 𝑎𝑘 for every 𝑘 = 2,3 … 𝑛. Show that the greedy algorithm always yields an optimal solution.

2) (5%) Give a set of denomination for which the greedy algorithm does not yield an optimal solution to some 𝑚.

3) (10%) Give an 𝑂(𝑛𝑚)-time algorithm that makes change for 𝑚 dollars for any set of 𝑛 different coin denomination.

4) (Bonus 5%) Now just consider 𝑛 = 3 case. Let 𝑟 = ⌈𝑎3/𝑎2⌉ and 𝑀 = 𝑟𝑎2. Prove that the greedy algorithm works for every 𝑚 if and only if it works for 𝑀.

* ⌈𝑠⌉ means the smallest integer that is not smaller than 𝑠. For example, ⌈2.78⌉ = 3, ⌈7.01⌉ = 8, ⌈5⌉ = 5. In addition, note that ⌈𝑠⌉ is an integer.

參考文獻

相關文件

Algorithm Design Methods Greedy Algorithm.. by Chin

[r]

 Some  queens  have  already  been   placed  on  the  chessboard  and  you  cannot  change

minimum number of steps.. A while later, a policeman discovers the car is missing and starts to pursue the thief on foot. His maximum running speed equals to two times the

You are asked to derive a dynamic programming algorithm to derive the minimum total distance.. (10%) Prove that this problem exhibits

• 下面介紹三種使用 greedy algorithm 產生 minimum cost s panning

As summarized in the following theorem, we resolve this open problem by augmenting Chudnovsky et al.'s even ‐hole detection algorithm into the first known polynomial ‐time

Remark: All the sequences are sequence of real numbers.. Formula that might be useful: Let θ