Submission #429794

#TimeUsernameProblemLanguageResultExecution timeMemory
429794MarcoMeijerCounting Mushrooms (IOI20_mushrooms)C++14
79.58 / 100
12 ms456 KiB
#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 = 130;

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)++;
    }

    // A_A_
    if(b.size() > a.size()) switchAB();
    while(a.size() < SQ && 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();
        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(!next.empty()) {
        vi m;
        vi ca = a;
        int total = 0;
        RE(i,a.size()-1) {
            if(next.empty()) break;
            m.pb(ca.back()); ca.pop_back();
            m.pb(next.back()); next.pop_back();
            total++;
        }
        m.pb(ca.back()); ca.pop_back();
        int res = use_machine(m);
        cntB += res/2;
        cntA += total - res/2;
    }

    if(swapped) switchAB();
	return cntA;
}
#Verdict Execution timeMemoryGrader output
Fetching results...