Submission #627696

# Submission time Handle Problem Language Result Execution time Memory
627696 2022-08-12T18:53:44 Z Tigerpants Rarest Insects (IOI22_insects) C++17
100 / 100
54 ms 292 KB
#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
#include <numeric>
#include <random>

using namespace std;

typedef vector<int> vi;
typedef set<int> si;
typedef vector<vi> vvi;
typedef vector<si> vsi;
typedef vector<bool> vb;

void move_inside(int i);
void move_outside(int i);
int press_button();

vector<int> po2 = {1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048};

int min_cardinality(int N) {
    vi x(N);
    for (int i = 0; i < N; i++) {
        x[i] = i;
    }
    
    auto rd = random_device {};
    auto rng = default_random_engine { rd() };
    shuffle(x.begin(), x.end(), rng);

    vb roots(N, false);
    vb inside(N, false);
    vb permanent(N, false);

    int no_roots = 1;
    roots[0] = true;
    move_inside(x[0]);
    inside[0] = true;
    permanent[0] = true;

    for (int i = 1; i < N; i++) {
        move_inside(x[i]);
        inside[i] = true;
        if (press_button() == 2) {
            move_outside(x[i]);
            inside[i] = false;
        } else {
            roots[i] = true;
            permanent[i] = true;
            no_roots++;
        }
    }

    vb active(N, true); // remove those which are too big
    
    if (2 * no_roots > N) {
        return 1;
    }

    if (no_roots == 1) {
        return N;
    }

    if (no_roots == 2) {
        // include root[0]
        // add next
        // press -> get which group
        // remove next
        int a = 1;
        int b = 1;
        for (int i = 1; i < N; i++) {
            if (roots[i]) {
                move_outside(x[i]);
            }
        }
        for (int i = 1; i < N; i++) {
            if (!roots[i]) {
                move_inside(x[i]);
                if (press_button() == 2) {
                    a++;
                } else {
                    b++;
                }
                move_outside(x[i]);
            }
        }
        return min<int>(a, b);
    }

    if (no_roots == 3) {
        int a = 1;
        int b = 1;
        int c = 1;
        int c_root = 0;
        for (int i = 1; i < N; i++) {
            if (roots[i]) {
                move_outside(x[i]);
                c_root = i;
            }
        }
        for (int i = 1; i < N; i++) {
            if (!roots[i]) {
                move_inside(x[i]);
                if (press_button() == 2) {
                    a++;
                } else {
                    move_inside(x[c_root]);
                    if (press_button() == 2) {
                        c++;
                    } else {
                        b++;
                    }
                    move_outside(x[c_root]);
                }
                move_outside(x[i]);
            }
        }
        return min<int>(a, min<int>(b, c));
    }

    int no_inside = no_roots;
    
    int high = (N / no_roots) + 1;
    int low = 1;
    while (low + 1 < high) {
        int middle = (low + high) / 2;

        // find minimum middle so log2(high - middle) is constant
        //int middle = high - *lower_bound(po2.begin(), po2.end(), high - ((low + middle) / 2));
        //cout << (low + high) / 2 << " " << middle << endl;

        for (int i = 0; i < N; i++) {
            if ((!inside[i]) && (active[i])) {
                move_inside(x[i]);
                inside[i] = true;
                no_inside++;
                if (no_inside >= no_roots + middle) {
                    if (press_button() > middle) {
                        move_outside(x[i]);
                        inside[i] = false;
                        no_inside--;
                    }
                }
            }
            if (no_inside == middle * no_roots) {break;}
        }
        if (no_inside == middle * no_roots) {
            low = middle;
            // don't remove them. make them permanent
            for (int i = 0; i < N; i++) {
                if (inside[i]) {
                    permanent[i] = true;
                }
            }
        } else {
            high = middle;
            // deactivate all higher ones
            // remove all non-permanent ones
            for (int i = 0; i < N; i++) {
                if (!inside[i]) {
                    active[i] = false;
                } else if (!permanent[i]) {
                    move_outside(x[i]);
                    no_inside--;
                    inside[i] = false;
                }
            }
        }
    }

    return low;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 208 KB Output is correct
2 Correct 0 ms 208 KB Output is correct
3 Correct 1 ms 208 KB Output is correct
4 Correct 0 ms 208 KB Output is correct
5 Correct 0 ms 208 KB Output is correct
6 Correct 2 ms 208 KB Output is correct
7 Correct 2 ms 208 KB Output is correct
8 Correct 4 ms 208 KB Output is correct
9 Correct 4 ms 208 KB Output is correct
10 Correct 3 ms 208 KB Output is correct
11 Correct 2 ms 208 KB Output is correct
12 Correct 4 ms 208 KB Output is correct
13 Correct 4 ms 208 KB Output is correct
14 Correct 4 ms 208 KB Output is correct
15 Correct 6 ms 208 KB Output is correct
16 Correct 4 ms 208 KB Output is correct
17 Correct 4 ms 208 KB Output is correct
18 Correct 6 ms 208 KB Output is correct
19 Correct 5 ms 208 KB Output is correct
20 Correct 3 ms 208 KB Output is correct
21 Correct 3 ms 208 KB Output is correct
22 Correct 2 ms 208 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 208 KB Output is correct
2 Correct 0 ms 208 KB Output is correct
3 Correct 1 ms 208 KB Output is correct
4 Correct 0 ms 208 KB Output is correct
5 Correct 0 ms 208 KB Output is correct
6 Correct 2 ms 208 KB Output is correct
7 Correct 2 ms 208 KB Output is correct
8 Correct 4 ms 208 KB Output is correct
9 Correct 4 ms 208 KB Output is correct
10 Correct 3 ms 208 KB Output is correct
11 Correct 2 ms 208 KB Output is correct
12 Correct 4 ms 208 KB Output is correct
13 Correct 4 ms 208 KB Output is correct
14 Correct 4 ms 208 KB Output is correct
15 Correct 6 ms 208 KB Output is correct
16 Correct 4 ms 208 KB Output is correct
17 Correct 4 ms 208 KB Output is correct
18 Correct 6 ms 208 KB Output is correct
19 Correct 5 ms 208 KB Output is correct
20 Correct 3 ms 208 KB Output is correct
21 Correct 3 ms 208 KB Output is correct
22 Correct 2 ms 208 KB Output is correct
23 Correct 9 ms 208 KB Output is correct
24 Correct 8 ms 208 KB Output is correct
25 Correct 15 ms 208 KB Output is correct
26 Correct 22 ms 208 KB Output is correct
27 Correct 16 ms 208 KB Output is correct
28 Correct 11 ms 208 KB Output is correct
29 Correct 25 ms 208 KB Output is correct
30 Correct 24 ms 208 KB Output is correct
31 Correct 17 ms 208 KB Output is correct
32 Correct 19 ms 208 KB Output is correct
33 Correct 18 ms 208 KB Output is correct
34 Correct 21 ms 208 KB Output is correct
35 Correct 23 ms 208 KB Output is correct
36 Correct 24 ms 208 KB Output is correct
37 Correct 25 ms 208 KB Output is correct
38 Correct 20 ms 208 KB Output is correct
39 Correct 19 ms 208 KB Output is correct
40 Correct 14 ms 208 KB Output is correct
41 Correct 9 ms 208 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 208 KB Output is correct
2 Correct 0 ms 208 KB Output is correct
3 Correct 1 ms 208 KB Output is correct
4 Correct 0 ms 208 KB Output is correct
5 Correct 0 ms 208 KB Output is correct
6 Correct 0 ms 208 KB Output is correct
7 Correct 20 ms 208 KB Output is correct
8 Correct 15 ms 208 KB Output is correct
9 Correct 34 ms 208 KB Output is correct
10 Correct 39 ms 208 KB Output is correct
11 Correct 29 ms 208 KB Output is correct
12 Correct 23 ms 208 KB Output is correct
13 Correct 46 ms 208 KB Output is correct
14 Correct 47 ms 208 KB Output is correct
15 Correct 34 ms 208 KB Output is correct
16 Correct 38 ms 208 KB Output is correct
17 Correct 43 ms 208 KB Output is correct
18 Correct 45 ms 208 KB Output is correct
19 Correct 39 ms 208 KB Output is correct
20 Correct 46 ms 208 KB Output is correct
21 Correct 48 ms 288 KB Output is correct
22 Correct 46 ms 292 KB Output is correct
23 Correct 44 ms 208 KB Output is correct
24 Correct 33 ms 208 KB Output is correct
25 Correct 24 ms 208 KB Output is correct
26 Correct 13 ms 208 KB Output is correct
27 Correct 43 ms 208 KB Output is correct
28 Correct 33 ms 208 KB Output is correct
29 Correct 25 ms 208 KB Output is correct
30 Correct 37 ms 208 KB Output is correct
31 Correct 49 ms 208 KB Output is correct
32 Correct 46 ms 208 KB Output is correct
33 Correct 41 ms 208 KB Output is correct
34 Correct 54 ms 208 KB Output is correct
35 Correct 38 ms 208 KB Output is correct
36 Correct 43 ms 208 KB Output is correct
37 Correct 47 ms 208 KB Output is correct
38 Correct 42 ms 208 KB Output is correct
39 Correct 37 ms 208 KB Output is correct
40 Correct 45 ms 208 KB Output is correct
41 Correct 31 ms 208 KB Output is correct
42 Correct 49 ms 208 KB Output is correct
43 Correct 11 ms 208 KB Output is correct
44 Correct 45 ms 208 KB Output is correct
45 Correct 19 ms 208 KB Output is correct
46 Correct 27 ms 208 KB Output is correct
47 Correct 29 ms 208 KB Output is correct
48 Correct 28 ms 208 KB Output is correct