forked from daiwb/Algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWakingUp.html
More file actions
26 lines (24 loc) · 6.75 KB
/
Copy pathWakingUp.html
File metadata and controls
26 lines (24 loc) · 6.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<html><body bgcolor="#ffffff" text="#000000"><table><tr><td colspan="2"><h3>Problem Statement</h3></td></tr><tr><td>    </td><td>Alex is sleeping soundly. At any minute T, his sleepiness can be characterized by an integer. Initially, at minute 0, Alex's sleepiness is some unknown integer S.
<br></br><br></br>
Unfortunately, there are several repeatedly ringing alarms disturbing him.
<br></br><br></br>
Starting from minute 1, the following happens. First, Alex's sleepiness increases by <b>D</b>. Then some of the alarms ring, decreasing Alex's sleepiness.
<br></br><br></br>
Formally, alarms' characteristics are given in three vector <int>s <b>period</b>, <b>start</b> and <b>volume</b>. The i-th alarm rings at minutes <b>start</b>[i], <b>start</b>[i] + <b>period</b>[i], <b>start</b>[i] + 2 * <b>period</b>[i], etc., and each time it rings, Alex's sleepiness instantly decreases by <b>volume</b>[i]. If several alarms ring at the same minute, their effects are added up, so each of them decreases Alex's sleepiness by its volume.
<br></br><br></br>
While Alex's sleepiness is positive, he's still sleeping. Once it becomes less than or equal to zero, Alex immediately wakes up. Note that Alex's initial sleepiness can be non-positive. In that case he just wakes up at minute 0.
<br></br><br></br>
You are given the vector <int>s <b>period</b>, <b>start</b>, <b>volume</b>, and the int <b>D</b>. Return the largest possible value of S (Alex's initial sleepiness) such that he will wake up at some moment. If he is guaranteed to wake up for all possible values of S, return -1 instead.
</td></tr><tr><td colspan="2"><h3>Definition</h3></td></tr><tr><td>    </td><td><table><tr><td>Class:</td><td>WakingUp</td></tr><tr><td>Method:</td><td>maxSleepiness</td></tr><tr><td>Parameters:</td><td>vector <int>, vector <int>, vector <int>, int</td></tr><tr><td>Returns:</td><td>int</td></tr><tr><td>Method signature:</td><td>int maxSleepiness(vector <int> period, vector <int> start, vector <int> volume, int D)</td></tr><tr><td colspan="2">(be sure your method is public)</td></tr></table></td></tr><tr><td colspan="2"><h3>Limits</h3></td></tr><tr><td>    </td><td><table><tr><td>Time limit (s):</td><td>2.000</td></tr><tr><td>Memory limit (MB):</td><td>256</td></tr></table></td></tr><tr><td colspan="2"><h3>Notes</h3></td></tr><tr><td align="center" valign="top">-</td><td>It is possible to prove that the answer for any test case fits into a 32-bit signed integer type.</td></tr><tr><td colspan="2"><h3>Constraints</h3></td></tr><tr><td align="center" valign="top">-</td><td><b>period</b> will contain between 1 and 50 elements, inclusive.</td></tr><tr><td align="center" valign="top">-</td><td><b>period</b>, <b>start</b> and <b>volume</b> will contain the same number of elements.</td></tr><tr><td align="center" valign="top">-</td><td>Each element of <b>period</b> will be between 1 and 10, inclusive.</td></tr><tr><td align="center" valign="top">-</td><td><b>start</b>[i] will be between 1 and <b>period</b>[i], inclusive.</td></tr><tr><td align="center" valign="top">-</td><td>Each element of <b>volume</b> will be between 1 and 1000, inclusive.</td></tr><tr><td align="center" valign="top">-</td><td><b>D</b> will be between 1 and 100, inclusive.</td></tr><tr><td colspan="2"><h3>Examples</h3></td></tr><tr><td align="center" nowrap="true">0)</td><td></td></tr><tr><td>    </td><td><table><tr><td><table><tr><td><pre>{2, 3}</pre></td></tr><tr><td><pre>{1, 2}</pre></td></tr><tr><td><pre>{3, 4}</pre></td></tr><tr><td><pre>3</pre></td></tr></table></td></tr><tr><td><pre>Returns: 2</pre></td></tr><tr><td><table><tr><td colspan="2">There are two alarms. The first alarm rings every 2 minutes, starting from minute 1, and has volume 3. The second alarm rings every 3 minutes, starting from minute 2, and has volume 4.
<br></br><br></br>
Here is what would happen for S = 2:
<ul>
<li>At minute 0, Alex's sleepiness is 2.</li>
<li>At minute 1, Alex's sleepiness increases to 5. Then the first alarm rings, decreasing his sleepiness to 2.</li>
<li>At minute 2, Alex's sleepiness increases to 5. Then the second alarm rings, decreasing his sleepiness to 1.</li>
<li>At minute 3, Alex's sleepiness increases to 4. Then the first alarm rings, decreasing his sleepiness to 1.</li>
<li>At minute 4, Alex's sleepiness increases to 4. No alarm rings at this minute.</li>
<li>At minute 5, Alex's sleepiness increases to 7. Then both alarms ring, decreasing his sleepiness to 0, so he wakes up.</li>
</ul>
It can be proven that for any larger S, Alex will never wake up.
</td></tr></table></td></tr></table></td></tr><tr><td align="center" nowrap="true">1)</td><td></td></tr><tr><td>    </td><td><table><tr><td><table><tr><td><pre>{1}</pre></td></tr><tr><td><pre>{1}</pre></td></tr><tr><td><pre>{17}</pre></td></tr><tr><td><pre>17</pre></td></tr></table></td></tr><tr><td><pre>Returns: 0</pre></td></tr><tr><td><table><tr><td colspan="2">For any positive S, Alex will never wake up. It's better not to fall asleep.</td></tr></table></td></tr></table></td></tr><tr><td align="center" nowrap="true">2)</td><td></td></tr><tr><td>    </td><td><table><tr><td><table><tr><td><pre>{1}</pre></td></tr><tr><td><pre>{1}</pre></td></tr><tr><td><pre>{23}</pre></td></tr><tr><td><pre>17</pre></td></tr></table></td></tr><tr><td><pre>Returns: -1</pre></td></tr><tr><td><table><tr><td colspan="2">Each minute Alex's sleepiness decreases by 6. That means he will wake up at some moment, regardless of the initial sleepiness.</td></tr></table></td></tr></table></td></tr><tr><td align="center" nowrap="true">3)</td><td></td></tr><tr><td>    </td><td><table><tr><td><table><tr><td><pre>{9, 2, 5, 5, 7}</pre></td></tr><tr><td><pre>{6, 1, 4, 1, 6}</pre></td></tr><tr><td><pre>{71, 66, 7, 34, 6}</pre></td></tr><tr><td><pre>50</pre></td></tr></table></td></tr><tr><td><pre>Returns: 78</pre></td></tr><tr><td><table><tr><td colspan="2"></td></tr></table></td></tr></table></td></tr><tr><td align="center" nowrap="true">4)</td><td></td></tr><tr><td>    </td><td><table><tr><td><table><tr><td><pre>{5, 6, 5, 3, 8, 3, 4}</pre></td></tr><tr><td><pre>{1, 1, 3, 2, 6, 3, 3}</pre></td></tr><tr><td><pre>{42, 85, 10, 86, 21, 78, 38}</pre></td></tr><tr><td><pre>88</pre></td></tr></table></td></tr><tr><td><pre>Returns: -1</pre></td></tr><tr><td><table><tr><td colspan="2"></td></tr></table></td></tr></table></td></tr></table><p>This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved. </p></body></html>