#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;
int t = type[ind];
shroom[t].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);
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) {
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);
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 k = probe.size();
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)
vi test;
int opp = !cur;
int l = probe[0], m = probe[1], r = probe[2];
test = {shroom[cur][0], l, shroom[cur][1], m, r, shroom[opp][0]};
int res = use_machine(test);
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;
}
void test2(vi probe) {
//test A_A_A_A_ when we already have 2 B's
vi test;
int cur = (shroom[0].size() >= shroom[1].size()) ? 0 : 1;
int opp = !cur;
int k = probe.size();
int res = quad(probe);
probe.pop_back();
if (res == 2) {
subtest2(probe, opp);
} else if (res == 4) {
subtest2(probe, cur);
}
}
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);
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<4; ++i) {
probe.pb(ind);
++ind;
}
test2(probe);
cur = (shroom[0].size() >= shroom[1].size()) ? 0 : 1;
opp = !cur;
}
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 'void test1(std::vector<int>)':
mushrooms.cpp:85:9: warning: unused variable 'k' [-Wunused-variable]
85 | int k = probe.size();
| ^
mushrooms.cpp: In function 'void test2(std::vector<int>)':
mushrooms.cpp:136:9: warning: unused variable 'k' [-Wunused-variable]
136 | int k = probe.size();
| ^
mushrooms.cpp: In function 'int count_mushrooms(int)':
mushrooms.cpp:195:57: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
195 | while (shroom[opp].size() < 2 && shroom[cur].size() < magic) {
| ~~~~~~~~~~~~~~~~~~~^~~~~~~
mushrooms.cpp:205:31: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
205 | while (shroom[cur].size() < magic) {
| ~~~~~~~~~~~~~~~~~~~^~~~~~~
# |
결과 |
실행 시간 |
메모리 |
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 |
3 ms |
336 KB |
Output is correct |
6 |
Correct |
2 ms |
336 KB |
Output is correct |
7 |
Correct |
5 ms |
336 KB |
Output is correct |
8 |
Correct |
7 ms |
336 KB |
Output is correct |
9 |
Correct |
5 ms |
336 KB |
Output is correct |
10 |
Correct |
6 ms |
336 KB |
Output is correct |
11 |
Partially correct |
8 ms |
592 KB |
Output is partially correct |
12 |
Correct |
7 ms |
592 KB |
Output is correct |
13 |
Correct |
5 ms |
336 KB |
Output is correct |
14 |
Correct |
6 ms |
608 KB |
Output is correct |
15 |
Partially correct |
7 ms |
336 KB |
Output is partially correct |
16 |
Partially correct |
8 ms |
592 KB |
Output is partially correct |
17 |
Correct |
4 ms |
336 KB |
Output is correct |
18 |
Correct |
8 ms |
336 KB |
Output is correct |
19 |
Correct |
6 ms |
336 KB |
Output is correct |
20 |
Correct |
6 ms |
536 KB |
Output is correct |
21 |
Correct |
6 ms |
504 KB |
Output is correct |
22 |
Partially correct |
6 ms |
336 KB |
Output is partially correct |
23 |
Correct |
8 ms |
336 KB |
Output is correct |
24 |
Correct |
7 ms |
760 KB |
Output is correct |
25 |
Partially correct |
9 ms |
792 KB |
Output is partially correct |
26 |
Correct |
7 ms |
336 KB |
Output is correct |
27 |
Correct |
7 ms |
592 KB |
Output is correct |
28 |
Correct |
6 ms |
336 KB |
Output is correct |
29 |
Partially correct |
7 ms |
336 KB |
Output is partially correct |
30 |
Correct |
7 ms |
556 KB |
Output is correct |
31 |
Partially correct |
9 ms |
544 KB |
Output is partially correct |
32 |
Partially correct |
7 ms |
592 KB |
Output is partially correct |
33 |
Partially correct |
7 ms |
336 KB |
Output is partially correct |
34 |
Partially correct |
6 ms |
336 KB |
Output is partially correct |
35 |
Correct |
9 ms |
516 KB |
Output is correct |
36 |
Partially correct |
8 ms |
336 KB |
Output is partially correct |
37 |
Partially correct |
6 ms |
724 KB |
Output is partially correct |
38 |
Partially correct |
7 ms |
592 KB |
Output is partially correct |
39 |
Partially correct |
9 ms |
524 KB |
Output is partially correct |
40 |
Partially correct |
6 ms |
336 KB |
Output is partially correct |
41 |
Partially correct |
6 ms |
336 KB |
Output is partially correct |
42 |
Partially correct |
7 ms |
552 KB |
Output is partially correct |
43 |
Partially correct |
7 ms |
1300 KB |
Output is partially correct |
44 |
Partially correct |
7 ms |
552 KB |
Output is partially correct |
45 |
Partially correct |
6 ms |
336 KB |
Output is partially correct |
46 |
Partially correct |
6 ms |
336 KB |
Output is partially correct |
47 |
Partially correct |
6 ms |
336 KB |
Output is partially correct |
48 |
Partially correct |
6 ms |
592 KB |
Output is partially correct |
49 |
Partially correct |
9 ms |
336 KB |
Output is partially correct |
50 |
Partially correct |
9 ms |
336 KB |
Output is partially correct |
51 |
Partially correct |
7 ms |
336 KB |
Output is partially correct |
52 |
Partially correct |
7 ms |
336 KB |
Output is partially correct |
53 |
Partially correct |
6 ms |
508 KB |
Output is partially correct |
54 |
Partially correct |
6 ms |
336 KB |
Output is partially correct |
55 |
Partially correct |
6 ms |
336 KB |
Output is partially correct |
56 |
Partially correct |
6 ms |
336 KB |
Output is partially correct |
57 |
Partially correct |
6 ms |
592 KB |
Output is partially correct |
58 |
Partially correct |
6 ms |
552 KB |
Output is partially correct |
59 |
Partially correct |
6 ms |
592 KB |
Output is partially correct |
60 |
Partially correct |
6 ms |
336 KB |
Output is partially correct |
61 |
Partially correct |
6 ms |
336 KB |
Output is partially correct |
62 |
Correct |
1 ms |
336 KB |
Output is correct |
63 |
Correct |
1 ms |
336 KB |
Output is correct |
64 |
Correct |
1 ms |
336 KB |
Output is correct |
65 |
Correct |
1 ms |
336 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 |
336 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 |
508 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 |
504 KB |
Output is correct |
78 |
Correct |
1 ms |
508 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 |
504 KB |
Output is correct |
90 |
Correct |
1 ms |
336 KB |
Output is correct |
91 |
Correct |
1 ms |
336 KB |
Output is correct |