TopCoder problem "GymTraining" used in SRM 421 (Division II Level One)



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.

 

Definition

    
Class:GymTraining
Method:trainingTime
Parameters:int, int, int, int, int
Returns:int
Method signature:int trainingTime(int needToTrain, int minPulse, int maxPulse, int trainChange, int restChange)
(be sure your method is public)
    
 

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

Problem url:

http://www.topcoder.com/stat?c=problem_statement&pm=10105

Problem stats url:

http://www.topcoder.com/tc?module=ProblemDetail&rd=13512&pm=10105

Writer:

ivan_metelsky

Testers:

PabloGilberto , Yarin , Olexiy

Problem categories:

Simulation