Skip to content

Commit 62c6195

Browse files
committed
srm 207 div 1 500
1 parent 4974ba4 commit 62c6195

File tree

2 files changed

+154
-0
lines changed

2 files changed

+154
-0
lines changed

TCSocks.cpp

+128
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
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+
#include <stack>
12+
#include <queue>
13+
using namespace std;
14+
typedef pair<int,int> pi;
15+
typedef vector<int> vi;
16+
typedef vector<vi> vvi;
17+
typedef set<int> si;
18+
19+
int maxp = 0, n = 0;
20+
int city[10][105];
21+
int C[10][10], T[10][10];
22+
vi money;
23+
24+
void solve(vi v){
25+
vi uv;
26+
int curr = 0, t = 0, p = 0;
27+
v.push_back(0);
28+
for(int i : v){
29+
t += T[curr][i];
30+
p -= C[curr][i];
31+
p += money[i]/city[i][t];
32+
curr = i;
33+
}
34+
maxp = max(p, maxp);
35+
v.pop_back();
36+
for(int i = 1; i < n; i++)
37+
if(find(v.begin(), v.end(), i) == v.end())
38+
uv.push_back(i);
39+
40+
for(int i : uv){
41+
v.push_back(i);
42+
solve(v);
43+
v.pop_back();
44+
}
45+
}
46+
47+
class TCSocks{
48+
public:
49+
int earnMoney(vector <int> _money, vector <string> cost, vector <string> time, vector <string> competitors) {
50+
51+
money = _money;
52+
n = money.size();
53+
maxp = 0;
54+
for(int i = 0; i < 10; i++)
55+
for(int j = 0; j < 105; j++)
56+
city[i][j] = 1;
57+
58+
for(int i = 0; i < n; i++){
59+
stringstream ss(cost[i]), st(time[i]);
60+
for(int j = 0; j < n; j++){
61+
int k;
62+
ss>>k;
63+
C[i][j] = k;
64+
st>>k;
65+
T[i][j] = k;
66+
}
67+
}
68+
69+
for(string s : competitors){
70+
stringstream ss(s);
71+
int t = 0, j = 0, i = 0;
72+
while(ss>>j){
73+
t += T[i][j];
74+
for(int k = t; k <= 100; k++) city[j][k] *= 2;
75+
i = j;
76+
}
77+
}
78+
79+
for(int i = 1; i < n; i++){
80+
vi s(1, i);
81+
solve(s);
82+
}
83+
84+
return maxp;
85+
}
86+
87+
// BEGIN CUT HERE
88+
public:
89+
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(); if ((Case == -1) || (Case == 4)) test_case_4(); if ((Case == -1) || (Case == 5)) test_case_5(); }
90+
private:
91+
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(); }
92+
void verify_case(int Case, const int &Expected, const int &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } }
93+
void test_case_0() { int Arr0[] = {0, 100, 100, 100}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); string Arr1[] = {"0 50 50 200", "0 0 50 200", "0 10 0 200", "0 0 0 0"}; vector <string> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); string Arr2[] = {"0 1 1 1", "1 0 1 1", "1 1 0 1", "1 1 1 0"}; vector <string> Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[0]))); string Arr3[] = {}; vector <string> Arg3(Arr3, Arr3 + (sizeof(Arr3) / sizeof(Arr3[0]))); int Arg4 = 140; verify_case(0, Arg4, earnMoney(Arg0, Arg1, Arg2, Arg3)); }
94+
void test_case_1() { int Arr0[] = {0, 100, 100, 100}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); string Arr1[] = {"0 50 50 200", "0 0 50 200", "0 10 0 200", "0 0 0 0"}; vector <string> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); string Arr2[] = {"0 1 1 1", "1 0 1 1", "1 1 0 1", "1 1 1 0"}; vector <string> Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[0]))); string Arr3[] = {"3", "2 3 1", "2 1"}; vector <string> Arg3(Arr3, Arr3 + (sizeof(Arr3) / sizeof(Arr3[0]))); int Arg4 = 50; verify_case(1, Arg4, earnMoney(Arg0, Arg1, Arg2, Arg3)); }
95+
void test_case_2() { int Arr0[] = {0, 100, 200}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); string Arr1[] = {"0 20 10", "10 0 20", "20 10 0"}; vector <string> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); string Arr2[] = {"0 1 5", "1 0 1", "1 1 0"}; vector <string> Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[0]))); string Arr3[] = {"2", "2"}; vector <string> Arg3(Arr3, Arr3 + (sizeof(Arr3) / sizeof(Arr3[0]))); int Arg4 = 240; verify_case(2, Arg4, earnMoney(Arg0, Arg1, Arg2, Arg3)); }
96+
void test_case_3() { int Arr0[] = {0, 40, 40, 40, 40, 40}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); string Arr1[] = {"0 25 25 25 25 25", "25 0 25 25 25 25", "25 25 0 25 25 25",
97+
"25 25 25 0 25 25", "25 25 25 25 0 25", "25 25 25 25 25 0"}; vector <string> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); string Arr2[] = {"0 1 1 1 1 1", "1 0 1 1 1 1", "1 1 0 1 1 1", "1 1 1 0 1 1", "1 1 1 1 0 1", "1 1 1 1 1 0"}; vector <string> Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[0]))); string Arr3[] = {"1", "2", "3", "4", "5"}; vector <string> Arg3(Arr3, Arr3 + (sizeof(Arr3) / sizeof(Arr3[0]))); int Arg4 = 0; verify_case(3, Arg4, earnMoney(Arg0, Arg1, Arg2, Arg3)); }
98+
void test_case_4() { int Arr0[] = {0, 70, 70, 70, 70, 70}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); string Arr1[] = {"0 25 25 25 25 25", "25 0 25 25 25 25", "25 25 0 25 25 25",
99+
"25 25 25 0 25 25", "25 25 25 25 0 25", "25 25 25 25 25 0"}; vector <string> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); string Arr2[] = {"0 1 1 1 1 1", "1 0 1 1 1 1", "1 1 0 1 1 1", "1 1 1 0 1 1", "1 1 1 1 0 1", "1 1 1 1 1 0"}; vector <string> Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[0]))); string Arr3[] = {"1", "2", "3", "4", "5"}; vector <string> Arg3(Arr3, Arr3 + (sizeof(Arr3) / sizeof(Arr3[0]))); int Arg4 = 25; verify_case(4, Arg4, earnMoney(Arg0, Arg1, Arg2, Arg3)); }
100+
void test_case_5() { int Arr0[] = {0,457,434,382,818,403,265,449,214}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); string Arr1[] = {"0 204 600 800 885 542 439 823 913",
101+
"32 0 813 687 242 129 34 447 862",
102+
"56 462 0 727 71 309 461 867 200",
103+
"656 96 334 0 178 650 108 477 547",
104+
"89 856 922 495 0 821 374 100 651",
105+
"634 810 319 947 322 0 283 227 286",
106+
"446 416 272 551 243 880 0 47 878",
107+
"390 315 221 765 938 732 747 0 435",
108+
"902 616 166 830 223 406 736 712 0"}; vector <string> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); string Arr2[] = {"0 1 10 6 5 5 4 7 6",
109+
"5 0 2 7 3 2 1 4 2",
110+
"1 9 0 8 6 1 3 9 9",
111+
"2 8 8 0 1 9 10 4 5",
112+
"8 8 2 7 0 5 3 9 1",
113+
"6 8 9 9 3 0 7 4 7",
114+
"10 8 9 10 7 1 0 9 4",
115+
"8 6 5 1 6 6 5 0 4",
116+
"3 8 4 4 6 10 10 3 0"}; vector <string> Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[0]))); string Arr3[] = {"1 8 2 5 4 7 6","6 2 4","8 7"}; vector <string> Arg3(Arr3, Arr3 + (sizeof(Arr3) / sizeof(Arr3[0]))); int Arg4 = 785; verify_case(5, Arg4, earnMoney(Arg0, Arg1, Arg2, Arg3)); }
117+
118+
// END CUT HERE
119+
120+
};
121+
122+
// BEGIN CUT HERE
123+
int main(){
124+
125+
TCSocks ___test;
126+
___test.run_test(-1);
127+
}
128+
// END CUT HERE

TCSocks.html

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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>You wish to earn money selling cute socks from the TC store. You live in Glastonbury, but its citizens are all long-time TopCoders, so you can't sell any socks there. Thus you are going to visit several other cities, find new TopCoders there, and sell them some socks. Unfortunately, several other TC members are also sock salesmen. If one or more sock salesmen visits a city before you, or at the same time as you, your profit in that city will be halved (round down using integer division) once for each of the other salesmen (because some people will buy his socks). Hence, if two people visit the city before you, you will only get one fourth of the potential profit. Also, travelling between cities is not free, so you may lose money if you visit too many cities. However, you have your competitors' plans, so you know which cities they will visit, and in what order they will visit them. Now you are planning your route, and want to maximize your profit.
2+
<br></br><br></br>
3+
You will be given a vector &lt;int&gt;, <b>money</b>, which gives maximal possible earnings for each city. You will also be given a vector &lt;string&gt;, <b>costs</b>, which contains the costs of getting from one city to another. A vector &lt;string&gt;, <b>times</b>, gives you the number of days it takes to get from one city to another (you may assume that it takes no time to sell socks once you get to a city).
4+
Both <b>costs</b> and <b>times</b> will be formatted in the same way. If they both have K elements, then each element of <b>costs</b> and <b>times</b> will contain K integers, each separated by a single space. The j<sup>th</sup> integer in the i<sup>th</sup> element of <b>times</b> will represent the number of days it takes to get from city i to city j. Similarly, the j<sup>th</sup> integer in the i<sup>th</sup> element of <b>costs</b> will represent the cost of travelling from city i to city j.
5+
Also, a vector &lt;string&gt;, <b>competitors</b>, gives you the routes of your competitors. Each element of <b>competitors</b> will be formatted as &quot;N<sub>1</sub> N<sub>2</sub> ... N<sub>k</sub>&quot;, where N<sub>1</sub> N<sub>2</sub> ... N<sub>k</sub> are the numbers of the cities a competitor will visit (so first he will go to the city N<sub>1</sub>, then to the city N<sub>2</sub> and so on).
6+
<br></br><br></br>
7+
Your method must plan the route that maximizes your profit. You will start in Glastonbury (city 0) and then visit any number of the cities (not visiting a particular city more than once), before finally returning to Glastonbury. All your competitors start their routes in Glastonbury at the same time as you, and they sell socks along the routes specified in <b>competitors</b>. It takes them the same amount of time to travel between cities as it takes you. Your method should return your maximum possible profit.</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>TCSocks</td></tr><tr><td>Method:</td><td>earnMoney</td></tr><tr><td>Parameters:</td><td>vector &lt;int&gt;, vector &lt;string&gt;, vector &lt;string&gt;, vector &lt;string&gt;</td></tr><tr><td>Returns:</td><td>int</td></tr><tr><td>Method signature:</td><td>int earnMoney(vector &lt;int&gt; money, vector &lt;string&gt; cost, vector &lt;string&gt; time, vector &lt;string&gt; competitors)</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>64</td></tr></table></td></tr><tr><td colspan="2"><h3>Notes</h3></td></tr><tr><td align="center" valign="top">-</td><td>You must finish your route in city 0.</td></tr><tr><td align="center" valign="top">-</td><td>You may not visit a city more than once.</td></tr><tr><td colspan="2"><h3>Constraints</h3></td></tr><tr><td align="center" valign="top">-</td><td><b>money</b> will contain between 1 and 10 elements, inclusive.</td></tr><tr><td align="center" valign="top">-</td><td><b>times</b>, <b>money</b> and <b>costs</b> will each have the same number of elements.</td></tr><tr><td align="center" valign="top">-</td><td>Each element of <b>money</b> will be between 0 and 1000, inclusive.</td></tr><tr><td align="center" valign="top">-</td><td>The first element of <b>money</b> will be 0.</td></tr><tr><td align="center" valign="top">-</td><td>Each element of <b>times</b> and <b>costs</b> will contain K single-space delimited integers, where K is the number of elements in <b>times</b> and <b>costs</b>.</td></tr><tr><td align="center" valign="top">-</td><td>Each integer in <b>costs</b> will be between 0 and 1000, inclusive, and will contain no extra leading zeros.</td></tr><tr><td align="center" valign="top">-</td><td>Each integer in <b>times</b> will be between 1 and 10, inclusive, and will contain no extra leading zeros.</td></tr><tr><td align="center" valign="top">-</td><td>The i<sup>th</sup> integers of the i<sup>th</sup> elements of <b>times</b> and <b>costs</b> will be 0.</td></tr><tr><td align="center" valign="top">-</td><td><b>competitors</b> will contain between 0 and 10 elements, inclusive.</td></tr><tr><td align="center" valign="top">-</td><td>Each element of <b>competitors</b> will be formatted as a single-space delimited list of 1 or more integers.</td></tr><tr><td align="center" valign="top">-</td><td>Each integer in each element of <b>competitors</b> will be between 1 and the number of elements in <b>money</b>-1, inclusive.</td></tr><tr><td align="center" valign="top">-</td><td>None of your competitors will visit any city more than once.</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>{0, 100, 100, 100}</pre></td></tr><tr><td><pre>{&quot;0 50 50 200&quot;, &quot;0 0 50 200&quot;, &quot;0 10 0 200&quot;, &quot;0 0 0 0&quot;}</pre></td></tr><tr><td><pre>{&quot;0 1 1 1&quot;, &quot;1 0 1 1&quot;, &quot;1 1 0 1&quot;, &quot;1 1 1 0&quot;}</pre></td></tr><tr><td><pre>{}</pre></td></tr></table></td></tr><tr><td><pre>Returns: 140</pre></td></tr><tr><td><table><tr><td colspan="2">You have no competitors. Your best path is 0 -&gt; 2 -&gt; 1 -&gt; 0.
8+
You spend 50 + 10 + 0 units, and earn 200 units. So the total income is 140.</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>{0, 100, 100, 100}</pre></td></tr><tr><td><pre>{&quot;0 50 50 200&quot;, &quot;0 0 50 200&quot;, &quot;0 10 0 200&quot;, &quot;0 0 0 0&quot;}</pre></td></tr><tr><td><pre>{&quot;0 1 1 1&quot;, &quot;1 0 1 1&quot;, &quot;1 1 0 1&quot;, &quot;1 1 1 0&quot;}</pre></td></tr><tr><td><pre>{&quot;3&quot;, &quot;2 3 1&quot;, &quot;2 1&quot;}</pre></td></tr></table></td></tr><tr><td><pre>Returns: 50</pre></td></tr><tr><td><table><tr><td colspan="2">The same data, but now you have three competitors. These competitors decrease your earnings in city 2, so you just visit city 1, and return back to Glastonbury.</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>{0, 100, 200}</pre></td></tr><tr><td><pre>{&quot;0 20 10&quot;, &quot;10 0 20&quot;, &quot;20 10 0&quot;}</pre></td></tr><tr><td><pre>{&quot;0 1 5&quot;, &quot;1 0 1&quot;, &quot;1 1 0&quot;}</pre></td></tr><tr><td><pre>{&quot;2&quot;, &quot;2&quot;}</pre></td></tr></table></td></tr><tr><td><pre>Returns: 240</pre></td></tr><tr><td><table><tr><td colspan="2">Both your competitors want to visit city 2 first. Nevertheless, you can leave them behind, visiting city 1, then city 2, and returing back home.</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>{0, 40, 40, 40, 40, 40}</pre></td></tr><tr><td><pre>{&quot;0 25 25 25 25 25&quot;, &quot;25 0 25 25 25 25&quot;, &quot;25 25 0 25 25 25&quot;,
9+
&quot;25 25 25 0 25 25&quot;, &quot;25 25 25 25 0 25&quot;, &quot;25 25 25 25 25 0&quot;}</pre></td></tr><tr><td><pre>{&quot;0 1 1 1 1 1&quot;, &quot;1 0 1 1 1 1&quot;, &quot;1 1 0 1 1 1&quot;, &quot;1 1 1 0 1 1&quot;, &quot;1 1 1 1 0 1&quot;, &quot;1 1 1 1 1 0&quot;}</pre></td></tr><tr><td><pre>{&quot;1&quot;, &quot;2&quot;, &quot;3&quot;, &quot;4&quot;, &quot;5&quot;}</pre></td></tr></table></td></tr><tr><td><pre>Returns: 0</pre></td></tr><tr><td><table><tr><td colspan="2">Here, staying at home is your best choice, because any trip is unprofitable.</td></tr></table></td></tr></table></td></tr><tr><td align="center" nowrap="true">4)</td><td></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td><table><tr><td><pre>{0, 70, 70, 70, 70, 70}</pre></td></tr><tr><td><pre>{&quot;0 25 25 25 25 25&quot;, &quot;25 0 25 25 25 25&quot;, &quot;25 25 0 25 25 25&quot;,
10+
&quot;25 25 25 0 25 25&quot;, &quot;25 25 25 25 0 25&quot;, &quot;25 25 25 25 25 0&quot;}</pre></td></tr><tr><td><pre>{&quot;0 1 1 1 1 1&quot;, &quot;1 0 1 1 1 1&quot;, &quot;1 1 0 1 1 1&quot;, &quot;1 1 1 0 1 1&quot;, &quot;1 1 1 1 0 1&quot;, &quot;1 1 1 1 1 0&quot;}</pre></td></tr><tr><td><pre>{&quot;1&quot;, &quot;2&quot;, &quot;3&quot;, &quot;4&quot;, &quot;5&quot;}</pre></td></tr></table></td></tr><tr><td><pre>Returns: 25</pre></td></tr><tr><td><table><tr><td colspan="2">The same case, except cities give you bigger income. Visiting just one city is still unprofitable, and visiting any two cities still isn't good idea, but you will profit 25 units for visiting all of them in any order.</td></tr></table></td></tr></table></td></tr><tr><td align="center" nowrap="true">5)</td><td></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td><table><tr><td><pre>{0,457,434,382,818,403,265,449,214}</pre></td></tr><tr><td><pre>{&quot;0 204 600 800 885 542 439 823 913&quot;,
11+
&quot;32 0 813 687 242 129 34 447 862&quot;,
12+
&quot;56 462 0 727 71 309 461 867 200&quot;,
13+
&quot;656 96 334 0 178 650 108 477 547&quot;,
14+
&quot;89 856 922 495 0 821 374 100 651&quot;,
15+
&quot;634 810 319 947 322 0 283 227 286&quot;,
16+
&quot;446 416 272 551 243 880 0 47 878&quot;,
17+
&quot;390 315 221 765 938 732 747 0 435&quot;,
18+
&quot;902 616 166 830 223 406 736 712 0&quot;}</pre></td></tr><tr><td><pre>{&quot;0 1 10 6 5 5 4 7 6&quot;,
19+
&quot;5 0 2 7 3 2 1 4 2&quot;,
20+
&quot;1 9 0 8 6 1 3 9 9&quot;,
21+
&quot;2 8 8 0 1 9 10 4 5&quot;,
22+
&quot;8 8 2 7 0 5 3 9 1&quot;,
23+
&quot;6 8 9 9 3 0 7 4 7&quot;,
24+
&quot;10 8 9 10 7 1 0 9 4&quot;,
25+
&quot;8 6 5 1 6 6 5 0 4&quot;,
26+
&quot;3 8 4 4 6 10 10 3 0&quot;}</pre></td></tr><tr><td><pre>{&quot;1 8 2 5 4 7 6&quot;,&quot;6 2 4&quot;,&quot;8 7&quot;}</pre></td></tr></table></td></tr><tr><td><pre>Returns: 785</pre></td></tr><tr><td></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)