COMP3023 Programming Assignment


Shooting Competition

Philoctetes is a sharp shooter. One day he is invited by a shooting competition. In the competition, each shooter is given an array of n targets. Every target has a value, a positive integer. If one target t is shot, the shooter’s scores will be increased by

v1 × v2 × v3

where v1 is the value on the target t, v2 is the value on t’s left neighbor, and v3 is the value on t’s right neighbor.

You need to design a greed algorithm and a dynamic programming algorithm to fifind an shooting order to maximize the score earned by Philoctetes under the following assumptions.

1.    Philoctetes never misses any target.

2.    Shooters know the value on every target before shooting.

3.    Each target has only two neighbors, left and right.

4.    If one target is shot, its left and right neighbors will be neighbors after-wards.

5.    Pretend that the fifirst target has an artifificial left neighbor - target 0.

       Philoctetes cannot shoot target 0, but the target has value 1 and will be counted in the score calculation.

6.    Similarly, the right nieghbor of the last target is also an artifificial target n + 1 of value 1, which cannot be shot.


Requirements

1. Algorithms

(a)    The greedy algorithm runs in O(n log n) time and uses O(1) space.

        The greedy algorithm is not required to always fifind the maximum score.

(b)    The dynamic programming runs in O(n2 ) time and uses O(n2 ) space.

         It always gives the maximum score.


2.Program

(a)    You need to implement your algorithms in C.

(b)    Your program reads a test case from “input.txt”.

(c)    “input.txt” only contains the values for each target, one value on each line.

(d)    Your program outputs the resulting score on the screen and the cor-responding shooting sequence to “output.txt”, one index of a target on each line.


3. Report

(a)    Your report should contain all the notations that you have used to model the problem,

(b)    the recurrent relation for the dynamic programming,

(c)    step-by-step pseudo codes for the two algorithms with clearly de-scribed inputs and outputs,

(d)    an instance such that your greedy algorithm cannot fifind the maxi-mum solution,

(e)    space and time complexity anlysis of your algorithms.

(f)     Your report should be 2 to 3 pages long, depends how you write.


4. Submission includs your code and your report. Name the package as “COMP3023 _ 20F _ PA #########.zip”, where ######### is your student ID.


Example

Suppose Philoctetes is given 4 targes of values 2, 1, 4, 3 (see “input example.txt”), the maximum score is 41 because

Iteration
Target index
Score
Values of the remaining targets
1
2
3
4
Target 2 (valued 1)
Target 3 (valued 4)
Target 1 (valued 2)
Target 4 (valued 3)
0 + 2 × 1 × 4
8 + 2 × 4 × 3
32 + 1 × 2 × 3
38 + 1 × 3 × 1
2, 4, 3
2, 3
3
Empty


The shooting sequence 2, 3, 1, 4 is in “output example.txt”.