forked from daiwb/Algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRoadOrFlightHard.html
More file actions
16 lines (16 loc) · 6.19 KB
/
Copy pathRoadOrFlightHard.html
File metadata and controls
16 lines (16 loc) · 6.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<html><body bgcolor="#ffffff" text="#000000"><table><tr><td colspan="2"><h3>Problem Statement</h3></td></tr><tr><td>    </td><td>The king has been out to work for a long time and he wants to go back to his queen as fast as possible. The king is in city 0 and the queen is in city <b>N</b>. There are roads and flights connecting city i to city (i+1) for all i between 0 and <b>N</b>-1, inclusive.<br></br>
<br></br>
The time it takes to travel from city i to city (i+1) by road and by flight is given by the i-th elements of <b>roadTime</b> and <b>flightTime</b>, respectively. <b>roadTime</b> can be generated from the following pseudocode:<br></br>
<pre>
roadTime[0] = <b>roadFirst</b> mod <b>roadMod</b>;
for i = 1 to N-1
roadTime[i] = (roadTime[i-1]*<b>roadProd</b> + <b>roadAdd</b>) mod <b>roadMod</b>;
// note that (roadTime[i-1]*<b>roadProd</b> + <b>roadAdd</b>) may overflow a 32-bit integer
</pre>
<b>flightTime</b> can be generated similarly by using <b>flightFirst</b>, <b>flightProd</b>, <b>flightAdd</b>, <b>flightMod</b>.<br></br>
<br></br>
However, taking a flight risks the life of the king during takeoffs due to the technological limitations in the kingdom. Hence the queen has asked him to ensure that the total number of takeoffs during his entire journey does not exceed <b>K</b>.<br></br>
<br></br>
To minimize the number of takeoffs, the king may choose to take a direct flight from city i to city i+j instead of separate flights from city i to city i+1, then from i+1 to i+2, ... and from i+(j-1) to i+j. The time taken for this flight is the sum of the times taken for the flights from i to i+1, i+1 to i+2, ..., i+(j-1) to i+j.<br></br>
<br></br>
Return the minimum amount of time in which the king can reach his queen.<br></br></td></tr><tr><td colspan="2"><h3>Definition</h3></td></tr><tr><td>    </td><td><table><tr><td>Class:</td><td>RoadOrFlightHard</td></tr><tr><td>Method:</td><td>minTime</td></tr><tr><td>Parameters:</td><td>int, int, int, int, int, int, int, int, int, int</td></tr><tr><td>Returns:</td><td>long long</td></tr><tr><td>Method signature:</td><td>long long minTime(int N, int roadFirst, int roadProd, int roadAdd, int roadMod, int flightFirst, int flightProd, int flightAdd, int flightMod, int K)</td></tr><tr><td colspan="2">(be sure your method is public)</td></tr></table></td></tr><tr><td>    </td></tr><tr><td></td></tr><tr><td colspan="2"><h3>Constraints</h3></td></tr><tr><td align="center" valign="top">-</td><td><b>N</b> will be between 1 and 400000, inclusive.</td></tr><tr><td align="center" valign="top">-</td><td><b>roadFirst</b>, <b>roadAdd</b>, <b>flightFirst</b> and <b>flightAdd</b> will each be between 0 and 100000, inclusive.</td></tr><tr><td align="center" valign="top">-</td><td><b>roadProd</b>, <b>roadMod</b>, <b>flightProd</b> and <b>flightMod</b> will each be between 1 and 100000, inclusive.</td></tr><tr><td align="center" valign="top">-</td><td><b>K</b> will be between 0 and 40, inclusive.</td></tr><tr><td align="center" valign="top">-</td><td><b>K</b> will be less than or equal to <b>N</b>.</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>3</pre></td></tr><tr><td><pre>14</pre></td></tr><tr><td><pre>1</pre></td></tr><tr><td><pre>2</pre></td></tr><tr><td><pre>10</pre></td></tr><tr><td><pre>18</pre></td></tr><tr><td><pre>1</pre></td></tr><tr><td><pre>10</pre></td></tr><tr><td><pre>17</pre></td></tr><tr><td><pre>1</pre></td></tr></table></td></tr><tr><td><pre>Returns: 14</pre></td></tr><tr><td><table><tr><td colspan="2">The pseudocode gives <b>roadTime</b> = {4, 6, 8} and <b>flightTime</b> = {1, 11, 4}. The fastest way to reach the queen is to take the road from city 0 to 1 and 1 to 2, and a flight from city 2 to 3. </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>3</pre></td></tr><tr><td><pre>4</pre></td></tr><tr><td><pre>1</pre></td></tr><tr><td><pre>2</pre></td></tr><tr><td><pre>10</pre></td></tr><tr><td><pre>1</pre></td></tr><tr><td><pre>1</pre></td></tr><tr><td><pre>10</pre></td></tr><tr><td><pre>17</pre></td></tr><tr><td><pre>2</pre></td></tr></table></td></tr><tr><td><pre>Returns: 11</pre></td></tr><tr><td><table><tr><td colspan="2"><b>roadTime</b> and <b>flightTime</b> are the same as in previous example. But now the king is allowed 2 takeoffs.</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>3</pre></td></tr><tr><td><pre>4</pre></td></tr><tr><td><pre>1</pre></td></tr><tr><td><pre>2</pre></td></tr><tr><td><pre>10</pre></td></tr><tr><td><pre>1</pre></td></tr><tr><td><pre>1</pre></td></tr><tr><td><pre>6</pre></td></tr><tr><td><pre>9</pre></td></tr><tr><td><pre>1</pre></td></tr></table></td></tr><tr><td><pre>Returns: 12</pre></td></tr><tr><td><table><tr><td colspan="2"><b>roadTime</b> = {4, 6, 8} and <b>flightTime</b> = {1, 7, 4}. Even though <b>roadTime</b>[1] < <b>flightTime</b>[1], it is best to take a direct flight from city 0 to city 3 which takes a total time of 1 + 7 + 4 = 12 units. </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>5</pre></td></tr><tr><td><pre>85739</pre></td></tr><tr><td><pre>94847</pre></td></tr><tr><td><pre>93893</pre></td></tr><tr><td><pre>98392</pre></td></tr><tr><td><pre>92840</pre></td></tr><tr><td><pre>93802</pre></td></tr><tr><td><pre>93830</pre></td></tr><tr><td><pre>92790</pre></td></tr><tr><td><pre>3</pre></td></tr></table></td></tr><tr><td><pre>Returns: 122365</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>