Connection Status:
Competition Arena > ChangingSounds
SRM 366 · 2007-09-18 · by jthread · Dynamic Programming
Class Name: ChangingSounds
Return Type: int
Method Name: maxFinal
Arg Types: (vector<int>, int, int)
Problem Statement

Problem Statement

You are a guitar player and you are going to play a concert. You don't like to play all the songs at the same volume, so you decide to change the volume level of your guitar before each new song. Before the concert begins, you make a list of the number of intervals you will be changing your volume level by before each song. For each volume change, you will decide whether to add that number of intervals to the volume, or substract it.


You are given a int[] changeIntervals, the i-th element of which is the number of intervals you will change your volume by before the i-th song. You are also given an int beginLevel, the initial volume of your guitar, and an int maxLevel, the highest volume setting of your guitar. You cannot change the volume of your guitar to a level above maxLevel or below 0 (but exactly 0 is no problem). Return the maximum volume you can use to play the last song. If there is no way to go through the list without exceeding maxLevel or going below 0, return -1.

Constraints

  • changeIntervals will contain between 1 and 50 elements, inclusive.
  • Each element of changeIntervals will be between 1 and maxLevel, inclusive.
  • maxLevel will be between 1 and 1000, inclusive.
  • beginLevel will be between 0 and maxLevel, inclusive.
Examples
0)
{5, 3, 7}
5
10
Returns: 10

First we turn the volume down to 0 (-5), then we turn it up to 3 (+3), and then up to 10 (+7) for the last song.

1)
{15, 2, 9, 10}
8
20
Returns: -1

Adding 15 to 8 or substracting 15 from 8 both give an invalid sound level.

2)
{74,39,127,95,63,140,99,96,154,18,137,162,14,88}
40
243
Returns: 238
3)
{214,40,203,333,118,575,217,386,319,548,137,34,415,473,140,309,333,314,358,17,330,301,188,241,542,49,226,65,423,452,288,369,281,288,485,460,506,636,520,262,489,493,405,227,507,384,544,666,118,582}
367
1000
Returns: 999
4)
{483,430,157,657,360,424,446,481,2,531,360,154,234,22,492,633,244,338,393,125,298,386,448,211,624,602,60,43,109,341,483,582,280,503,444,277,645,247,186,233,86,530,264,208,466,65,471,644,210,330}
551
1000
Returns: 999

Submissions are judged against all 119 archived test cases, of which 5 are shown here. Case numbers match the judge’s.

Coding Area

Language: C++17 · define a public class ChangingSounds with a public method int maxFinal(vector<int> changeIntervals, int beginLevel, int maxLevel) · 119 test cases · 2 s / 256 MB per case

Submitting as anonymous