#include "koala.h"
#include <bits/stdc++.h>
using namespace std;
string to_string(string s) { return s; }
template <typename T> string to_string(T v) {
bool first = true;
string res = "[";
for (const auto &x : v) {
if (!first)
res += ", ";
first = false;
res += to_string(x);
}
res += "]";
return res;
}
void dbg_out() { cout << endl; }
template <typename Head, typename... Tail> void dbg_out(Head H, Tail... T) {
cout << ' ' << to_string(H);
dbg_out(T...);
}
#ifdef DEBUG
#define dbg(...) cout << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#else
#define dbg(...)
#endif
vector<bool> simulate(int N, int W, vector<int> B, vector<int> P) {
int i, j;
int S = 0;
int cache[2][205];
int num[2][205];
char taken[105][205];
for (i = 0; i < 205; ++i) {
cache[1][i] = 0;
num[1][i] = 0;
}
for (i = 0; i < N; ++i) {
int v = B[i] + 1;
int ii = i & 1;
int o = ii ^ 1;
for (j = 0; j <= W; ++j) {
cache[ii][j] = cache[o][j];
num[ii][j] = num[o][j];
taken[i][j] = 0;
}
for (j = W; j >= v; --j) {
int h = cache[o][j - v] + P[i];
int hn = num[o][j - v] + 1;
if (h > cache[ii][j] || (h == cache[ii][j] && hn > num[ii][j])) {
cache[ii][j] = h;
num[ii][j] = hn;
taken[i][j] = 1;
} else {
taken[i][j] = 0;
}
}
}
int cur = W;
vector<bool> take(N);
for (i = N - 1; i >= 0; --i) {
take[i] = taken[i][cur];
int R = taken[i][cur] ? (B[i] + 1) : 0;
cur -= R;
}
return take;
}
int minValue(int N, int W) {
vector<int> b(N);
b[0] = 1;
vector<int> r(N);
playRound(b.data(), r.data());
for (int i = 0; i < N; ++i)
if (b[i] >= r[i])
return i;
assert(false);
}
int maxValue(int N, int W) {
vector<int> candidats(N);
iota(candidats.begin(), candidats.end(), 0);
while (candidats.size() > 1) {
int nbCandidats = candidats.size();
vector<int> b(N);
for (int u : candidats)
b[u] = W / nbCandidats;
vector<int> r(N);
playRound(b.data(), r.data());
;
vector<int> nxt;
for (int u : candidats)
if (b[u] < r[u])
nxt.push_back(u);
candidats = nxt;
}
assert(!candidats.empty());
return candidats[0];
return 0;
}
int greaterValue(int N, int W) {
int lo = 1, up = 9;
while (true) {
int x = (lo + up) / 2;
vector<int> b(N), r(N);
b[0] = b[1] = x;
playRound(b.data(), r.data());
int take0 = b[0] < r[0];
int take1 = b[1] < r[1];
dbg(x, take0, take1);
if (take0 and take1)
lo = x + 1;
else if (!take0 and !take1)
up = x - 1;
else if (take0)
return 0;
else
return 1;
}
}
void allValues(int N, int W, int *P) {
if (W == 2 * N) {
auto compare = [&](int i, int j) {
vector<int> b(N);
b[i] = b[j] = N;
vector<int> r(N);
playRound(b.data(), r.data());
assert((b[i] < r[i]) xor (b[j] < r[j]));
return b[i] >= r[i];
};
vector<int> order(N);
iota(order.begin(), order.end(), 0);
auto merge_sort = [&](auto rec, vector<int> toSort) {
if (toSort.size() == 1)
return toSort;
vector<int> l, r;
for (int i = 0; i < (int)toSort.size(); ++i)
(i % 2 ? r : l).push_back(toSort[i]);
l = rec(rec, l);
r = rec(rec, r);
merge(l.begin(), l.end(), r.begin(), r.end(), toSort.begin(), compare);
return toSort;
};
order = merge_sort(merge_sort, order);
for (int i = 0; i < N; ++i)
P[order[i]] = i + 1;
} else {
auto Rec = [&](auto rec, int l, int r, vector<int> indices) {
assert((int)indices.size() == r - l + 1);
if (l == r) {
P[indices[0]] = l;
return;
}
vector<int> p(N);
iota(p.begin(), p.end(), 1);
for (int x = 1; x <= W / (r - l + 1); ++x) {
vector<int> B(N);
for (int i = l; i <= r; ++i)
B[i - 1] = x;
vector<bool> take = simulate(N, W, B, p);
bool allTake = true, allNot = true;
for (int i = l; i <= r; ++i)
allTake &= take[i - 1], allNot &= !take[i - 1];
if (!allTake and !allNot) {
B.assign(N, 0);
for (int i : indices)
B[i] = x;
vector<int> R(N);
playRound(B.data(), R.data());
vector<int> small, big;
for (int i : indices) {
if (R[i] > B[i])
big.push_back(i);
else
small.push_back(i);
}
rec(rec, l, l + small.size() - 1, small);
rec(rec, l + small.size(), r, big);
return;
}
}
};
vector<int> indices(N);
iota(indices.begin(), indices.end(), 0);
Rec(Rec, 1, N, indices);
}
}
Compilation message
koala.cpp: In function 'std::vector<bool> simulate(int, int, std::vector<int>, std::vector<int>)':
koala.cpp:34:7: warning: unused variable 'S' [-Wunused-variable]
34 | int S = 0;
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
208 KB |
Output is correct |
2 |
Correct |
3 ms |
208 KB |
Output is correct |
3 |
Correct |
3 ms |
312 KB |
Output is correct |
4 |
Correct |
3 ms |
208 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
208 KB |
Output is correct |
2 |
Correct |
10 ms |
208 KB |
Output is correct |
3 |
Correct |
10 ms |
208 KB |
Output is correct |
4 |
Correct |
10 ms |
208 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
42 ms |
336 KB |
Output is correct |
2 |
Correct |
48 ms |
324 KB |
Output is correct |
3 |
Correct |
40 ms |
324 KB |
Output is correct |
4 |
Correct |
40 ms |
324 KB |
Output is correct |
5 |
Correct |
40 ms |
344 KB |
Output is correct |
6 |
Correct |
43 ms |
324 KB |
Output is correct |
7 |
Correct |
40 ms |
324 KB |
Output is correct |
8 |
Correct |
48 ms |
344 KB |
Output is correct |
9 |
Correct |
46 ms |
324 KB |
Output is correct |
10 |
Correct |
44 ms |
336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
28 ms |
208 KB |
Output is correct |
2 |
Correct |
25 ms |
208 KB |
Output is correct |
3 |
Correct |
25 ms |
316 KB |
Output is correct |
4 |
Correct |
24 ms |
316 KB |
Output is correct |
5 |
Correct |
30 ms |
208 KB |
Output is correct |
6 |
Correct |
25 ms |
320 KB |
Output is correct |
7 |
Correct |
24 ms |
208 KB |
Output is correct |
8 |
Correct |
24 ms |
316 KB |
Output is correct |
9 |
Correct |
28 ms |
208 KB |
Output is correct |
10 |
Correct |
28 ms |
208 KB |
Output is correct |
11 |
Correct |
24 ms |
208 KB |
Output is correct |
12 |
Correct |
28 ms |
208 KB |
Output is correct |
13 |
Correct |
25 ms |
208 KB |
Output is correct |
14 |
Correct |
26 ms |
320 KB |
Output is correct |
15 |
Correct |
27 ms |
208 KB |
Output is correct |
16 |
Correct |
33 ms |
208 KB |
Output is correct |
17 |
Correct |
25 ms |
300 KB |
Output is correct |
18 |
Correct |
25 ms |
208 KB |
Output is correct |
19 |
Correct |
25 ms |
208 KB |
Output is correct |
20 |
Correct |
24 ms |
208 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
336 KB |
Output is correct |
2 |
Correct |
11 ms |
336 KB |
Output is correct |
3 |
Correct |
11 ms |
324 KB |
Output is correct |
4 |
Correct |
13 ms |
340 KB |
Output is correct |
5 |
Correct |
12 ms |
332 KB |
Output is correct |
6 |
Correct |
11 ms |
348 KB |
Output is correct |
7 |
Correct |
12 ms |
336 KB |
Output is correct |
8 |
Correct |
14 ms |
328 KB |
Output is correct |
9 |
Correct |
13 ms |
344 KB |
Output is correct |
10 |
Correct |
11 ms |
336 KB |
Output is correct |
11 |
Correct |
11 ms |
332 KB |
Output is correct |
12 |
Correct |
11 ms |
336 KB |
Output is correct |
13 |
Correct |
14 ms |
340 KB |
Output is correct |
14 |
Correct |
11 ms |
340 KB |
Output is correct |
15 |
Correct |
11 ms |
344 KB |
Output is correct |
16 |
Correct |
11 ms |
344 KB |
Output is correct |
17 |
Correct |
11 ms |
336 KB |
Output is correct |
18 |
Correct |
11 ms |
336 KB |
Output is correct |
19 |
Correct |
11 ms |
328 KB |
Output is correct |
20 |
Correct |
11 ms |
336 KB |
Output is correct |
21 |
Correct |
14 ms |
340 KB |
Output is correct |
22 |
Correct |
11 ms |
336 KB |
Output is correct |
23 |
Correct |
11 ms |
340 KB |
Output is correct |
24 |
Correct |
13 ms |
344 KB |
Output is correct |
25 |
Correct |
11 ms |
336 KB |
Output is correct |
26 |
Correct |
14 ms |
336 KB |
Output is correct |
27 |
Correct |
11 ms |
340 KB |
Output is correct |
28 |
Correct |
14 ms |
568 KB |
Output is correct |
29 |
Correct |
11 ms |
336 KB |
Output is correct |
30 |
Correct |
11 ms |
328 KB |
Output is correct |
31 |
Correct |
11 ms |
336 KB |
Output is correct |
32 |
Correct |
11 ms |
340 KB |
Output is correct |
33 |
Correct |
11 ms |
336 KB |
Output is correct |
34 |
Correct |
11 ms |
328 KB |
Output is correct |
35 |
Correct |
11 ms |
332 KB |
Output is correct |
36 |
Correct |
11 ms |
336 KB |
Output is correct |
37 |
Correct |
11 ms |
336 KB |
Output is correct |
38 |
Correct |
11 ms |
336 KB |
Output is correct |
39 |
Correct |
15 ms |
336 KB |
Output is correct |
40 |
Correct |
11 ms |
336 KB |
Output is correct |