# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
429874 | MarcoMeijer | Counting Mushrooms (IOI20_mushrooms) | C++14 | 12 ms | 512 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>
using namespace std;
// macros
typedef long long ll;
typedef long double ld;
typedef pair<int, int> ii;
typedef pair<ll, ll> lll;
typedef tuple<int, int, int> iii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<iii> viii;
typedef vector<ll> vll;
typedef vector<lll> vlll;
#define REP(a,b,c) for(int a=int(b); a<int(c); a++)
#define RE(a,c) REP(a,0,c)
#define RE1(a,c) REP(a,1,c+1)
#define REI(a,b,c) REP(a,b,c+1)
#define REV(a,b,c) for(int a=int(c-1); a>=int(b); a--)
#define FOR(a,b) for(auto& a : b)
#define all(a) a.begin(), a.end()
#define INF 1e9
#define EPS 1e-9
#define pb push_back
#define popb pop_back
#define fi first
#define se second
#define sz size()
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
// output
template<class T1, class T2> void OUT(const pair<T1,T2>& x);
template<class T> void OUT(const vector<T>& x);
template<class T> void OUT(const T& x) {cout << x;}
template<class H, class... T> void OUT(const H& h, const T&... t) {OUT(h); OUT(t...); }
template<class T1, class T2> void OUT(const pair<T1,T2>& x) {OUT(x.fi,' ',x.se);}
template<class T> void OUT(const vector<T>& x) {RE(i,x.size()) OUT(i==0?"":" ",x[i]);}
template<class... T> void OUTL(const T&... t) {OUT(t..., "\n"); }
template<class H> void OUTLS(const H& h) {OUTL(h); }
template<class H, class... T> void OUTLS(const H& h, const T&... t) {OUT(h,' '); OUTLS(t...); }
const int SQ = 195;
int count_mushrooms(int n) {
vi a, b;
a.pb(0);
int cntA=1, cntB=0;
bool swapped = 0;
auto switchAB = [&]() {
swap(cntA,cntB);
swap(a,b);
swapped = !swapped;
};
vi next;
REP(i,1,n) next.pb(i);
// brute force if possible
if(n <= 227) {
while(!next.empty()) {
vi m;
m.pb(a.back());
m.pb(next.back()); next.pop_back();
int res = use_machine(m);
(res ? b : a).pb(m.back());
(res ? cntB : cntA)++;
}
return cntA;
}
// A_
while(a.size() < 2 && b.size() < 2) {
vi m;
m.pb(a.back());
m.pb(next.back()); next.pop_back();
int res = use_machine(m);
(res ? b : a).pb(m.back());
(res ? cntB : cntA)++;
if(b.size() > a.size()) switchAB();
}
// A_A_
auto A_A_ = [&](int x, int y) {
vi m;
m.pb(a[0]);
m.pb(x);
m.pb(a[1]);
m.pb(y);
int res = use_machine(m);
bool res1 = res&1;
bool res2 = res&2;
(res1 ? b : a).pb(m[3]);
(res1 ? cntB : cntA)++;
(res2 ? b : a).pb(m[1]);
(res2 ? cntB : cntA)++;
if(b.size() > a.size()) switchAB();
};
while(a.size() < 3 && b.size() < 3) {
int x = next.back(); next.pop_back();
int y = next.back(); next.pop_back();
A_A_(x,y);
}
// 1 3 5 => 2 4
// A_A_A_ => A_A_
while(a.size() < SQ && b.size() < 2) {
vi m;
m.pb(a[0]);
m.pb(next.back()); next.pop_back();
m.pb(a[1]);
m.pb(next.back()); next.pop_back();
m.pb(a[2]);
m.pb(next.back()); next.pop_back();
int res = use_machine(m);
if(res&1) {
b.pb(m[5]); cntB++;
} else {
a.pb(m[5]); cntA++;
}
if(res&2) {
A_A_(m[1],m[3]);
} else {
if(res&4) {
b.pb(m[1]), b.pb(m[3]); cntB+=2;
} else {
a.pb(m[1]), a.pb(m[3]); cntA+=2;
}
}
if(b.size() > a.size()) switchAB();
}
// A_A_A_ => B_BA_A_A_
while(a.size() + b.size() < SQ) {
vi m;
m.pb(a[0]);
m.pb(next.back()); next.pop_back();
m.pb(a[1]);
m.pb(next.back()); next.pop_back();
m.pb(a[2]);
m.pb(next.back()); next.pop_back();
int res = use_machine(m);
if(res&1) {
b.pb(m[5]); cntB++;
} else {
a.pb(m[5]); cntA++;
}
if(res&2) {
// 1 4 6 8
// B_BA_A_A_
vi p;
p.pb(b[0]);
p.pb(m[1]);
p.pb(b[1]);
p.pb(a[0]);
p.pb(m[3]);
p.pb(a[1]);
p.pb(next.back()); next.pop_back();
p.pb(a[2]);
p.pb(next.back()); next.pop_back();
int res = use_machine(p) - 1;
if(res&1) {
b.pb(p[8]); cntB++;
} else {
a.pb(p[8]); cntA++;
}
if(res&2) {
b.pb(p[6]); cntB++;
} else {
a.pb(p[6]); cntA++;
}
if(res&4) {
b.pb(p[4]); cntB++;
a.pb(p[1]); cntA++;
} else {
a.pb(p[4]); cntA++;
b.pb(p[1]); cntB++;
}
} else {
if(res&4) {
b.pb(m[1]), b.pb(m[3]); cntB+=2;
} else {
a.pb(m[1]), a.pb(m[3]); cntA+=2;
}
}
if(b.size() > a.size()) switchAB();
}
while(!next.empty()) {
if(b.size() > a.size()) switchAB();
vi m;
vi ca = a;
int total = 0;
RE(i,a.size()) {
if(next.empty()) break;
m.pb(ca.back()); ca.pop_back();
m.pb(next.back()); next.pop_back();
total++;
}
int res = use_machine(m);
if(res&1) {
b.pb(m.back());
res++;
} else {
a.pb(m.back());
res++;
}
cntB += res/2;
cntA += total - res/2;
}
if(swapped) switchAB();
return cntA;
}
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |