#include "mushrooms.h"
#include <bits/stdc++.h>
#define vi vector<int>
#define pb push_back
using namespace std;
const int maxn = 20000;
vi shroom[2] = {{0},{}};
int uses = 0;
int type[maxn] = {0};
void found(int ind, int val) {
type[ind] = val;
shroom[val].pb(ind);
}
void start(int sec) {
//test A_
vi test = {0,sec};
int res = use_machine(test);
++uses;
found(sec,res);
}
void start2(vi probe) {
//test A_A_
vi test;
int cur = (shroom[0].size() >= shroom[1].size()) ? 0 : 1;
int opp = !cur;
for (int i=0; i<2; ++i) {
test.pb(shroom[cur][i]);
test.pb(probe[i]);
}
int res = use_machine(test);
++uses;
int l = probe[0], r = probe[1];
if (res%2 == 0) {
found(r,cur);
} else {
found(r,opp);
--res;
}
if (res == 0) {
found(l,cur);
} else {
found(l,opp);
}
}
int quad(vi probe) {
//test A_A_A_A_
vi test;
int cur = (shroom[0].size() >= shroom[1].size()) ? 0 : 1;
int opp = !cur;
int k = probe.size();
for (int i=0; i<k; ++i) {
test.pb(shroom[cur][i]);
test.pb(probe[i]);
}
int res = use_machine(test);
++uses;
int last = probe[k-1];
if (res%2 == 0) {
found(last,cur);
} else {
found(last,opp);
--res;
}
if (res == 0) {
for (int i=0; i<k-1; ++i) {
found(probe[i],cur);
}
} else if (res == 6) {
for (int i=0; i<k-1; ++i) {
found(probe[i],opp);
}
}
return res;
}
void test1(vi probe) {
//test A_A_A_A_ while we have less than 2 B's
int cur = (shroom[0].size() >= shroom[1].size()) ? 0 : 1;
int opp = !cur;
int res = quad(probe);
int l = probe[0], m = probe[1], r = probe[2];
if (res == 2) {
vi subtest = {l,m};
start2(subtest);
if (type[l] == cur && type[m] == cur) {
found(r, opp);
} else {
found(r,cur);
}
} else if (res == 4) {
vi subtest = {l,m};
start2(subtest);
if (type[l] == opp && type[m] == opp) {
found(r,cur);
} else {
found(r,opp);
}
}
}
void subtest2(vi probe, int cur) {
//test A_A__B if we know probe is a permutation of (ABB)
//test B_B__A if (AAB)
//future george here: YOU FORGOT THE FREE SHROOM
vi test;
int opp = !cur;
int l = probe[0], m = probe[1], r = probe[2], rr = probe[3];
test = {shroom[cur][0], l, shroom[cur][1], m, r, shroom[opp][0], rr};
int res = use_machine(test);
++uses;
if (res%2 == 0) {
found(rr,cur);
--res;
} else {
found(rr,opp);
}
if (res == 1) {
found(l,cur);
found(m,opp);
found(r,opp);
} else if (res == 3) {
found(l,opp);
found(m,cur);
found(r,opp);
} else if (res == 5) {
found(l,opp);
found(m,opp);
found(r,cur);
}
return;
}
bool test2(vi probe) {
//test A_A_A_A_ when we already have 2 B's
//future george here: YOU FORGOT THE FREE SHROOM
vi test = probe;
test.pop_back();
int cur = (shroom[0].size() >= shroom[1].size()) ? 0 : 1;
int opp = !cur;
int res = quad(test);
//forgot case res 0, res 6 - free shroom is lost
//fixed with bool - true if we are instantly done
test.pop_back();
test.pb(probe.back());
if (res == 2) {
subtest2(test, opp);
return false;
} else if (res == 4) {
subtest2(test, cur);
return false;
}
return true;
}
int test3(vi probe) {
//test A_A_A_......A_A_
vi test;
int cur = (shroom[0].size() >= shroom[1].size()) ? 0 : 1;
int opp = !cur;
int k = probe.size();
for (int i=0; i<k; ++i) {
test.pb(shroom[cur][i]);
test.pb(probe[i]);
}
int res = use_machine(test);
++uses;
int last = probe[k-1];
if (res%2 == 0) {
found(last,cur);
} else {
found(last,opp);
--res;
}
int numcur = res/2;
if (cur == 0) {
return k-1-numcur;
}
return numcur;
}
int count_mushrooms(int n) {
int count_a = 0;
int magic = 100;
for (int i=0; i<n; ++i) {
type[i] = -1;
}
if (n <= 221) {
for (int i = 1; i<n; ++i) {
start(i);
}
return shroom[0].size();
}
int ind = 1;
while (shroom[0].size() < 2 && shroom[1].size()<2) {
start(ind);
++ind;
}
while (shroom[0].size() < 4 && shroom[1].size()<4) {
vi probe = {ind, ind+1};
start2(probe);
ind +=2;
}
int cur = (shroom[0].size() >= shroom[1].size()) ? 0 : 1;
int opp = !cur;
while (shroom[opp].size() < 2 && shroom[cur].size() < magic) {
vi probe;
for (int i=0; i<4; ++i) {
probe.pb(ind);
++ind;
}
test1(probe);
}
cur = (shroom[0].size() >= shroom[1].size()) ? 0 : 1;
opp = !cur;
while (shroom[cur].size() < magic) {
vi probe;
for (int i=0; i<5; ++i) {
probe.pb(ind);
++ind;
}
bool quick = test2(probe);
if (quick) {
--ind; //if the first 3 are the same, we don't need an extra check just for the last free shroom, just do it in the next batch
}
cur = (shroom[0].size() >= shroom[1].size()) ? 0 : 1;
opp = !cur;
// cout << "excluding ind " << ind << " we found " << shroom[0].size() << "\n";
}
int s = shroom[0].size();
while (ind != n) {
cur = (shroom[0].size() >= shroom[1].size()) ? 0 : 1;
opp = !cur;
int s = shroom[cur].size();
int cap = min(s, n-ind);
vi probe;
for (int i=0; i<cap; ++i) {
probe.pb(ind);
++ind;
}
count_a += test3(probe);
}
return count_a + shroom[0].size();
}
Compilation message
mushrooms.cpp: In function 'int count_mushrooms(int)':
mushrooms.cpp:212:57: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
212 | while (shroom[opp].size() < 2 && shroom[cur].size() < magic) {
| ~~~~~~~~~~~~~~~~~~~^~~~~~~
mushrooms.cpp:222:31: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
222 | while (shroom[cur].size() < magic) {
| ~~~~~~~~~~~~~~~~~~~^~~~~~~
mushrooms.cpp:236:9: warning: unused variable 's' [-Wunused-variable]
236 | int s = shroom[0].size();
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
4 ms |
508 KB |
Output is correct |
6 |
Correct |
3 ms |
592 KB |
Output is correct |
7 |
Correct |
7 ms |
336 KB |
Output is correct |
8 |
Correct |
5 ms |
336 KB |
Output is correct |
9 |
Correct |
8 ms |
548 KB |
Output is correct |
10 |
Correct |
7 ms |
336 KB |
Output is correct |
11 |
Correct |
8 ms |
552 KB |
Output is correct |
12 |
Correct |
6 ms |
536 KB |
Output is correct |
13 |
Correct |
5 ms |
336 KB |
Output is correct |
14 |
Correct |
6 ms |
336 KB |
Output is correct |
15 |
Correct |
7 ms |
592 KB |
Output is correct |
16 |
Correct |
8 ms |
336 KB |
Output is correct |
17 |
Correct |
5 ms |
504 KB |
Output is correct |
18 |
Correct |
9 ms |
592 KB |
Output is correct |
19 |
Correct |
7 ms |
552 KB |
Output is correct |
20 |
Correct |
10 ms |
592 KB |
Output is correct |
21 |
Correct |
9 ms |
336 KB |
Output is correct |
22 |
Correct |
7 ms |
336 KB |
Output is correct |
23 |
Correct |
6 ms |
336 KB |
Output is correct |
24 |
Correct |
5 ms |
592 KB |
Output is correct |
25 |
Correct |
9 ms |
336 KB |
Output is correct |
26 |
Correct |
7 ms |
336 KB |
Output is correct |
27 |
Correct |
6 ms |
336 KB |
Output is correct |
28 |
Correct |
6 ms |
336 KB |
Output is correct |
29 |
Correct |
7 ms |
592 KB |
Output is correct |
30 |
Correct |
8 ms |
848 KB |
Output is correct |
31 |
Correct |
7 ms |
336 KB |
Output is correct |
32 |
Correct |
7 ms |
336 KB |
Output is correct |
33 |
Correct |
10 ms |
336 KB |
Output is correct |
34 |
Correct |
6 ms |
556 KB |
Output is correct |
35 |
Correct |
8 ms |
548 KB |
Output is correct |
36 |
Correct |
7 ms |
336 KB |
Output is correct |
37 |
Correct |
7 ms |
552 KB |
Output is correct |
38 |
Correct |
8 ms |
592 KB |
Output is correct |
39 |
Correct |
7 ms |
808 KB |
Output is correct |
40 |
Correct |
7 ms |
336 KB |
Output is correct |
41 |
Correct |
9 ms |
336 KB |
Output is correct |
42 |
Correct |
7 ms |
336 KB |
Output is correct |
43 |
Correct |
7 ms |
336 KB |
Output is correct |
44 |
Correct |
8 ms |
592 KB |
Output is correct |
45 |
Correct |
6 ms |
336 KB |
Output is correct |
46 |
Correct |
9 ms |
336 KB |
Output is correct |
47 |
Correct |
6 ms |
336 KB |
Output is correct |
48 |
Correct |
9 ms |
804 KB |
Output is correct |
49 |
Correct |
10 ms |
336 KB |
Output is correct |
50 |
Correct |
10 ms |
336 KB |
Output is correct |
51 |
Correct |
7 ms |
336 KB |
Output is correct |
52 |
Correct |
7 ms |
548 KB |
Output is correct |
53 |
Correct |
7 ms |
336 KB |
Output is correct |
54 |
Correct |
7 ms |
336 KB |
Output is correct |
55 |
Correct |
7 ms |
336 KB |
Output is correct |
56 |
Correct |
7 ms |
548 KB |
Output is correct |
57 |
Correct |
6 ms |
552 KB |
Output is correct |
58 |
Correct |
9 ms |
336 KB |
Output is correct |
59 |
Correct |
8 ms |
604 KB |
Output is correct |
60 |
Correct |
7 ms |
592 KB |
Output is correct |
61 |
Correct |
6 ms |
524 KB |
Output is correct |
62 |
Correct |
1 ms |
336 KB |
Output is correct |
63 |
Correct |
1 ms |
336 KB |
Output is correct |
64 |
Correct |
1 ms |
508 KB |
Output is correct |
65 |
Correct |
1 ms |
504 KB |
Output is correct |
66 |
Correct |
1 ms |
336 KB |
Output is correct |
67 |
Correct |
1 ms |
336 KB |
Output is correct |
68 |
Correct |
1 ms |
336 KB |
Output is correct |
69 |
Correct |
1 ms |
504 KB |
Output is correct |
70 |
Correct |
1 ms |
336 KB |
Output is correct |
71 |
Correct |
1 ms |
336 KB |
Output is correct |
72 |
Correct |
1 ms |
336 KB |
Output is correct |
73 |
Correct |
1 ms |
336 KB |
Output is correct |
74 |
Correct |
1 ms |
336 KB |
Output is correct |
75 |
Correct |
1 ms |
336 KB |
Output is correct |
76 |
Correct |
1 ms |
336 KB |
Output is correct |
77 |
Correct |
1 ms |
336 KB |
Output is correct |
78 |
Correct |
1 ms |
336 KB |
Output is correct |
79 |
Correct |
1 ms |
336 KB |
Output is correct |
80 |
Correct |
1 ms |
336 KB |
Output is correct |
81 |
Correct |
1 ms |
336 KB |
Output is correct |
82 |
Correct |
1 ms |
336 KB |
Output is correct |
83 |
Correct |
1 ms |
336 KB |
Output is correct |
84 |
Correct |
1 ms |
336 KB |
Output is correct |
85 |
Correct |
1 ms |
336 KB |
Output is correct |
86 |
Correct |
1 ms |
336 KB |
Output is correct |
87 |
Correct |
1 ms |
336 KB |
Output is correct |
88 |
Correct |
1 ms |
336 KB |
Output is correct |
89 |
Correct |
1 ms |
336 KB |
Output is correct |
90 |
Correct |
1 ms |
336 KB |
Output is correct |
91 |
Correct |
1 ms |
336 KB |
Output is correct |