Homework #1
Due Time: 2018/10/18 (Thu.) 14:20 Contact TAs: ada-ta@csie.ntu.edu.tw
Instructions and Announcements
• There are three programming problems and two hand-written problems.
• Programming. The judge system is located at https://ada18-judge.csie.org. Please login and submit your code for the programming problems (i.e., those containing “Programming” in the problem title) by the deadline. NO LATE SUBMISSION IS ALLOWED.
• Hand-written. For other problems (also known as the “hand-written problems”), please turn in a printed/written version of your answers to the instructor at the beginning of the class.
In case that your homework is lost during the grading, you can also upload your homework to the NTU COOL system. NO LATE SUBMISSION IS ALLOWED.
• Collaboration policy. Discussions with others are strongly encouraged. However, you should write down your solutions in your own words. In addition, for each and every problem you have to specify the references (e.g., the Internet URL you consulted with or the people you discussed with) on the first page of your solution to that problem. You may get zero point due to the lack of references.
Problem 1 - ADA Coin (Programming) (10 points)
Problem Description
Nowadays cryptocurrency is the hottest topic and Alpha noticed an unpopular one: ADA coin. After the first ADA class, Alpha was surprised by the popularity of ADA, so he believes that a cryptocurrency named “ADA” will become the dominating coin on the earth soon.
Based on the knowledge from “Machine Learning Foundations” and “Machine Learning Tech- niques”, Alpha has built a DEEP learning model to predict the price trend of the ADA coin for next n days. That is, Alpha believes he knows the price change of the ADA coin for the next n days.
Alpha wants to earn as many dollars as possible, but due to the high transaction fee, he decides to buy and sell exactly once. Also, Alpha does not want to buy and sell at the same day, so it is possible that Alpha loses some money even if he has known the price trend. Can you write a program to help Alpha find out how much he can earn during the next n days?
Input
The first line of the input file contains an integer indicating n.
The next line contains n integers, separated by spaces. For the i-th number ai, ai > 0 indicates that the price of the ADA coin rises ai dollars at the i-th day, while ai < 0 indicates that the price of the ADA coin falls −ai dollars at the i-th day. ai = 0 indicates that the price stays the same.
• 1 ≤ n ≤ 200000
• |ai| ≤ 10000
Output
Please output an integer indicating the maximal money Alpha can earn in the next n days.
Sample Input 1 6
2 -4 6 -1 0 2 Sample Output 1 7
Sample Input 2 3
-4 -6 -1
Sample Output 2 -1
Problem 2 - Rectangle Area (Programming) (20 points)
Problem Description
For any 2 points on the 2-dimensional plane, we can form a rectangle, where the edges are parallel to x-axis or y-axis and the diagonal line connects 2 points. That is, with 2 points, (x1, y1) and (x2, y2) , we can define a rectangle with width |x1− x2| and height |y1− y2|, and thus its area is |x1− x2| × |y1− y2|.
Given a set of points S, there are |S|×(|S|−1)
2 formed rectangles. Can you find the sum of the areas of all rectangles?
Input
The first line of the input file contains an integer indicating |S|.
For the next |S| lines, each line contains 2 integers x, y indicating a point in S.
• 2 ≤ |S| ≤ 100000.
• For all (x, y) ∈ S, 0 ≤ x, y ≤ 10000.
Subtask 1 (20 %)
• |S| ≤ 1000
Subtask 2 (20 %)
• x, y ≤ 100
Subtask 3 (60 %)
• No other constraints.
Output
Please output an integer indicating the answer.
Note that the answer might exceed the capacity of a 32-bit integer.
Sample Input 1 3
1 1 2 3 4 0
Sample Output 1 11
Sample Input 2 4
1 1 1 1 1 3 3 3
Sample Output 2 8
Problem 3 - Joe’s Game (Programming) (20 points)
Problem Description
There are n piles of stones on the ground. The i-th pile contains ai stones. Your friend, Joe, designs a game and wants to play this game with you.
The game consists of several rounds. In each round, Joe will give you a magic number k, and then you have to answer how many pairs of piles (i, j) such that ai+ aj = k and i < j.
Input
The first line contains an integer n indicating the number of piles, where 0 ≤ n ≤ 1, 000, 000.
The second line contains n integers ai indicating the number of stones of the i-th pile, where 0 ≤ ai≤ 100, 000.
The third line contains an integer m indicating the number of queries, where 0 ≤ m ≤ 1, 000, 000.
The fourth line contains m integers ki indicating the magic number of the i-th query, where 0 ≤ ki≤ 1, 000, 000, 000.
Output
Print m integers in one line — for each answer to each round.
Or, you can print ”joe59491” to get a wrong answer.
Note that the answer might exceed the capacity of a 32-bit integer.
Subtask 1 (20 %)
• n ≤ 1000
Subtask 2 (20 %)
• ai≤ 1000
Subtask 3 (60 %)
• No other constraints.
Sample Input 1 10
5 2 2 4 1 2 9 0 1 3 7
1 2 3 4 5 6 59491 Sample Output 1 2 4 7 6 6 5 0
Problem 4 - Time Complexity & Arvin’s Trip (Hand-Written) (25 points)
(1) Given each pair of expressions A and B in the following table, please find the relation between them (A is O, o, Ω, ω, Θ or None of B). Assume that k ≥ 1, > 0 and c > 1 are constants.
Your answer should be in the form like below:
Given A = x2+ 2 and B = x2 Ans: A is O, o, Ω, ω, Θ, None of B
A B
(a) (5%) log2nk n
(b) (5%) √
n nsin n
(c) (5%) 2
√logn nlogn (d) (5%) log n! lognn
(2) Arvin is an idol in the CSIE deparment, and some students who respect him form a group called “A127s”. Today, Arvin and A127s are now in the Fabulous Fox Theatre, which is lavishly located in the Atlanta with a 4,665-seat auditorium. The A127s sit in a line of seats. However, there is a problem that all boys sit together and all girls do, too. It is important for Arvin to resolve the issue so that he can match boys and girls.
Now, we give you an id sequence, which denotes the sitting positions of all A127s with n ele- ments (n/2 boys and n/2 girls). That is, given the static form [b1, b2, b3, · · · , bn/2, g1, g2, g3, · · · , gn/2], you need to switch the elements such that the sequence becomes [b1, g1, b2, g2, · · · , bn/2, gn/2] with the extra space O(log n) (note that n is even).
(a) (25%) Give the pseudo code for a Divide-and-Conquer algorithm with O(n log n) time com- plexity that changes the original sequence to the target sequence, and briefly explain your code.
Assume that the original ids are stored in the array M [0. . . ..n − 1].
(b) (15%) Please show the time complexity of your algorithm using Θ and briefly explain how you derive this bound.
(3) It is time for them to take a picture with this magnificent structure. A127s still stand in a horizontal line, and Arvin needs some of A127s form a second line behind. However, due to Arvin’s insistence, the second line needs to be first increasing and then decreasing. It is called
“unimodel.” Arvin wants to know how many combinations of the A127 that can fit Arvin’s requests.
Given a height array H[0 · · · n − 1] and the number k. Find the number of the unimodal subsequences with size k in the array. For instance,
Input: H = [142, 188, 189, 163], k = 3
Output: 3 ([142,188,163], [142,189,163], [188,189,163] are the unimodal subsequences with size 3.) (a) (25%) Give the pseudo code for a Dynamic Programming algorithm with time complexity O(kn2) and briefly explain your code. Assume that the original heights are stored in the array H.
(b) (15%) Please show the complexity of your algorithm using Θ and briefly explain how you
Problem 5 - Recurrence & ADA-TA-QQ Problem (Hand-Written) (25 points)
(1) (50%) Solve the following recurrences. Assume that T (n) = 1 ∀n ≤ 2.
Write down your proofs thoroughly. If you apply some theorems that are not taught in class, you have to prove them first.
(a) (10%) T (n) = 0.5T (n2) + n3
Prove or disprove that T (n) = Θ(n3).
(b) (10%) T (n) = 7T (n4) + 16n2log n
Prove or disprove that T (n) = Θ(n2log n).
(c) (10%) T (n) = 4T (n2) + n2log n
Prove or disprove that T (n) = Θ(n2log2n).
(d) (10%) T (n) = T (n2) + T (n3) + T (n6) + n Prove or disprove that T (n) = Θ(n log n).
(e) (10%) T (n) =√ nT (√
n) + n
Prove or disprove that T (n) = Θ(n log log n).
(2) (50%) Imagine that you are a TA of ADA that is responsible for handling additional enrollment.
However, obviously, the number of students who want to register is far more than the classroom capacity.
After giving most of the enrollment sheets to those with higher priorities, you notice that there is only 1 seat left whereas n students are still waiting. The professor plan to choose the last one based on a raffle: she picks a lucky number k (1 ≤ k ≤ n) and tells you to choose the student with kth largest student ID. We call it “ADA-TA-QQ Problem”.
All people in the class are watching you, making you very nervous. As a TA, you have to solve this very quickly. Unfortunately, the waiting students are not sorted in IDs. Furthermore, you cannot memorize all of their IDs and thus derive the answer easily. What you can do is just comparing IDs or dividing them into groups.
(a) (15%) Please design a Divide-and-Conquer algorithm that solves ADA-TA-QQ Problem in O(n) time and O(n) space. Give the pseudo code and explain your code clearly.
(b) (10%) Prove the correctness of your algorithm.
(c) (15%) Analyze your algorithm, showing that it runs in O(n) time.
(d) (5%) Analyze your algorithm, showing that it runs in O(n) space.
(e) (5%) Justify that the time complexity of ADA-TA-QQ Problem is Θ(n).