forked from keshavnandan/Topcoder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConnectingCars.html
25 lines (25 loc) · 5.3 KB
/
ConnectingCars.html
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
<html><body bgcolor="#000000" text="#ffffff"><table><tr><td colspan="2"><h3>Problem Statement</h3></td></tr><tr><td>    </td><td><p>Janusz works in roller coaster maintenance.
The station of the roller coaster is a long straight segment of railroad tracks.
There are some cars on those tracks.
The cars are currently not attached to each other, and there may be gaps between some of them.
Janusz has to push them all together and connect them into a train.
</p><p>
You are given the vector <int>s <b>positions</b> and <b>lengths</b>.
For each valid i, there is a car that is <b>lengths</b>[i] meters long and starts <b>positions</b>[i] meters from the beginning of the station.
(In other words, the coordinates currently occupied by this car are in the interval from <b>positions</b>[i] to <b>positions</b>[i]+<b>lengths</b>[i].)
</p><p>
Moving a single car one meter in either direction costs Janusz one unit of energy.
Compute the smallest total amount of energy sufficient to push all cars together.
In the final configuration the cars must be located one after another with no gaps between them.
</p><p>
(Note that there is no restriction on the movement of cars or on the final position of the train.
Janusz may push the cars in any order, and he may even push some cars by a non-integer number of meters if he wants to.)
</p></td></tr><tr><td colspan="2"><h3>Definition</h3></td></tr><tr><td>    </td><td><table><tr><td>Class:</td><td>ConnectingCars</td></tr><tr><td>Method:</td><td>minimizeCost</td></tr><tr><td>Parameters:</td><td>vector <int>, vector <int></td></tr><tr><td>Returns:</td><td>long long</td></tr><tr><td>Method signature:</td><td>long long minimizeCost(vector <int> positions, vector <int> lengths)</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><tr><td>Stack 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>You may assume that the optimal answer is always an integer that fits into a signed 64-bit integer data type.</td></tr><tr><td colspan="2"><h3>Constraints</h3></td></tr><tr><td align="center" valign="top">-</td><td><b>lengths</b> and <b>positions</b> will have the same number of elements.</td></tr><tr><td align="center" valign="top">-</td><td><b>lengths</b> will have between 2 and 50 elements, inclusive.</td></tr><tr><td align="center" valign="top">-</td><td>Each element of <b>lengths</b> and <b>positions</b> will be between 1 and 10^9, inclusive.</td></tr><tr><td align="center" valign="top">-</td><td>The segments occupied by the cars may touch but they will not overlap.</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>{1, 3, 10, 20}</pre></td></tr><tr><td><pre>{2, 2, 5, 3}</pre></td></tr></table></td></tr><tr><td><pre>Returns: 15</pre></td></tr><tr><td><table><tr><td colspan="2">There are four cars.
The intervals currently occupied by the cars are (1,3), (3,5), (10,15), and (20,23).
In one optimal solution Janusz would move each of the first two cars three meters to the right, the third car two meters to the left, and the fourth car seven meters to the left.
At the end, the cars occupy the intervals (4,6), (6,8), (8,13), and (13,16).
Total energy spent: 3+3+2+7 = 15.</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>{100, 50, 1}
</pre></td></tr><tr><td><pre>{10, 2, 1}</pre></td></tr></table></td></tr><tr><td><pre>Returns: 96</pre></td></tr><tr><td><table><tr><td colspan="2">There are three cars.
The gaps between consecutive cars have 48 meters each.
The best solution is to keep the middle car in place and to push the other two towards it.
This requires 48+48 = 96 units of energy.</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>{4, 10, 100, 13, 80}</pre></td></tr><tr><td><pre>{5, 3, 42, 40, 9}</pre></td></tr></table></td></tr><tr><td><pre>Returns: 66</pre></td></tr><tr><td><table><tr><td colspan="2"></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>{5606451, 63581020, 81615191, 190991272, 352848147, 413795385, 468408016, 615921162, 760622952, 791438427}</pre></td></tr><tr><td><pre>{42643329, 9909484, 58137134, 99547272, 39849232, 15146704, 144630245, 604149, 15591965, 107856540}</pre></td></tr></table></td></tr><tr><td><pre>Returns: 1009957100</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>