GymTraining
SRM 421 · 2008-10-07 · by ivan_metelsky
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.
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
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.
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.
200 50 200 150 1 Returns: 30050
In this case, you need a lot of time to finish your training.
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.
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