# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
432571 | TryMax | Counting Mushrooms (IOI20_mushrooms) | C++17 | 15 ms | 328 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "mushrooms.h"
#include <bits/stdc++.h>
#ifdef LOCAL
#include "stub.cpp"
#endif // LOCAL
#define f first
#define s second
#define pb push_back
using namespace std;
const int N = 2e5 + 10, C = 200;
int a[N];
int small(int n){
int ans = 1;
for(int i = 1; i < n; ++i){
vector<int> x;
x.pb(i - 1);
x.pb(i);
a[i] = a[i - 1] ^ use_machine(x);
ans += (a[i] == 0);
}
return ans;
}
bool ask(int a, int b){
vector<int> x;
x.pb(a), x.pb(b);
return use_machine(x);
}
int count_mushrooms(int n){
if(n <= C)
return small(n);
a[C - 1] = a[0] ^ ask(0, C - 1);
int ans = (a[0] == 0) + (a[C - 1] == 0);
for(int i = 1; i < C - 1 - i; ++i){
vector<int> x;
x.pb(i);
x.pb(i - 1);
x.pb(C - 1 - i + 1);
x.pb(C - 1 - i);
int res = use_machine(x);
if(a[i - 1] != a[C - 1 - i + 1])
--res;
if(res == 0)
a[i] = a[i - 1], a[C - 1 - i] = a[C - i];
else if(res == 2)
a[i] = a[i - 1] ^ 1, a[C - 1 - i] = a[C - i] ^ 1;
else{
if(ask(i, i - 1))
a[i] = a[i - 1] ^ 1, a[C - 1 - i] = a[C - i];
else
a[i] = a[i - 1], a[C - 1 - i] = a[C - i] ^ 1;
}
ans += (a[i] == 0) + (a[C - i - 1] == 0);
}
for(int i = 1; i <= (n + C - 1) / C; ++i){
vector<int> x;
for(int j = 0; j < min(n, C); ++j)
if(a[j] == 0){
if(i * C + j >= n)
break;
x.pb(j), x.pb(i * C + j);
}
if(!x.empty()){
int cntb = (use_machine(x) + 1) / 2;
ans += x.size() / 2 - cntb;
}
x.clear();
for(int j = 0; j < min(n, C); ++j)
if(a[j] == 1){
if(i * C + j >= n)
break;
x.pb(j), x.pb(i * C + j);
}
if(!x.empty()){
int cnta = (use_machine(x) + 1) / 2;
ans += cnta;
}
}
return ans;
}
/*
3
0 1 1
1
2
10
0 0 0 0 0 1 1 0 0 1
3
2
*/
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |