Skip to content

Commit 3171fec

Browse files
committed
srm 181 div 1 1000
1 parent df2ddfc commit 3171fec

File tree

2 files changed

+134
-0
lines changed

2 files changed

+134
-0
lines changed

KiloManX.cpp

+113
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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 pair<int,int> pi;
13+
typedef vector<int> vi;
14+
typedef vector<vi> vvi;
15+
typedef vector<string> vs;
16+
typedef vector<vs> vvs;
17+
#define inf 1000000000
18+
int D[20];
19+
int M[20][20];
20+
int n;
21+
22+
struct node{
23+
24+
int state;
25+
int cost;
26+
node(int s, int c) : state(s), cost(c) {}
27+
};
28+
29+
bool operator<(node a, node b){
30+
return a.cost > b.cost;
31+
}
32+
33+
class KiloManX {
34+
public:
35+
36+
int leastShots(vs damageChart, vi bossHealth)
37+
{
38+
map<int, int> H;
39+
n = bossHealth.size();
40+
for(int i = 0; i <= n; i++)
41+
for(int j = 0; j <= n; j++)
42+
M[i][j] = inf;
43+
for(int i = 1; i <= n; i++) M[0][i] = bossHealth[i-1];
44+
for(int i = 1; i <= n; i++)
45+
for(int j = 1; j <= n; j++){
46+
int k = damageChart[i-1][j-1] - '0';
47+
if(k) M[i][j] = (bossHealth[j-1] + k - 1)/k;
48+
}
49+
priority_queue<node> Q;
50+
for(int i = 1; i <= n; i++){
51+
H[(1<<i)+1] = M[0][i];
52+
Q.push(node((1<<i)+1, M[0][i]));
53+
}
54+
while(!Q.empty()){
55+
node p = Q.top();
56+
Q.pop();
57+
int s = p.state, c = p.cost, m = inf;
58+
if(s == (1<<(n+1))-1) return c;
59+
for(int i = 0; i <= n; i++)
60+
for(int j = 1; j <= n; j++)
61+
if(s&(1<<i) && !(s&(1<<j)) && M[i][j] < inf){
62+
63+
m = M[i][j];
64+
int t = (s|(1<<j));
65+
if(H.count(t) == 0 || H[t] > c+m){
66+
H[t] = c+m;
67+
//cout<<"node <"<<i<<","<<j<<"> with value "<<m<<" pushed into queue at value "<<c<<endl;
68+
Q.push(node(t, c+m));
69+
}
70+
}
71+
}
72+
}
73+
74+
// BEGIN CUT HERE
75+
public:
76+
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(); }
77+
private:
78+
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(); }
79+
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; } }
80+
void test_case_0() { string Arr0[] = {"070","500","140"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {150,150,150}; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arg2 = 218; verify_case(0, Arg2, leastShots(Arg0, Arg1)); }
81+
void test_case_1() { string Arr0[] = {"1542", "7935", "1139", "8882"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {150,150,150,150}; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arg2 = 205; verify_case(1, Arg2, leastShots(Arg0, Arg1)); }
82+
void test_case_2() { string Arr0[] = {"07", "40"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {150,10}; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arg2 = 48; verify_case(2, Arg2, leastShots(Arg0, Arg1)); }
83+
void test_case_3() { string Arr0[] = {"198573618294842",
84+
"159819849819205",
85+
"698849290010992",
86+
"000000000000000",
87+
"139581938009384",
88+
"158919111891911",
89+
"182731827381787",
90+
"135788359198718",
91+
"187587819218927",
92+
"185783759199192",
93+
"857819038188122",
94+
"897387187472737",
95+
"159938981818247",
96+
"128974182773177",
97+
"135885818282838"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {157, 1984, 577, 3001, 2003, 2984, 5988, 190003,
98+
9000, 102930, 5938, 1000000, 1000000, 5892, 38}; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arg2 = 260445; verify_case(3, Arg2, leastShots(Arg0, Arg1)); }
99+
void test_case_4() { string Arr0[] = {"02111111", "10711111", "11071111", "11104111",
100+
"41110111", "11111031", "11111107", "11111210"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {28,28,28,28,28,28,28,28}; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arg2 = 92; verify_case(4, Arg2, leastShots(Arg0, Arg1)); }
101+
102+
// END CUT HERE
103+
104+
};
105+
106+
// BEGIN CUT HERE
107+
int main(){
108+
109+
KiloManX ___test;
110+
___test.run_test(-1);
111+
112+
}
113+
// END CUT HERE

KiloManX.txt

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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>The KiloMan series has always had a consistent pattern: you start off with one (rather weak) default weapon, and then defeat some number of bosses. When you defeat a boss, you then acquire his weapon, which can then be used against other bosses, and so on. Usually, each boss is weak against some weapon acquired from another boss, so there is a recommended order in which to tackle the bosses. You have been playing for a while and wonder exactly how efficient you can get at playing the game. Your metric for efficiency is the total number of weapon shots fired to defeat all of the bosses.</p>
2+
3+
<p>You have a chart in front of you detailing how much damage each weapon does to each boss per shot, and you know how much health each boss has. When a boss's health is reduced to 0 or less, he is defeated. You start off only with the Kilo Buster, which does 1 damage per shot to any boss. The chart is represented as a vector &lt;string&gt;, with the <i>i</i>th element containing N one-digit numbers ('0'-'9'), detailing the damage done to bosses 0, 1, 2, ..., N-1 by the weapon obtained from boss <i>i</i>, and the health is represented as a vector &lt;int&gt;, with the <i>i</i>th element representing the amount of health that boss <i>i</i> has.</p>
4+
5+
<p>Given a vector &lt;string&gt; <b>damageChart</b> representing all the weapon damages, and a vector &lt;int&gt; <b>bossHealth</b> showing how much health each boss has, your method should return an int which is the least number of shots that need to be fired to defeat all of the bosses.</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>KiloManX</td></tr><tr><td>Method:</td><td>leastShots</td></tr><tr><td>Parameters:</td><td>vector &lt;string&gt;, vector &lt;int&gt;</td></tr><tr><td>Returns:</td><td>int</td></tr><tr><td>Method signature:</td><td>int leastShots(vector &lt;string&gt; damageChart, vector &lt;int&gt; bossHealth)</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>Constraints</h3></td></tr><tr><td align="center" valign="top">-</td><td><b>damageChart</b> will contain between 1 and 15 elements, inclusive.</td></tr><tr><td align="center" valign="top">-</td><td>each element of <b>damageChart</b> will be of the same length, which will be the same as the number of elements in <b>damageChart</b>.</td></tr><tr><td align="center" valign="top">-</td><td>each element of <b>damageChart</b> will contain only the characters '0'-'9'.</td></tr><tr><td align="center" valign="top">-</td><td><b>bossHealth</b> will contain between 1 and 15 elements, inclusive.</td></tr><tr><td align="center" valign="top">-</td><td><b>damageChart</b> and <b>bossHealth</b> will contain the same number of elements.</td></tr><tr><td align="center" valign="top">-</td><td>each element in <b>bossHealth</b> will be between 1 and 1000000, 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>&#160;&#160;&#160;&#160;</td><td><table><tr><td><table><tr><td><pre>{&quot;070&quot;,&quot;500&quot;,&quot;140&quot;}</pre></td></tr><tr><td><pre>{150,150,150}</pre></td></tr></table></td></tr><tr><td><pre>Returns: 218</pre></td></tr><tr><td><table><tr><td colspan="2">The best way to go is to use the KiloBuster to defeat boss 2 (indexed from 0), then use the weapon from boss 2 to defeat boss 1, and then use the weapon from boss 1 to defeat boss 0. This leads to total damage of 150 + 38 + 30 = 218.</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>{&quot;1542&quot;, &quot;7935&quot;, &quot;1139&quot;, &quot;8882&quot;}</pre></td></tr><tr><td><pre>{150,150,150,150}</pre></td></tr></table></td></tr><tr><td><pre>Returns: 205</pre></td></tr><tr><td><table><tr><td colspan="2">Defeat boss 2, use his weapon to defeat boss 3, and then use boss 3's weapon to defeat both bosses 0 and 1, for a total shot count of 150 + 17 + 19 + 19 = 205. It would be more efficient to defeat boss 1 with his own weapon, but it would be cheating to get his weapon before defeating him.</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>{&quot;07&quot;, &quot;40&quot;}</pre></td></tr><tr><td><pre>{150,10}</pre></td></tr></table></td></tr><tr><td><pre>Returns: 48</pre></td></tr><tr><td></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>{&quot;198573618294842&quot;,
6+
&quot;159819849819205&quot;,
7+
&quot;698849290010992&quot;,
8+
&quot;000000000000000&quot;,
9+
&quot;139581938009384&quot;,
10+
&quot;158919111891911&quot;,
11+
&quot;182731827381787&quot;,
12+
&quot;135788359198718&quot;,
13+
&quot;187587819218927&quot;,
14+
&quot;185783759199192&quot;,
15+
&quot;857819038188122&quot;,
16+
&quot;897387187472737&quot;,
17+
&quot;159938981818247&quot;,
18+
&quot;128974182773177&quot;,
19+
&quot;135885818282838&quot;}</pre></td></tr><tr><td><pre>{157, 1984, 577, 3001, 2003, 2984, 5988, 190003,
20+
9000, 102930, 5938, 1000000, 1000000, 5892, 38}</pre></td></tr></table></td></tr><tr><td><pre>Returns: 260445</pre></td></tr><tr><td></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>{&quot;02111111&quot;, &quot;10711111&quot;, &quot;11071111&quot;, &quot;11104111&quot;,
21+
&quot;41110111&quot;, &quot;11111031&quot;, &quot;11111107&quot;, &quot;11111210&quot;}</pre></td></tr><tr><td><pre>{28,28,28,28,28,28,28,28}</pre></td></tr></table></td></tr><tr><td><pre>Returns: 92</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)