#include "mushrooms.h"
#include <bits/stdc++.h>
using namespace std;
int seed = time(0);
bool swapped = false;
int i = 1;
int just_count_A = 0;
int just_count_B = 0;
vector<int> A = { 0 };
vector<int> B = { };
vector<int> idx;
int calc_pivots_size(int n) {
return 1 + 3*sqrt(n)/4;
}
void make_swap() {
swapped = not swapped;
swap(just_count_A, just_count_B);
swap(A, B);
}
bool decide_swap() {
if (A.size() < B.size()) {
make_swap();
}
return true;
}
int handle_parity(int parity) {
(parity == 0 ? A : B).push_back(idx[i]);
return 1;
}
int handle_pair(int raw_info) {
int even024 = raw_info - (raw_info%2);
if (even024 == 2) {
throw invalid_argument("conflict: need more info to handle this case");
}
(even024 == 0 ? A : B).push_back(idx[i]);
(even024 == 0 ? A : B).push_back(idx[i+1]);
return 2;
}
int handle_conflict_pair(int raw_info) {
int flag3b = raw_info - 1;
(flag3b & 0b100 ? A : B).push_back(idx[i]);
(flag3b & 0b100 ? B : A).push_back(idx[i+1]);
(flag3b & 0b010 ? B : A).push_back(idx[i+2]);
(flag3b & 0b001 ? B : A).push_back(idx[i+3]);
return 4;
}
void get_pivots(int n) {
int info;
int size = calc_pivots_size(n);
while (decide_swap() and (int)A.size() < size and i+5 < n) {
if (A.size() < 2) {
i += handle_parity(use_machine({ idx[i], A[0] }));
} else if (A.size() < 3 or B.size() < 2) {
info = use_machine({ idx[i], A[0], idx[i+1], A[1] });
i += handle_parity(info%2);
i += handle_parity(info/2);
} else {
info = use_machine({ idx[i], A[0], idx[i+1], A[1], idx[i+2], A[2] });
i += handle_parity(info%2);
try {
i += handle_pair(info);
} catch (const invalid_argument &) {
info = use_machine({ B[0], idx[i], B[1], A[0], idx[i+1], A[1], idx[i+2], A[2], idx[i+3] });
i += handle_conflict_pair(info);
}
}
}
}
vector<int> make_sample(int size) {
vector<int> sample = { };
for (int j=0; j<size; j++) {
sample.insert(sample.end(), { idx[i+j], A[j] });
}
return sample;
}
void count_the_rest(int n) {
while (decide_swap() and i < n) {
int test_size = min((int)A.size(), n-i);
int info = use_machine(make_sample(test_size));
i += handle_parity(info%2);
i += test_size-1;
just_count_A += (test_size-1) - (info/2);
just_count_B += info/2;
}
}
int count_mushrooms(int n) {
srand(seed);
idx.push_back(0);
for(int i=1;i<n;i++) idx.push_back(i);
random_shuffle(idx.begin()+1, idx.end());
get_pivots(n);
count_the_rest(n);
if (swapped) {
make_swap();
}
return A.size() + just_count_A;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
3 |
Correct |
0 ms |
256 KB |
Output is correct |
4 |
Correct |
1 ms |
256 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
3 ms |
384 KB |
Output is correct |
7 |
Correct |
7 ms |
512 KB |
Output is correct |
8 |
Correct |
9 ms |
512 KB |
Output is correct |
9 |
Correct |
10 ms |
512 KB |
Output is correct |
10 |
Correct |
9 ms |
512 KB |
Output is correct |
11 |
Correct |
9 ms |
512 KB |
Output is correct |
12 |
Correct |
9 ms |
512 KB |
Output is correct |
13 |
Correct |
9 ms |
512 KB |
Output is correct |
14 |
Correct |
6 ms |
512 KB |
Output is correct |
15 |
Correct |
10 ms |
512 KB |
Output is correct |
16 |
Correct |
11 ms |
512 KB |
Output is correct |
17 |
Correct |
7 ms |
384 KB |
Output is correct |
18 |
Correct |
11 ms |
512 KB |
Output is correct |
19 |
Correct |
10 ms |
512 KB |
Output is correct |
20 |
Correct |
10 ms |
512 KB |
Output is correct |
21 |
Correct |
9 ms |
512 KB |
Output is correct |
22 |
Correct |
9 ms |
512 KB |
Output is correct |
23 |
Correct |
11 ms |
512 KB |
Output is correct |
24 |
Correct |
7 ms |
384 KB |
Output is correct |
25 |
Correct |
10 ms |
512 KB |
Output is correct |
26 |
Correct |
10 ms |
512 KB |
Output is correct |
27 |
Correct |
9 ms |
512 KB |
Output is correct |
28 |
Correct |
13 ms |
512 KB |
Output is correct |
29 |
Correct |
10 ms |
512 KB |
Output is correct |
30 |
Correct |
10 ms |
512 KB |
Output is correct |
31 |
Correct |
9 ms |
512 KB |
Output is correct |
32 |
Correct |
12 ms |
512 KB |
Output is correct |
33 |
Correct |
10 ms |
512 KB |
Output is correct |
34 |
Correct |
10 ms |
512 KB |
Output is correct |
35 |
Correct |
9 ms |
512 KB |
Output is correct |
36 |
Correct |
10 ms |
512 KB |
Output is correct |
37 |
Correct |
9 ms |
512 KB |
Output is correct |
38 |
Correct |
12 ms |
512 KB |
Output is correct |
39 |
Correct |
9 ms |
544 KB |
Output is correct |
40 |
Correct |
10 ms |
512 KB |
Output is correct |
41 |
Correct |
10 ms |
512 KB |
Output is correct |
42 |
Correct |
10 ms |
512 KB |
Output is correct |
43 |
Correct |
11 ms |
512 KB |
Output is correct |
44 |
Correct |
10 ms |
512 KB |
Output is correct |
45 |
Correct |
10 ms |
512 KB |
Output is correct |
46 |
Correct |
10 ms |
512 KB |
Output is correct |
47 |
Correct |
10 ms |
512 KB |
Output is correct |
48 |
Correct |
12 ms |
512 KB |
Output is correct |
49 |
Correct |
9 ms |
512 KB |
Output is correct |
50 |
Correct |
10 ms |
512 KB |
Output is correct |
51 |
Correct |
10 ms |
512 KB |
Output is correct |
52 |
Correct |
10 ms |
512 KB |
Output is correct |
53 |
Correct |
10 ms |
512 KB |
Output is correct |
54 |
Correct |
10 ms |
512 KB |
Output is correct |
55 |
Correct |
10 ms |
512 KB |
Output is correct |
56 |
Correct |
10 ms |
512 KB |
Output is correct |
57 |
Correct |
9 ms |
512 KB |
Output is correct |
58 |
Correct |
10 ms |
512 KB |
Output is correct |
59 |
Correct |
11 ms |
512 KB |
Output is correct |
60 |
Correct |
10 ms |
512 KB |
Output is correct |
61 |
Correct |
10 ms |
512 KB |
Output is correct |
62 |
Correct |
1 ms |
256 KB |
Output is correct |
63 |
Correct |
0 ms |
256 KB |
Output is correct |
64 |
Correct |
1 ms |
256 KB |
Output is correct |
65 |
Correct |
0 ms |
256 KB |
Output is correct |
66 |
Correct |
0 ms |
256 KB |
Output is correct |
67 |
Correct |
0 ms |
256 KB |
Output is correct |
68 |
Correct |
0 ms |
256 KB |
Output is correct |
69 |
Correct |
0 ms |
256 KB |
Output is correct |
70 |
Correct |
0 ms |
256 KB |
Output is correct |
71 |
Correct |
0 ms |
256 KB |
Output is correct |
72 |
Correct |
0 ms |
256 KB |
Output is correct |
73 |
Correct |
0 ms |
256 KB |
Output is correct |
74 |
Correct |
0 ms |
256 KB |
Output is correct |
75 |
Correct |
0 ms |
256 KB |
Output is correct |
76 |
Correct |
0 ms |
256 KB |
Output is correct |
77 |
Correct |
0 ms |
256 KB |
Output is correct |
78 |
Correct |
1 ms |
256 KB |
Output is correct |
79 |
Correct |
1 ms |
256 KB |
Output is correct |
80 |
Correct |
0 ms |
256 KB |
Output is correct |
81 |
Correct |
1 ms |
256 KB |
Output is correct |
82 |
Correct |
0 ms |
256 KB |
Output is correct |
83 |
Correct |
1 ms |
384 KB |
Output is correct |
84 |
Correct |
0 ms |
256 KB |
Output is correct |
85 |
Correct |
0 ms |
256 KB |
Output is correct |
86 |
Correct |
0 ms |
256 KB |
Output is correct |
87 |
Correct |
1 ms |
256 KB |
Output is correct |
88 |
Correct |
0 ms |
256 KB |
Output is correct |
89 |
Correct |
0 ms |
256 KB |
Output is correct |
90 |
Correct |
0 ms |
256 KB |
Output is correct |
91 |
Correct |
1 ms |
256 KB |
Output is correct |