Submission #526472

# Submission time Handle Problem Language Result Execution time Memory
526472 2022-02-14T22:17:27 Z peti1234 Counting Mushrooms (IOI20_mushrooms) C++17
100 / 100
8 ms 344 KB
#include <bits/stdc++.h>
#include "mushrooms.h"
using namespace std;
vector<int> a, b, sz;
// A es B gombak, es a kerdes
int sa, sb, pos, ert, x, adb;
// az a es b vektorok merete, pos: hagyadik gombatol vannak az ismeretlenek, ert=110 (gyok n), x a valasz az utolso kerdesre, adb: vegso valasz

// nehany egyszeru fuggveny
void pb(int x) {
    sz.push_back(x);
}
void ap(int x) {
    a.push_back(x);
}
void bp(int x) {
    b.push_back(x);
}
void cl() {
    sz.clear();
}
void add(int a) {
    if (x%2) bp(a);
    else ap(a);
}
void fadd(int a) {
    if (x%2) ap(a);
    else bp(a);
}
int kerd() {
    return use_machine(sz);
}
void ek(int a) {
    pb(0), pb(a);
    x=kerd();
    add(a),
    cl();
}


int count_mushrooms(int n) {
    // a vektorban vannak a biztos A tipusuak, B-ben a b tipusuak
    ap(0);
    ert=110;

    // naivan meg lehet csinalni
    if (n<220) {
        for (int i=1; i<n; i++) ek(i);
        return a.size();
    }

    // ket sima kerdes
    for (int i=1; i<=2; i++) ek(i);
    sa=a.size();

    // egy kerdessel ket uj ertek
    if (sa>1) {
        pb(a[0]), pb(3), pb(a[1]), pb(4);
        x=kerd();
        add(4), x/=2;
        add(3);
    } else {
        pb(b[0]), pb(3), pb(b[1]), pb(4);
        x=kerd();
        fadd(4), x/=2;
        fadd(3);
    }


    pos=5;
    sa=a.size(), sb=b.size();
    while(max(sa, sb)<ert) {
        /*
        kicsit mas, mint amit megbeszeltunk:
        A1A2A3 itt 3-at ki lehet talalni, az 1-2 akkor ha azonos
        ha kulonbozo, akkor B1BA2A4A5, mind a 4-et ki lehet talalni hasonloan, ahogy megbeszeltuk
        ha nincs eleg B, akkor a korabbi egyszerubb modszerrel kell kitalalni: A1A4 - 4 a paritasbol egyertelmu, 1 pedig a masodik bitbol
        igy ket kerdesbol 4 uj elem lett, de ez legfeljebb ketszer tortenhet meg
        */

        cl();


        // a ket 25 soros resz ugyanaz, csak az A es B forditva van
        if (sa>2) {
            pb(a[0]), pb(pos), pb(a[1]), pb(pos+1), pb(a[2]), pb(pos+2);
            x=kerd();
            add(pos+2);
            if (x<2) ap(pos), ap(pos+1), pos+=3;
            else if (x>=4) bp(pos), bp(pos+1), pos+=3;
            else {
                if (sb>1) {
                    cl();
                    pb(b[0]), pb(pos), pb(b[1]), pb(a[0]), pb(pos+1), pb(a[1]), pb(pos+3), pb(a[2]), pb(pos+4);
                    x=kerd()-1;
                    add(pos+4);
                    x/=2;
                    add(pos+3);
                    x/=2;
                    add(pos+1), fadd(pos);
                    pos+=5;
                } else {
                    cl();
                    pb(a[0]), pb(pos), pb(a[1]), pb(pos+3);
                    x=kerd();
                    add(pos+3), x/=2;
                    add(pos), fadd(pos+1);
                    pos+=4;
                }
            }
        } else {
            pb(b[0]), pb(pos), pb(b[1]), pb(pos+1), pb(b[2]), pb(pos+2);
            x=kerd();
            fadd(pos+2);
            if (x<2) bp(pos), bp(pos+1), pos+=3;
            else if (x>=4) ap(pos), ap(pos+1), pos+=3;
            else {
                if (sa>1) {
                    cl();
                    pb(a[0]), pb(pos), pb(a[1]), pb(b[0]), pb(pos+1), pb(b[1]), pb(pos+3), pb(b[2]), pb(pos+4);
                    x=kerd()-1;
                    fadd(pos+4);
                    x/=2;
                    fadd(pos+3);
                    x/=2;
                    fadd(pos+1), add(pos);
                    pos+=5;
                } else {
                    cl();
                    pb(b[0]), pb(pos), pb(b[1]), pb(pos+3);
                    x=kerd();
                    fadd(pos+3), x/=2;
                    fadd(pos), add(pos+1);
                    pos+=4;
                }
            }
        }
        sa=a.size(), sb=b.size();
    }
    while(pos<n) {
        // eleg sok azonos van, innentol A_A_A_....A_ kerdesek, az utolsorol pontosan lehet tudni, hogy mi
        cl();
        if (sa>=sb) {
            for (int i=0; i<sa && pos+i<n; i++) {
                pb(a[i]), pb(pos+i);
            }
            x=kerd();
            int y=sz.back();
            add(y);
            pos=1+y;
            int si=sz.size();
            adb+=((si-2)/2-x/2);

        } else {
            for (int i=0; i<sb && pos+i<n; i++) {
                pb(b[i]), pb(pos+i);
            }
            x=kerd();
            int y=sz.back();
            fadd(y);
            pos=1+y;
            adb+=(x/2);
        }
        sa=a.size(), sb=b.size();
    }
    return sa+adb;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 216 KB Output is correct
2 Correct 0 ms 216 KB Output is correct
3 Correct 1 ms 308 KB Output is correct
4 Correct 0 ms 216 KB Output is correct
5 Correct 3 ms 216 KB Output is correct
6 Correct 2 ms 216 KB Output is correct
7 Correct 5 ms 300 KB Output is correct
8 Correct 5 ms 216 KB Output is correct
9 Correct 6 ms 308 KB Output is correct
10 Correct 5 ms 216 KB Output is correct
11 Correct 6 ms 216 KB Output is correct
12 Correct 6 ms 320 KB Output is correct
13 Correct 5 ms 216 KB Output is correct
14 Correct 4 ms 320 KB Output is correct
15 Correct 6 ms 216 KB Output is correct
16 Correct 6 ms 300 KB Output is correct
17 Correct 3 ms 200 KB Output is correct
18 Correct 6 ms 304 KB Output is correct
19 Correct 6 ms 200 KB Output is correct
20 Correct 5 ms 200 KB Output is correct
21 Correct 6 ms 200 KB Output is correct
22 Correct 7 ms 300 KB Output is correct
23 Correct 7 ms 212 KB Output is correct
24 Correct 5 ms 200 KB Output is correct
25 Correct 7 ms 304 KB Output is correct
26 Correct 8 ms 200 KB Output is correct
27 Correct 6 ms 312 KB Output is correct
28 Correct 5 ms 200 KB Output is correct
29 Correct 5 ms 200 KB Output is correct
30 Correct 5 ms 308 KB Output is correct
31 Correct 6 ms 324 KB Output is correct
32 Correct 8 ms 216 KB Output is correct
33 Correct 5 ms 216 KB Output is correct
34 Correct 6 ms 216 KB Output is correct
35 Correct 6 ms 324 KB Output is correct
36 Correct 8 ms 316 KB Output is correct
37 Correct 5 ms 216 KB Output is correct
38 Correct 6 ms 216 KB Output is correct
39 Correct 6 ms 216 KB Output is correct
40 Correct 6 ms 312 KB Output is correct
41 Correct 6 ms 216 KB Output is correct
42 Correct 5 ms 216 KB Output is correct
43 Correct 5 ms 216 KB Output is correct
44 Correct 7 ms 216 KB Output is correct
45 Correct 6 ms 324 KB Output is correct
46 Correct 6 ms 216 KB Output is correct
47 Correct 6 ms 216 KB Output is correct
48 Correct 6 ms 216 KB Output is correct
49 Correct 6 ms 312 KB Output is correct
50 Correct 5 ms 328 KB Output is correct
51 Correct 6 ms 304 KB Output is correct
52 Correct 6 ms 216 KB Output is correct
53 Correct 6 ms 216 KB Output is correct
54 Correct 5 ms 324 KB Output is correct
55 Correct 6 ms 324 KB Output is correct
56 Correct 5 ms 216 KB Output is correct
57 Correct 6 ms 324 KB Output is correct
58 Correct 6 ms 216 KB Output is correct
59 Correct 7 ms 216 KB Output is correct
60 Correct 7 ms 216 KB Output is correct
61 Correct 5 ms 344 KB Output is correct
62 Correct 0 ms 216 KB Output is correct
63 Correct 0 ms 216 KB Output is correct
64 Correct 0 ms 216 KB Output is correct
65 Correct 0 ms 216 KB Output is correct
66 Correct 0 ms 216 KB Output is correct
67 Correct 1 ms 216 KB Output is correct
68 Correct 0 ms 216 KB Output is correct
69 Correct 0 ms 216 KB Output is correct
70 Correct 1 ms 216 KB Output is correct
71 Correct 0 ms 216 KB Output is correct
72 Correct 0 ms 216 KB Output is correct
73 Correct 0 ms 216 KB Output is correct
74 Correct 0 ms 216 KB Output is correct
75 Correct 1 ms 216 KB Output is correct
76 Correct 0 ms 216 KB Output is correct
77 Correct 0 ms 216 KB Output is correct
78 Correct 1 ms 216 KB Output is correct
79 Correct 0 ms 216 KB Output is correct
80 Correct 0 ms 216 KB Output is correct
81 Correct 1 ms 216 KB Output is correct
82 Correct 0 ms 216 KB Output is correct
83 Correct 0 ms 216 KB Output is correct
84 Correct 0 ms 216 KB Output is correct
85 Correct 0 ms 216 KB Output is correct
86 Correct 0 ms 216 KB Output is correct
87 Correct 1 ms 216 KB Output is correct
88 Correct 0 ms 204 KB Output is correct
89 Correct 1 ms 200 KB Output is correct
90 Correct 0 ms 208 KB Output is correct
91 Correct 0 ms 216 KB Output is correct