/*
* Optimal Semi-Randomized solution for task mushrooms
* Multiple heuristics included in the first phase
* Including a decision tree to determine what to do based on the result of
* {a, 0, b, 0, c, 0, d, 0, e}
* Decision tree is calculated in a manner that for each answer to the above query
* Hold all different cases of a..e and see which query makes distinct answers
*
* Note that randomized solutions won't help a lot because grader is adaptive
*
* Author: Ali Behjati
*/
#include "mushrooms.h"
#include <iostream>
#include <cassert>
#include <map>
using namespace std;
const int N = 20000 + 100;
vector<map<int,int>> dec_tree = {
{{2, 2},
{1, 4},
{0, 8},
{3, 17}
},
{{4, 3},
{6, 5},
{2, 9},
{5, 18},
{3, 20},
{1, 24}
},
{{1, 6},
{3, 10},
{2, 12},
{5, 19},
{6, 21},
{4, 25}
},
{{1, 7},
{3, 11},
{5, 13},
{2, 22},
{6, 26},
{4, 28}
},
{{3, 14},
{1, 23},
{2, 27},
{4, 29}
}};
vector<vector<int>> dec_ques = {
{ 0, 1, 4, 2 },
{ 0, 1, 2, 4, -1, 3, -1 },
{ 1, 2, 3, 0, -2, 4, -2 },
{ 0, 1, 2, 4, -2, 3, -2 },
{ 0, 1, 2, -2, 3 }
};
vector<int> detected[2];
vector<int> chain[N];
void push(int t, int i) {
detected[t].push_back(i);
for (auto x : chain[i])
if( x < 0 )
push( t ^ 1 , -x );
else
push( t , x );
chain[i].clear();
}
int sz() {
return max(detected[0].size(), detected[1].size());
}
int perm[N];
int asked = 0;
int _use_machine(vector<int> a) {
asked++;
vector<int> b;
for(auto x: a)
b.push_back(perm[x]);
return use_machine(b);
}
int count_mushrooms(int n) {
detected[0].push_back(0);
srand( 11 );
for(int i = 1; i < n; i++) {
perm[i] = i;
swap( perm[ rand() % i + 1 ] , perm[i] );
}
int i;
for(i = 1; i < n && sz() < 2; i++) {
int res = _use_machine({0, i});
detected[res].push_back(i);
}
int k = 100;
int t = (detected[1].size() == 2);
int unknown = -1;
for(; i < n && sz() < k && i < 2*k ; i += 2) {
unknown = -1;
int res;
if (i+4 < n && sz() >= 4 && detected[0].size() >= 2 && detected[1].size() >= 2) {
int t = (detected[1].size() == sz());
int res = _use_machine({i, detected[t][0], i+1, detected[t][1], i+2, detected[t][2], i+3, detected[t][3], i+4});
if (res ==0) {
push(t, i);
push(t, i+1);
push(t, i+2);
push(t, i+3);
push(t, i+4);
i += 3;
} else if (res == 8) {
push(t^1, i);
push(t^1, i+1);
push(t^1, i+2);
push(t^1, i+3);
push(t^1, i+4);
i += 3;
} else if (res == 1) {
push(t, i+1);
push(t, i+2);
push(t, i+3);
chain[i+4].push_back(-i);
i += 2;
} else if (res == 7) {
push(t^1, i+1);
push(t^1, i+2);
push(t^1, i+3);
chain[i+4].push_back(-i);
i += 2;
} else {
assert(2 <= res && res <= 6);
res -= 2;
vector<int> ask;
int ptr[2] = {0,0};
for(auto x: dec_ques[res]) {
if (x >= 0)
ask.push_back(i+x);
else {
int tpe = (x+2) ^ t;
ask.push_back(detected[tpe][ptr[tpe]++]);
}
}
int new_res = _use_machine(ask);
int mask = dec_tree[res][new_res];
for(int j = 0; j < 5; j++)
push(((mask>>j) & 1)^t, i+j);
i += 3;
}
} else {
if(i+1 < n)
res = _use_machine({detected[t][0], i, detected[t][1], i+1});
else
res = _use_machine({detected[t][0], i, detected[t][1]});
int not_t = (res & 2) ? 1 : 0;
push(t ^ not_t, i);
if(i+1 < n) {
not_t = res & 1;
push(t ^ not_t, i+1);
}
}
}
assert( asked < 2 * k );
int more_zeros = 0;
for(; i < n; ) {
int size = sz();
int t = (detected[1].size() == sz());
int rem = min(size, n - i);
vector<int> tmp;
for(int j = 0; j < rem; j++) {
tmp.push_back(i+j);
tmp.push_back(detected[t][j]);
}
int res = _use_machine(tmp);
more_zeros += (t ? res / 2 : rem - 1 - res / 2);
int not_t = (res & 1);
push(t ^ not_t, i);
i += rem;
}
return detected[0].size() + more_zeros;
}
Compilation message
mushrooms.cpp: In function 'int count_mushrooms(int)':
mushrooms.cpp:115:41: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
115 | int t = (detected[1].size() == sz());
| ~~~~~~~~~~~~~~~~~~~^~~~~~~
mushrooms.cpp:187:37: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
187 | int t = (detected[1].size() == sz());
| ~~~~~~~~~~~~~~~~~~~^~~~~~~
mushrooms.cpp:110:9: warning: variable 'unknown' set but not used [-Wunused-but-set-variable]
110 | int unknown = -1;
| ^~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
856 KB |
Output is correct |
2 |
Correct |
0 ms |
856 KB |
Output is correct |
3 |
Correct |
0 ms |
856 KB |
Output is correct |
4 |
Correct |
1 ms |
856 KB |
Output is correct |
5 |
Correct |
1 ms |
856 KB |
Output is correct |
6 |
Correct |
1 ms |
936 KB |
Output is correct |
7 |
Correct |
4 ms |
856 KB |
Output is correct |
8 |
Correct |
4 ms |
856 KB |
Output is correct |
9 |
Correct |
4 ms |
1028 KB |
Output is correct |
10 |
Correct |
4 ms |
856 KB |
Output is correct |
11 |
Correct |
5 ms |
856 KB |
Output is correct |
12 |
Correct |
4 ms |
856 KB |
Output is correct |
13 |
Correct |
4 ms |
852 KB |
Output is correct |
14 |
Correct |
2 ms |
984 KB |
Output is correct |
15 |
Correct |
6 ms |
1112 KB |
Output is correct |
16 |
Correct |
9 ms |
856 KB |
Output is correct |
17 |
Correct |
4 ms |
1112 KB |
Output is correct |
18 |
Correct |
4 ms |
856 KB |
Output is correct |
19 |
Correct |
4 ms |
856 KB |
Output is correct |
20 |
Correct |
4 ms |
856 KB |
Output is correct |
21 |
Correct |
4 ms |
1012 KB |
Output is correct |
22 |
Correct |
5 ms |
1004 KB |
Output is correct |
23 |
Correct |
5 ms |
1000 KB |
Output is correct |
24 |
Correct |
3 ms |
960 KB |
Output is correct |
25 |
Correct |
5 ms |
856 KB |
Output is correct |
26 |
Correct |
5 ms |
1000 KB |
Output is correct |
27 |
Correct |
5 ms |
856 KB |
Output is correct |
28 |
Correct |
6 ms |
896 KB |
Output is correct |
29 |
Correct |
6 ms |
856 KB |
Output is correct |
30 |
Correct |
5 ms |
856 KB |
Output is correct |
31 |
Correct |
6 ms |
1000 KB |
Output is correct |
32 |
Correct |
5 ms |
856 KB |
Output is correct |
33 |
Correct |
5 ms |
856 KB |
Output is correct |
34 |
Correct |
5 ms |
856 KB |
Output is correct |
35 |
Correct |
5 ms |
856 KB |
Output is correct |
36 |
Correct |
6 ms |
1008 KB |
Output is correct |
37 |
Correct |
5 ms |
1000 KB |
Output is correct |
38 |
Correct |
4 ms |
856 KB |
Output is correct |
39 |
Correct |
5 ms |
1012 KB |
Output is correct |
40 |
Correct |
7 ms |
856 KB |
Output is correct |
41 |
Correct |
4 ms |
856 KB |
Output is correct |
42 |
Correct |
4 ms |
856 KB |
Output is correct |
43 |
Correct |
8 ms |
856 KB |
Output is correct |
44 |
Correct |
5 ms |
856 KB |
Output is correct |
45 |
Correct |
5 ms |
1112 KB |
Output is correct |
46 |
Correct |
4 ms |
856 KB |
Output is correct |
47 |
Correct |
4 ms |
856 KB |
Output is correct |
48 |
Correct |
4 ms |
856 KB |
Output is correct |
49 |
Correct |
4 ms |
1020 KB |
Output is correct |
50 |
Correct |
4 ms |
992 KB |
Output is correct |
51 |
Correct |
4 ms |
856 KB |
Output is correct |
52 |
Correct |
4 ms |
856 KB |
Output is correct |
53 |
Correct |
4 ms |
856 KB |
Output is correct |
54 |
Correct |
4 ms |
856 KB |
Output is correct |
55 |
Correct |
5 ms |
856 KB |
Output is correct |
56 |
Correct |
4 ms |
856 KB |
Output is correct |
57 |
Correct |
4 ms |
1008 KB |
Output is correct |
58 |
Correct |
4 ms |
856 KB |
Output is correct |
59 |
Correct |
4 ms |
996 KB |
Output is correct |
60 |
Correct |
5 ms |
1004 KB |
Output is correct |
61 |
Correct |
4 ms |
856 KB |
Output is correct |
62 |
Correct |
0 ms |
856 KB |
Output is correct |
63 |
Correct |
0 ms |
856 KB |
Output is correct |
64 |
Correct |
1 ms |
856 KB |
Output is correct |
65 |
Correct |
1 ms |
856 KB |
Output is correct |
66 |
Correct |
1 ms |
856 KB |
Output is correct |
67 |
Correct |
0 ms |
856 KB |
Output is correct |
68 |
Correct |
0 ms |
856 KB |
Output is correct |
69 |
Correct |
0 ms |
856 KB |
Output is correct |
70 |
Correct |
0 ms |
856 KB |
Output is correct |
71 |
Correct |
1 ms |
856 KB |
Output is correct |
72 |
Correct |
0 ms |
856 KB |
Output is correct |
73 |
Correct |
1 ms |
856 KB |
Output is correct |
74 |
Correct |
0 ms |
856 KB |
Output is correct |
75 |
Correct |
1 ms |
856 KB |
Output is correct |
76 |
Correct |
1 ms |
856 KB |
Output is correct |
77 |
Correct |
1 ms |
856 KB |
Output is correct |
78 |
Correct |
0 ms |
856 KB |
Output is correct |
79 |
Correct |
1 ms |
856 KB |
Output is correct |
80 |
Correct |
0 ms |
856 KB |
Output is correct |
81 |
Correct |
0 ms |
856 KB |
Output is correct |
82 |
Correct |
1 ms |
852 KB |
Output is correct |
83 |
Correct |
1 ms |
856 KB |
Output is correct |
84 |
Correct |
0 ms |
856 KB |
Output is correct |
85 |
Correct |
1 ms |
856 KB |
Output is correct |
86 |
Correct |
1 ms |
856 KB |
Output is correct |
87 |
Correct |
1 ms |
856 KB |
Output is correct |
88 |
Correct |
0 ms |
856 KB |
Output is correct |
89 |
Correct |
1 ms |
856 KB |
Output is correct |
90 |
Correct |
1 ms |
856 KB |
Output is correct |
91 |
Correct |
0 ms |
856 KB |
Output is correct |