[Solved] CH08-320201-Homework 10 Longest Ordered Subarray, Sum in triangles and Scuba Diver

$25

File Name: CH08-320201-Homework_10_Longest_Ordered_Subarray,_Sum_in_triangles_and_Scuba_Diver.zip
File Size: 772.44 KB

SKU: [Solved] CH08-320201-Homework 10 Longest Ordered Subarray, Sum in triangles and Scuba Diver Category: Tag:
5/5 - (1 vote)

All problems need to be implemented in python or C++.

Problem 1: Longest ordered subarray

Consider the array A=(a1,a2,an). We call a subarray a succession of numbers in A where the position of any value in the subarray in the array is always bigger than the value of the previous one. Use dynamic programming to find a subarray of maximal ordered length of the array A. You can assume there are no duplicate values in the array and that there exists just one optimal solution.

Example

For A=(8,3,6,50,10,8,100,30,60,40,80) the solution is: (3,6,10,30,60,80) Your program should take an input and spit out an output as follows:

Sample input

8 3 6 50 10 8 100 30 60 40 80

Sample output

3 6 10 30 60 80

Problem 2: Sum in triangles

Consider a triangle formed from n lines (1 < n 100), each line containing natural numbers between [1,10000].

  1. Use dynamic programming to determine the biggest sum of numbers existent between the road fromthe number in the first line and a number from the last line and print the respective road to the output. Each number in this road is seated to the left or to the right of the other value above it.
  2. Analyze the runtime of your solution and compare it to a brute force approach.
  3. Explain why a greedy algorithm does not work for this problem.

Example

The values are displayed for example in this manner:

7

3 8

8 1 0

2 7 4 4

4 5 2 6 5

For this example the resulting sum would be 30.

Your program should take an input and spit out an output as follows:

Sample input

73 88 1 02 7 4 44 5 2 6 5

5

Sample output

30

7 3 8 7 5

Problem 3: Scuba Diver

A scuba diver uses a special equipment for diving. He has a cylinder with two containers: one with oxygen and the other with nitrogen. Depending on the time he wants to stay under water and the depth of diving the scuba diver needs various amount of oxygen and nitrogen. The scuba diver has at his disposal a certain number of cylinders. Each cylinder can be described by its weight and the volume of gas it contains. In order to complete his task the scuba diver needs specific amounts of oxygen and nitrogen. Theoretically, the diver can take as many cylinders as he wants/needs. Use dynamic programming to find the minimal total weight of cylinders he has to take to complete the task and which those cylinders are. In case of several acceptable solutions, printing just one of them is enough.

Example

The scuba diver has at his disposal 5 cylinders described below. Each description consists of: volume of oxygen, volume of nitrogen (both values are given in liters) and weight of the cylinder (given in decagrams):

3 36 12010 25 1295 50 2501 45 1304 20 119

5

If the scuba diver needs 5 liters of oxygen and 60 liters of nitrogen then he has to take two cylinders of total weight 249 (for example the first and the second ones or the fourth and the fifth ones).

Input

The number of test cases c is in the first line of input, then c test cases follow separated by an empty line.

In the first line of a test case there are two integers t,a separated by a single space, 1 t 21 and 1 a 79. They denote volumes of oxygen and nitrogen respectively, needed to complete the task. The second line contains one integer n, 1 n 1000, which is the number of accessible cylinders. The following n lines contain descriptions of cylinders; i-th line contains three integers ti, ai, wi separated by single spaces, (1 ti 21, 1 ai 79,1 wi 800). These are respectively: volume of oxygen and nitrogen in the i-th cylinder and the weight of this cylinder.

Output

On the first line will be printed the total weight wt and on the next line separated by spaces the index of the cylinders in order.

Sample input

15 6053 36 12010 25 1295 50 2501 45 1304 20 119

5

2

Sample output

249 1 2

Reviews

There are no reviews yet.

Only logged in customers who have purchased this product may leave a review.

Shopping Cart
[Solved] CH08-320201-Homework 10 Longest Ordered Subarray, Sum in triangles and Scuba Diver
$25