Skip to content

Commit 14af882

Browse files
committed
add kilomanx
1 parent bac03c1 commit 14af882

File tree

2 files changed

+273
-50
lines changed

2 files changed

+273
-50
lines changed

KiloManX/KiloManX.cpp

Lines changed: 214 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,214 @@
1+
#include <vector>
2+
#include <list>
3+
#include <map>
4+
#include <set>
5+
#include <queue>
6+
#include <deque>
7+
#include <stack>
8+
#include <bitset>
9+
#include <algorithm>
10+
#include <functional>
11+
#include <numeric>
12+
#include <utility>
13+
#include <sstream>
14+
#include <iostream>
15+
#include <iomanip>
16+
#include <cstdio>
17+
#include <cmath>
18+
#include <cstdlib>
19+
#include <ctime>
20+
21+
using namespace std;
22+
23+
int dist[32768];
24+
25+
struct Order {
26+
bool operator() (const int x, const int y) const {
27+
return make_pair(dist[x], x) < make_pair(dist[y], y);
28+
}
29+
};
30+
31+
class KiloManX {
32+
public:
33+
int leastShots(vector <string> damageChart, vector <int> bossHealth) {
34+
for (int i = 0; i < 32768; i++)
35+
dist[i] = INT_MAX;
36+
37+
int N = damageChart.size();
38+
set<int, Order> qs;
39+
qs.insert(0);
40+
dist[0] = 0;
41+
42+
int res = INT_MAX;
43+
while (!qs.empty()) {
44+
int w = *qs.begin();
45+
int s = dist[w];
46+
qs.erase(qs.begin());
47+
48+
//bitset<15> bs(w);
49+
//cout << "where: " << bs << " shots: " << s << endl;
50+
if (w == (1 << N)-1) {
51+
if (s < res) res = s;
52+
continue;
53+
}
54+
55+
for (int i = 0; i < N; i++) {
56+
if (w >> i & 1) continue;
57+
int best = bossHealth[i];
58+
int bj = -1;
59+
for (int j = 0; j < N; j++) {
60+
if (i == j) continue;
61+
if ((w >> j & 1) && (damageChart[j][i] != '0')) {
62+
int a = damageChart[j][i]-'0';
63+
int ds = bossHealth[i]/a;
64+
if (bossHealth[i]%a != 0) ds += 1;
65+
if (ds < best) {
66+
best = ds;
67+
bj = j;
68+
}
69+
}
70+
}
71+
72+
int ns = s + best;
73+
int nw = (w | 1 << i);
74+
if (ns < dist[nw]) {
75+
if (qs.count(nw)) qs.erase(nw);
76+
dist[nw] = ns;
77+
qs.insert(nw);
78+
//cout << "kill boss: " << i << " best: " << best << " bj: " << bj << endl;
79+
}
80+
}
81+
}
82+
83+
return res;
84+
}
85+
};
86+
87+
88+
// BEGIN KAWIGIEDIT TESTING
89+
// Generated by KawigiEdit 2.1.8 (beta) modified by pivanof
90+
#include <iostream>
91+
#include <string>
92+
#include <vector>
93+
using namespace std;
94+
bool KawigiEdit_RunTest(int testNum, vector <string> p0, vector <int> p1, bool hasAnswer, int p2) {
95+
cout << "Test " << testNum << ": [" << "{";
96+
for (int i = 0; int(p0.size()) > i; ++i) {
97+
if (i > 0) {
98+
cout << ",";
99+
}
100+
cout << "\"" << p0[i] << "\"";
101+
}
102+
cout << "}" << "," << "{";
103+
for (int i = 0; int(p1.size()) > i; ++i) {
104+
if (i > 0) {
105+
cout << ",";
106+
}
107+
cout << p1[i];
108+
}
109+
cout << "}";
110+
cout << "]" << endl;
111+
KiloManX *obj;
112+
int answer;
113+
obj = new KiloManX();
114+
clock_t startTime = clock();
115+
answer = obj->leastShots(p0, p1);
116+
clock_t endTime = clock();
117+
delete obj;
118+
bool res;
119+
res = true;
120+
cout << "Time: " << double(endTime - startTime) / CLOCKS_PER_SEC << " seconds" << endl;
121+
if (hasAnswer) {
122+
cout << "Desired answer:" << endl;
123+
cout << "\t" << p2 << endl;
124+
}
125+
cout << "Your answer:" << endl;
126+
cout << "\t" << answer << endl;
127+
if (hasAnswer) {
128+
res = answer == p2;
129+
}
130+
if (!res) {
131+
cout << "DOESN'T MATCH!!!!" << endl;
132+
} else if (double(endTime - startTime) / CLOCKS_PER_SEC >= 20) {
133+
cout << "FAIL the timeout" << endl;
134+
res = false;
135+
} else if (hasAnswer) {
136+
cout << "Match :-)" << endl;
137+
} else {
138+
cout << "OK, but is it right?" << endl;
139+
}
140+
cout << "" << endl;
141+
return res;
142+
}
143+
int main() {
144+
bool all_right;
145+
all_right = true;
146+
147+
vector <string> p0;
148+
vector <int> p1;
149+
int p2;
150+
151+
{
152+
// ----- test 0 -----
153+
string t0[] = {"070","500","140"};
154+
p0.assign(t0, t0 + sizeof(t0) / sizeof(t0[0]));
155+
int t1[] = {150,150,150};
156+
p1.assign(t1, t1 + sizeof(t1) / sizeof(t1[0]));
157+
p2 = 218;
158+
all_right = KawigiEdit_RunTest(0, p0, p1, true, p2) && all_right;
159+
// ------------------
160+
}
161+
162+
{
163+
// ----- test 1 -----
164+
string t0[] = {"1542","7935","1139","8882"};
165+
p0.assign(t0, t0 + sizeof(t0) / sizeof(t0[0]));
166+
int t1[] = {150,150,150,150};
167+
p1.assign(t1, t1 + sizeof(t1) / sizeof(t1[0]));
168+
p2 = 205;
169+
all_right = KawigiEdit_RunTest(1, p0, p1, true, p2) && all_right;
170+
// ------------------
171+
}
172+
173+
{
174+
// ----- test 2 -----
175+
string t0[] = {"07","40"};
176+
p0.assign(t0, t0 + sizeof(t0) / sizeof(t0[0]));
177+
int t1[] = {150,10};
178+
p1.assign(t1, t1 + sizeof(t1) / sizeof(t1[0]));
179+
p2 = 48;
180+
all_right = KawigiEdit_RunTest(2, p0, p1, true, p2) && all_right;
181+
// ------------------
182+
}
183+
184+
{
185+
// ----- test 3 -----
186+
string t0[] = {"198573618294842","159819849819205","698849290010992","000000000000000","139581938009384","158919111891911","182731827381787","135788359198718","187587819218927","185783759199192","857819038188122","897387187472737","159938981818247","128974182773177","135885818282838"};
187+
p0.assign(t0, t0 + sizeof(t0) / sizeof(t0[0]));
188+
int t1[] = {157,1984,577,3001,2003,2984,5988,190003,9000,102930,5938,1000000,1000000,5892,38};
189+
p1.assign(t1, t1 + sizeof(t1) / sizeof(t1[0]));
190+
p2 = 260445;
191+
all_right = KawigiEdit_RunTest(3, p0, p1, true, p2) && all_right;
192+
// ------------------
193+
}
194+
195+
{
196+
// ----- test 4 -----
197+
string t0[] = {"02111111","10711111","11071111","11104111","41110111","11111031","11111107","11111210"};
198+
p0.assign(t0, t0 + sizeof(t0) / sizeof(t0[0]));
199+
int t1[] = {28,28,28,28,28,28,28,28};
200+
p1.assign(t1, t1 + sizeof(t1) / sizeof(t1[0]));
201+
p2 = 92;
202+
all_right = KawigiEdit_RunTest(4, p0, p1, true, p2) && all_right;
203+
// ------------------
204+
}
205+
206+
if (all_right) {
207+
cout << "You're a stud (at least on the example cases)!" << endl;
208+
} else {
209+
cout << "Some of the test cases had errors." << endl;
210+
}
211+
return 0;
212+
}
213+
// END KAWIGIEDIT TESTING
214+
//Powered by KawigiEdit 2.1.8 (beta) modified by pivanof!

TurntableService/TurntableService.cpp

Lines changed: 59 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,19 @@
2020
#include <climits>
2121
using namespace std;
2222

23+
int fs[16];
24+
int dist[32768][16];
25+
26+
struct Order {
27+
bool operator() (const int x, const int y) const {
28+
return make_pair(dist[x/16][x%16], x) < make_pair(dist[y/16][y%16], y);
29+
}
30+
};
31+
2332
class TurntableService {
2433
public:
25-
int fs[16];
26-
int dp[32768][16];
27-
2834
int calculateTime(vector <string> favorites) {
29-
int N = favorites.size();
35+
int N = favorites.size();
3036
for (int i = 0; i < N; i++) {
3137
int f = 0;
3238
istringstream is(favorites[i]);
@@ -38,29 +44,30 @@ class TurntableService {
3844

3945
for (int i = 0; i < 32768; i++)
4046
for (int j = 0; j < 16; j++)
41-
dp[i][j] = INT_MAX;
42-
dp[0][0] = 0;
47+
dist[i][j] = INT_MAX;
48+
dist[0][0] = 0;
4349

4450
int res = INT_MAX;
45-
46-
queue<int> qs;
47-
qs.push(0);
51+
52+
set<int, Order> qs;
53+
qs.insert(0);
4854
while (!qs.empty()) {
49-
int d = qs.front()/16;
50-
int p = qs.front()%16;
51-
int t = dp[d][p];
52-
qs.pop();
53-
55+
int w = *qs.begin();
56+
qs.erase(qs.begin());
57+
int d = w/16;
58+
int p = w%16;
59+
int t = dist[d][p];
60+
5461
//cout << "where " << p << endl;
5562
//cout << "time " << t << endl;
5663
//bitset<15> bs(d);
5764
//cout << "done " << bs << endl;
58-
65+
5966
if (d == (1<<N)-1) {
6067
if (t < res) res = t;
6168
continue;
6269
}
63-
70+
6471
for (int r = 0; r < N; r++) {
6572
int np = (p+r)%N;
6673
//cout << "next pos " << np << endl;
@@ -78,53 +85,55 @@ class TurntableService {
7885
//cout << "next done " << bs << endl;
7986
if (nd == d) continue;
8087
nt += 15;
81-
if (nt >= dp[nd][np]) continue;
82-
dp[nd][np] = nt;
83-
qs.push(nd*16+np);
88+
if (nt < dist[nd][np]) {
89+
int nw = nd*16+np;
90+
if (qs.count(nw)) qs.erase(nw);
91+
dist[nd][np] = nt;
92+
qs.insert(nw);
93+
}
8494
}
8595
}
8696

8797
return res;
88-
}
98+
}
8999
};
90100

91-
92101
// BEGIN KAWIGIEDIT TESTING
93102
// Generated by KawigiEdit 2.1.8 (beta) modified by pivanof
94103
#include <iostream>
95104
#include <string>
96105
#include <vector>
97106
using namespace std;
98107
bool KawigiEdit_RunTest(int testNum, vector <string> p0, bool hasAnswer, int p1) {
99-
cout << "Test " << testNum << ": [" << "{";
100-
for (int i = 0; int(p0.size()) > i; ++i) {
101-
if (i > 0) {
102-
cout << ",";
103-
}
104-
cout << "\"" << p0[i] << "\"";
105-
}
106-
cout << "}";
107-
cout << "]" << endl;
108-
TurntableService *obj;
109-
int answer;
110-
obj = new TurntableService();
111-
clock_t startTime = clock();
112-
answer = obj->calculateTime(p0);
113-
clock_t endTime = clock();
114-
delete obj;
115-
bool res;
116-
res = true;
117-
cout << "Time: " << double(endTime - startTime) / CLOCKS_PER_SEC << " seconds" << endl;
118-
if (hasAnswer) {
119-
cout << "Desired answer:" << endl;
120-
cout << "\t" << p1 << endl;
121-
}
122-
cout << "Your answer:" << endl;
123-
cout << "\t" << answer << endl;
124-
if (hasAnswer) {
125-
res = answer == p1;
126-
}
127-
if (!res) {
108+
cout << "Test " << testNum << ": [" << "{";
109+
for (int i = 0; int(p0.size()) > i; ++i) {
110+
if (i > 0) {
111+
cout << ",";
112+
}
113+
cout << "\"" << p0[i] << "\"";
114+
}
115+
cout << "}";
116+
cout << "]" << endl;
117+
TurntableService *obj;
118+
int answer;
119+
obj = new TurntableService();
120+
clock_t startTime = clock();
121+
answer = obj->calculateTime(p0);
122+
clock_t endTime = clock();
123+
delete obj;
124+
bool res;
125+
res = true;
126+
cout << "Time: " << double(endTime - startTime) / CLOCKS_PER_SEC << " seconds" << endl;
127+
if (hasAnswer) {
128+
cout << "Desired answer:" << endl;
129+
cout << "\t" << p1 << endl;
130+
}
131+
cout << "Your answer:" << endl;
132+
cout << "\t" << answer << endl;
133+
if (hasAnswer) {
134+
res = answer == p1;
135+
}
136+
if (!res) {
128137
cout << "DOESN'T MATCH!!!!" << endl;
129138
} else if (double(endTime - startTime) / CLOCKS_PER_SEC >= 2) {
130139
cout << "FAIL the timeout" << endl;

0 commit comments

Comments
 (0)