Submission #596025

# Submission time Handle Problem Language Result Execution time Memory
596025 2022-07-14T09:22:00 Z MadokaMagicaFan Counting Mushrooms (IOI20_mushrooms) C++14
100 / 100
12 ms 580 KB
#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using vi = vector<int>;
using pi = pair<int,int>;

#define all(v)          v.begin(),v.end()
#define sort(v)         sort(all(v))
#define endl            '\n'
#define forn(i,n)       for(int i = 0; i < n; ++i)
#define forbe(i,b,e)    for(int i = b; i < e; ++i)
#define forr(i,n)       for(int i = n-1; i >= 0; --i)
#define sz(v)           ((int)v.size())
#define pb              push_back
#define f               first
#define s               second

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

int S = 83;

int use_machine(vi x);

int count_mushrooms(int n) {
    int l, j, i, val, ans = 0, pnt = 3;

    if (n < 420) {
        for (i = 2; i < n; i+=2) ans += (2 - use_machine({i-1,0,i}));
        if ((n&1)==0) ans += (1 - use_machine({n-1,0}));
        return ans+1;
    }

    vi a, b, ask, rm, rem;
    a.pb(0);

    for (i = 1; i < n; ++i) rm.pb(i);
    shuffle(rm.begin(), rm.end(), rng);

    while (max(sz(a), sz(b)) < 2) {
        if (use_machine({0,rm.back()})) b.pb(rm.back());
        else a.pb(rm.back());
        rm.pop_back();
    }

    if (sz(b) > sz(a))
        swap(a,b);

    while (max(sz(a), sz(b)) < 3) {
        assert(sz(rm) > 1);
        ask.clear();
        i = rm.back(); rm.pop_back();
        j = rm.back(); rm.pop_back();

        ask = {a[0], i, a[1], j};
        val = use_machine(ask);
        if (val&1) b.pb(j);
        else       a.pb(j);
        if (val&2) b.pb(i);
        else       a.pb(i);
    }

    if (sz(b) > sz(a))
        swap(a,b);

    int cnt = 0;
    while (cnt < S && sz(b) < 2) {
        ++cnt;
        ask.clear();
        assert(sz(rm) > 2);
        i = rm.back(); rm.pop_back();
        j = rm.back(); rm.pop_back();

        assert(sz(rem) == 0 || sz(rem) == 2);
        if (sz(rem)) l = rem[0];
        else        {l = rm.back(); rm.pop_back();}

        ask = {a[0], i, a[1], j, a[2], l};
        val = use_machine(ask);
        
        if (val&1) {
            b.pb(l);
            if (sz(rem)) a.pb(rem[1]);
        } else {
            a.pb(l);
            if (sz(rem)) b.pb(rem[1]);
        }

        rem.clear();
        val >>= 1;

        if (val == 0) {
            a.pb(i), a.pb(j);
        } else if (val == 2) {
            b.pb(i), b.pb(j);
        } else {
            rem.pb(i), rem.pb(j);
        }
    }

    shuffle(rm.begin(), rm.end(), rng);
    int scnt = 0;
    while (cnt < S) {
        ++cnt;
        ++scnt;
        ask.clear();
        assert(sz(rm) > 2);
        i = rm.back(); rm.pop_back();
        j = rm.back(); rm.pop_back();

        assert(sz(rem) == 0 || sz(rem) == 2);
        if (sz(rem)) l = rem[0];
        else        {l = rm.back(); rm.pop_back();}

        ask = {a[0], i, a[1], b[0], j, b[1],  a[2], l};
        val = use_machine(ask)-2;
        
        if (val&1) {
            b.pb(l);
            if (sz(rem) && scnt==1) a.pb(rem[1]);
            if (sz(rem) && scnt!=1) b.pb(rem[1]);
        } else {
            a.pb(l);
            if (sz(rem) && scnt==1) b.pb(rem[1]);
            if (sz(rem) && scnt!=1) a.pb(rem[1]);
        }

        rem.clear();
        val >>= 1;

        if (val == 0) {
            a.pb(i), b.pb(j);
        } else if (val == 2) {
            b.pb(i), a.pb(j);
        } else {
            rem.pb(i), rem.pb(j);
        }
    }
    if (sz(rem)) { assert(sz(rem)==2);rm.pb(rem[0]), rm.pb(rem[1]); }


    shuffle(rm.begin(), rm.end(), rng);
    if (a[0] != 0) swap(a,b);
    ans = sz(a);

    while (sz(rm)) {
        ask.clear();
        if (sz(a) > sz(b)) {
            while (sz(rm) && sz(ask) < (sz(a)<<1)) {
                ask.pb(a[sz(ask)>>1]);
                ask.pb(rm.back()); rm.pop_back();
            }
            ans += (sz(ask)>>1);
            val = use_machine(ask);

            ans -= ((val+1)>>1);

            if (val&1) 
                b.pb(ask[sz(ask)-1]);
            else
                a.pb(ask[sz(ask)-1]);
        } else {
            while (sz(rm) && sz(ask) < (sz(b)<<1)) {
                ask.pb(b[sz(ask)>>1]);
                ask.pb(rm.back()); rm.pop_back();
            }
            val = use_machine(ask);
            ans += ((val+1)>>1);

            if (val&1) 
                a.pb(ask[sz(ask)-1]);
            else
                b.pb(ask[sz(ask)-1]);
        }
    }
    return ans;
}

#ifdef ONPC
vi rans;

int cnt = 0;

int use_machine(vi x){
    ++ cnt;
    int _ans = 0;
    /* cout << "ASK: "; */
    /* forn(i , sz(x)) */
    /*     cout << x[i] << ' '; */
    /*     cout << endl; */
    forn(i , sz(x)-1)
        if (rans[x[i]] != rans[x[i+1]])
            ++_ans;
    /* cout << "ANSWER: " << _ans << endl; */
    return _ans;
}

void solve() {
    int n;
    cin >> n;

    rans.resize(n);

    char c;

    int ranswer = 0;

    forn(i, n) {
        cin >> c;
        rans[i]=(c=='A');

        if (c=='A') ++ranswer;
    }

    int cans = count_mushrooms(n);

    /* if (cans != ranswer) */
    /*     forn(i, n) */
    /*         cout << rans[i] << ' '; */

    /* cout << endl; */
    /* cout << cans << ' ' << ranswer << endl; */
    /* cout << n << ' ' << cnt << endl; */

    cout << cnt << endl;
}

int main(int argc, char **argv) {
    if (argc > 1) {
        S = 0;
        int p = 0;
        while (argv[1][p] != '\0')
            S = S * 10 + (argv[1][p++] - '0');
    }
    /* freopen("infile", "r", stdin); */
//    ios_base::sync_with_stdio(0);cin.tie(0);
    solve();
}
#endif

Compilation message

mushrooms.cpp: In function 'int count_mushrooms(int)':
mushrooms.cpp:27:32: warning: unused variable 'pnt' [-Wunused-variable]
   27 |     int l, j, i, val, ans = 0, pnt = 3;
      |                                ^~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 208 KB Output is correct
2 Correct 1 ms 208 KB Output is correct
3 Correct 1 ms 208 KB Output is correct
4 Correct 1 ms 208 KB Output is correct
5 Correct 1 ms 208 KB Output is correct
6 Correct 2 ms 320 KB Output is correct
7 Correct 6 ms 464 KB Output is correct
8 Correct 11 ms 464 KB Output is correct
9 Correct 7 ms 512 KB Output is correct
10 Correct 8 ms 464 KB Output is correct
11 Correct 10 ms 464 KB Output is correct
12 Correct 8 ms 464 KB Output is correct
13 Correct 6 ms 464 KB Output is correct
14 Correct 4 ms 336 KB Output is correct
15 Correct 7 ms 464 KB Output is correct
16 Correct 11 ms 464 KB Output is correct
17 Correct 9 ms 412 KB Output is correct
18 Correct 7 ms 464 KB Output is correct
19 Correct 9 ms 464 KB Output is correct
20 Correct 6 ms 464 KB Output is correct
21 Correct 8 ms 464 KB Output is correct
22 Correct 10 ms 464 KB Output is correct
23 Correct 10 ms 464 KB Output is correct
24 Correct 7 ms 336 KB Output is correct
25 Correct 11 ms 464 KB Output is correct
26 Correct 8 ms 464 KB Output is correct
27 Correct 7 ms 464 KB Output is correct
28 Correct 10 ms 464 KB Output is correct
29 Correct 7 ms 464 KB Output is correct
30 Correct 8 ms 464 KB Output is correct
31 Correct 12 ms 464 KB Output is correct
32 Correct 7 ms 464 KB Output is correct
33 Correct 9 ms 464 KB Output is correct
34 Correct 8 ms 464 KB Output is correct
35 Correct 7 ms 464 KB Output is correct
36 Correct 6 ms 464 KB Output is correct
37 Correct 7 ms 464 KB Output is correct
38 Correct 9 ms 464 KB Output is correct
39 Correct 9 ms 464 KB Output is correct
40 Correct 9 ms 464 KB Output is correct
41 Correct 8 ms 464 KB Output is correct
42 Correct 9 ms 464 KB Output is correct
43 Correct 7 ms 464 KB Output is correct
44 Correct 11 ms 464 KB Output is correct
45 Correct 7 ms 464 KB Output is correct
46 Correct 7 ms 464 KB Output is correct
47 Correct 6 ms 464 KB Output is correct
48 Correct 11 ms 464 KB Output is correct
49 Correct 9 ms 464 KB Output is correct
50 Correct 6 ms 464 KB Output is correct
51 Correct 11 ms 464 KB Output is correct
52 Correct 7 ms 464 KB Output is correct
53 Correct 9 ms 464 KB Output is correct
54 Correct 9 ms 580 KB Output is correct
55 Correct 10 ms 464 KB Output is correct
56 Correct 7 ms 464 KB Output is correct
57 Correct 7 ms 464 KB Output is correct
58 Correct 9 ms 464 KB Output is correct
59 Correct 7 ms 464 KB Output is correct
60 Correct 6 ms 464 KB Output is correct
61 Correct 7 ms 464 KB Output is correct
62 Correct 0 ms 208 KB Output is correct
63 Correct 1 ms 208 KB Output is correct
64 Correct 0 ms 208 KB Output is correct
65 Correct 0 ms 208 KB Output is correct
66 Correct 0 ms 208 KB Output is correct
67 Correct 0 ms 208 KB Output is correct
68 Correct 1 ms 208 KB Output is correct
69 Correct 0 ms 208 KB Output is correct
70 Correct 1 ms 208 KB Output is correct
71 Correct 1 ms 208 KB Output is correct
72 Correct 1 ms 208 KB Output is correct
73 Correct 1 ms 208 KB Output is correct
74 Correct 0 ms 208 KB Output is correct
75 Correct 1 ms 208 KB Output is correct
76 Correct 0 ms 208 KB Output is correct
77 Correct 1 ms 208 KB Output is correct
78 Correct 0 ms 208 KB Output is correct
79 Correct 0 ms 208 KB Output is correct
80 Correct 0 ms 208 KB Output is correct
81 Correct 1 ms 208 KB Output is correct
82 Correct 1 ms 208 KB Output is correct
83 Correct 0 ms 208 KB Output is correct
84 Correct 0 ms 280 KB Output is correct
85 Correct 1 ms 208 KB Output is correct
86 Correct 1 ms 208 KB Output is correct
87 Correct 0 ms 208 KB Output is correct
88 Correct 0 ms 208 KB Output is correct
89 Correct 0 ms 208 KB Output is correct
90 Correct 1 ms 256 KB Output is correct
91 Correct 0 ms 208 KB Output is correct