Datasets:

Modalities:
Image
Text
Formats:
parquet
Size:
< 1K
Tags:
code
Libraries:
Datasets
pandas
License:
hackercup / 2018 /round3 /ethan_max_subarray.html
wjomlex's picture
2018 Problems
ab396f0 verified
raw
history blame
3.64 kB
<p>
In preparation for his final exam, Ethan is doing his fourth programming assignment: finding the subarray with the maximum sum in an array of integers.
</p>
<p>
Given an array of <strong>N</strong> integers <strong>A<sub>1..N</sub></strong>,
Ethan's task is to find the maximum sum of any (possibly empty) contiguous subarray of <strong>A</strong>.
Ethan has implemented an algorithm to solve this problem, described by the following pseudocode:
</p>
<ul>
<li>&nbsp;&nbsp;&nbsp; 1. Set <strong>s</strong> and <strong>m</strong> to both be equal to 0. </li>
<li>&nbsp;&nbsp;&nbsp; 2. Iterate <em>i</em> upwards from 1 to <strong>N</strong>: </li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 2a. If <strong>A<sub>i</sub></strong> &ge; 0, increment <strong>s</strong> by <strong>A<sub>i</sub></strong>,
otherwise set <strong>s</strong> to be equal to 0. </li>
<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 2b. If <strong>s</strong> &gt; <strong>m</strong>, set <strong>m</strong> to be equal to <strong>s</strong>. </li>
<li>&nbsp;&nbsp;&nbsp; 3. Output <strong>m</strong>. </li>
</ul>
<p>
Is there any hope for Ethan? With exasperation, you set out in vain to teach another lesson.
</p>
<p>
The professor of the class has once again left you with some half-written test cases.
You're given an initial array <strong>B<sub>1..M</sub></strong>, such that the absolute value of each element is at most <strong>K</strong>.
You'd like to insert <strong>M</strong> - 1 more integers into the array, one between each pair of adjacent elements in the original array,
to construct a new array <strong>A<sub>1..N</sub></strong> where <strong>N</strong> = 2<strong>M</strong> - 1.
Each of the inserted elements must likewise have an absolute value of at most <strong>K</strong>.
You'll then feed the new array <strong>A</strong> into Ethan's algorithm.
Your goal is to maximize the absolute difference between the final array's correct maximum subarray sum and the output of Ethan's algorithm.
</p>
<h3>Input</h3>
<p>
Input begins with an integer <strong>T</strong>, the number of test cases.
For each test case, there is first a line containing the space-separated integers <strong>M</strong> and <strong>K</strong>.
Then one more line follows containing the <strong>M</strong> space-separated integers <strong>B<sub>1</sub></strong> through <strong>B<sub>M</sub></strong>.
</p>
<h3>Output</h3>
<p>
For the <em>i</em>th test case, output a line containing "Case #<em>i</em>: "
followed by the maximum possible absolute difference between the correct maximum subarray sum and the output of Ethan's algorithm.
</p>
<h3>Constraints</h3>
<p>
1 &le; <strong>T</strong> &le; 60 <br />
1 &le; <strong>M</strong> &le; 50 <br />
1 &le; <strong>K</strong> &le; 50 <br />
-<strong>K</strong> &le; <strong>A<sub>i</sub></strong> &le; <strong>K</strong> <br />
</p>
<h3>Explanation of Sample</h3>
<p>
In the first case, <strong>A</strong> = [3], and both Ethan's answer and the correct answer are equal to 3.
</p>
<p>
In the second case, <strong>A</strong> = [-3], and both Ethan's answer and the correct answer are equal to 0.
</p>
<p>
In the third case, one value will be inserted into <strong>B</strong>, and you should choose to insert -1 to yield <strong>A</strong> = [2, -1, 2].
This results in Ethan's answer being 2 and the correct answer being 3, yielding an absolute answer difference of 1.
</p>
<p>
In the fourth case, there are multiple choices of inserted elements which result in an absolute answer difference of 3.
For example, it's possible for Ethan's answer equal to be made to equal 3 while the correct answer equals 6.
</p>