#include "mushrooms.h"
#include <bits/stdc++.h>
using namespace std;
#define pii pair<int, int>
#define ll long long
#define all(v) v.begin(), v.end()
#define oo 1e9
int count_mushrooms(int n){
srand(time(0));
//
int B = 141;
pair<int, int> best = {oo, 0};
for(int x = 3; x <= n; x++) best = min(best, {2 + (2 * (x - 2)) / 2 + (x & 1) + (n - 1) / x, x});
B = best.second;
//
vector<int> id;
for(int i = 0; i < n; i++) id.push_back(i);
random_shuffle(id.begin() + 1, id.end());
vector<int> v[2];
v[0] = {0};
int i = 1;
for(; i < min(3, n); i++){
v[use_machine({0, id[i]})].push_back(i);
}
int t = (v[0].size() < v[1].size());
for(; i + 1 < n && (int)max(v[0].size(), v[1].size()) < B; i += 2){
int a = use_machine({id[i], id[v[t][0]], id[i + 1], id[v[t][1]]});
v[t ^ (a & 1)].push_back(i);
v[t ^ bool(a & 2)].push_back(i + 1);
}
if(i == n - 1){
v[use_machine({0, id[i]})].push_back(i);
i++;
}
int ans = v[0].size();
if(i == n) return ans;
while(i < n){
t = (v[0].size() < v[1].size());
vector<int> tmp;
int lst = 0;
for(int j = 0; j < v[t].size() && i < n; j++, i++){
tmp.push_back(id[v[t][j]]);
tmp.push_back(id[i]);
}
int a = use_machine(tmp);
if(a & 1) v[t ^ 1].push_back(id[i - 1]);
if(t) ans += (use_machine(tmp) + 1) / 2;
else ans += (tmp.size() / 2) - (use_machine(tmp) + 1) / 2;
}
return ans;
}
Compilation message
mushrooms.cpp: In function 'int count_mushrooms(int)':
mushrooms.cpp:42:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
42 | for(int j = 0; j < v[t].size() && i < n; j++, i++){
| ~~^~~~~~~~~~~~~
mushrooms.cpp:41:7: warning: unused variable 'lst' [-Wunused-variable]
41 | int lst = 0;
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Incorrect |
1 ms |
344 KB |
Duplicate value 1050 in the query array. |
7 |
Halted |
0 ms |
0 KB |
- |