Connection Status:
Competition Arena > GymTraining
SRM 421 · 2008-10-07 · by ivan_metelsky · Simulation
Class Name: GymTraining
Return Type: int
Method Name: trainingTime
Arg Types: (int, int, int, int, int)
Problem Statement

Problem Statement

You are at the gym and you want to do some training. The training process is divided into one-minute segments. During each minute, you can either train or rest.

If you choose to train during a minute, it increases your pulse by trainChange. That is, if your pulse was X, it becomes X + trainChange after a minute of training. You never want your pulse to exceed maxPulse, so you can train only if X + trainChange is less than or equal to maxPulse.

If you choose to rest during a minute, it decreases your pulse by restChange. That is, if your pulse was X, it becomes X - restChange after a minute of rest. However, your pulse never falls below minPulse, so if X - restChange is less than minPulse, your pulse becomes minPulse instead of X - restChange.

Your pulse is initially minPulse. You want to train for a total of needToTrain minutes (these minutes don't need to be consecutive). Return the minimum number of minutes your complete training process will take. If you can't train for needToTrain minutes, return -1 instead.

Constraints

  • minPulse will be between 50 and 200, inclusive.
  • maxPulse will be between minPulse and 200, inclusive.
  • needToTrain, trainChange and restChange will each be between 1 and 200, inclusive.
Examples
0)
5
70
120
25
15
Returns: 10

Your training process goes as follows: Time Activity Pulse after activity 1 train 95 2 train 120 3 rest 105 4 rest 90 5 train 115 6 rest 100 7 rest 85 8 train 110 9 rest 95 10 train 120

1)
100
50
100
5
200
Returns: 109

After 10 consecutive minutes of training, your pulse rises from 50 to 100. Then, after a minute of rest, it falls back to 50. You will do this nine times and then finish with 10 consecutive minutes of training, for a total of 9*(10+1)+10 = 109 minutes.

2)
1
60
70
11
11
Returns: -1

A minute of training raises your pulse from 60 to 71, which exceeds your maximum pulse. Therefore, it is not possible to do any training.

3)
200
50
200
150
1
Returns: 30050

In this case, you need a lot of time to finish your training.

4)
19
89
143
17
13
Returns: 40

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

Coding Area

Language: C++17 · define a public class GymTraining with a public method int trainingTime(int needToTrain, int minPulse, int maxPulse, int trainChange, int restChange) · 100 test cases · 2 s / 256 MB per case

Submitting as anonymous