Skip to content

Commit 4cdba68

Browse files
committed
srm 645 div 2 500
1 parent 8a21fb1 commit 4cdba68

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed

ConnectingCars.cpp

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#include <iostream>
2+
#include <sstream>
3+
#include <vector>
4+
#include <algorithm>
5+
#include <set>
6+
#include <map>
7+
#include <queue>
8+
#include <cstring>
9+
#include <climits>
10+
#include <cstdio>
11+
using namespace std;
12+
typedef long long ll;
13+
typedef pair<int, int> pi;
14+
15+
class ConnectingCars{
16+
public:
17+
ll minimizeCost(vector <int> positions, vector <int> lengths) {
18+
int n = positions.size();
19+
long long res = 0;
20+
vector<pi> v;
21+
for(int i = 0; i < n; i++)
22+
v.push_back(make_pair(positions[i], positions[i]+lengths[i]));
23+
sort(v.begin(), v.end());
24+
25+
for(int i = 0; i+1 < n; i++){
26+
ll diff = abs(v[i].second - v[i+1].first);
27+
res += min(diff*(i+1), diff*(n-i-1));
28+
}
29+
return res;
30+
}
31+
32+
// BEGIN CUT HERE
33+
public:
34+
void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0(); if ((Case == -1) || (Case == 1)) test_case_1(); if ((Case == -1) || (Case == 2)) test_case_2(); if ((Case == -1) || (Case == 3)) test_case_3(); }
35+
private:
36+
template <typename T> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); }
37+
void verify_case(int Case, const long long &Expected, const long long &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } }
38+
void test_case_0() { int Arr0[] = {1, 3, 10, 20}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {2, 2, 5, 3}; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); long long Arg2 = 15LL; verify_case(0, Arg2, minimizeCost(Arg0, Arg1)); }
39+
void test_case_1() { int Arr0[] = {100, 50, 1}
40+
; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {10, 2, 1}; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); long long Arg2 = 96LL; verify_case(1, Arg2, minimizeCost(Arg0, Arg1)); }
41+
void test_case_2() { int Arr0[] = {4, 10, 100, 13, 80}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {5, 3, 42, 40, 9}; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); long long Arg2 = 66LL; verify_case(2, Arg2, minimizeCost(Arg0, Arg1)); }
42+
void test_case_3() { int Arr0[] = {5606451, 63581020, 81615191, 190991272, 352848147, 413795385, 468408016, 615921162, 760622952, 791438427}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {42643329, 9909484, 58137134, 99547272, 39849232, 15146704, 144630245, 604149, 15591965, 107856540}; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); long long Arg2 = 1009957100LL; verify_case(3, Arg2, minimizeCost(Arg0, Arg1)); }
43+
44+
// END CUT HERE
45+
46+
};
47+
48+
// BEGIN CUT HERE
49+
int main(){
50+
51+
ConnectingCars ___test;
52+
___test.run_test(-1);
53+
}
54+
// END CUT HERE

ConnectingCars.html

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<html><body bgcolor="#000000" text="#ffffff"><table><tr><td colspan="2"><h3>Problem Statement</h3></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><p>Janusz works in roller coaster maintenance.
2+
The station of the roller coaster is a long straight segment of railroad tracks.
3+
There are some cars on those tracks.
4+
The cars are currently not attached to each other, and there may be gaps between some of them.
5+
Janusz has to push them all together and connect them into a train.
6+
</p><p>
7+
You are given the vector &lt;int&gt;s <b>positions</b> and <b>lengths</b>.
8+
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.
9+
(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].)
10+
</p><p>
11+
Moving a single car one meter in either direction costs Janusz one unit of energy.
12+
Compute the smallest total amount of energy sufficient to push all cars together.
13+
In the final configuration the cars must be located one after another with no gaps between them.
14+
</p><p>
15+
(Note that there is no restriction on the movement of cars or on the final position of the train.
16+
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.)
17+
</p></td></tr><tr><td colspan="2"><h3>Definition</h3></td></tr><tr><td>&#160;&#160;&#160;&#160;</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 &lt;int&gt;, vector &lt;int&gt;</td></tr><tr><td>Returns:</td><td>long long</td></tr><tr><td>Method signature:</td><td>long long minimizeCost(vector &lt;int&gt; positions, vector &lt;int&gt; 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>&#160;&#160;&#160;&#160;</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>&#160;&#160;&#160;&#160;</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.
18+
The intervals currently occupied by the cars are (1,3), (3,5), (10,15), and (20,23).
19+
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.
20+
At the end, the cars occupy the intervals (4,6), (6,8), (8,13), and (13,16).
21+
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>&#160;&#160;&#160;&#160;</td><td><table><tr><td><table><tr><td><pre>{100, 50, 1}
22+
</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.
23+
The gaps between consecutive cars have 48 meters each.
24+
The best solution is to keep the middle car in place and to push the other two towards it.
25+
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>&#160;&#160;&#160;&#160;</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>&#160;&#160;&#160;&#160;</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>

0 commit comments

Comments
 (0)