Submission #628167

# Submission time Handle Problem Language Result Execution time Memory
628167 2022-08-13T06:57:28 Z I_love_Hoang_Yen Rarest Insects (IOI22_insects) C++17
99.89 / 100
76 ms 1312 KB
#include "insects.h"
#include "bits/stdc++.h"

#define SZ(s) ((int) s.size())
using namespace std;

#ifndef RR
#define DEBUG(X)
#endif

/*
int min_cardinality(int n) {
    int res = n + 1;  // final result

    // used(i) = true if we already counted species of i
    std::vector<bool> used(n, false);
    for (int i = 0; i < n; ++i) {
        if (!used[i]) {
            // set containing all insects of this species
            std::set<int> same_species {i};
            move_inside(i);

            for (int j = i + 1; j < n; ++j) {
                if (!used[j]) {
                    move_inside(j);
                    if (press_button() == 1 + SZ(same_species)) {
                        same_species.insert(j);
                    } else {
                        move_outside(j);
                    }
                }
            }

            res = std::min(res, SZ(same_species));

            // remove all insects from machine
            for (int j : same_species) {
                move_outside(j);
                used[j] = true;
            }
        }
    }
    return res;
}
*/

int min_cardinality(int n) {
    std::set<int> insides;  // set of insects inside machine
    // lambda functions {{{
    // lambda function to add one insect to machine
    auto add = [&] (int i) { move_inside(i); insides.insert(i); };
    // lambda function to remove one insect from machine
    auto remove = [&] (int i) { move_outside(i); insides.erase(i); };
    // }}}

    // machine_states(f) = set of insects inside machine after we
    //                     binary search with value f
    std::map<int, std::set<int>> machine_states;

    // Step 1: Find set containing unique insects
    for (int i = 0; i < n; ++i) {
        add(i);
        if (press_button() > 1) {  // this species appeared before
            remove(i);
        }
    }
    int unique_vals = SZ(insides);

    machine_states[1] = insides;
    machine_states[n] = std::set<int>();
    for (int i = 0; i < n; ++i) machine_states[n].insert(i);

    // Step 2: Binary search for min f
    int l = 2, r = n / unique_vals, res = 1;
    while (l <= r) {
        int mid = (l + r) / 2;

        assert(mid > res);
        auto it = machine_states.lower_bound(mid);
        std::set<int> just_added;
        for (int i : it->second) if (!insides.count(i)) {
            add(i);
            if (press_button() > mid) remove(i);
            else just_added.insert(i);
        }
        int total = SZ(insides);
        machine_states[mid] = insides;

        if (total >= unique_vals * mid) {
            res = mid;
            l = mid + 1;
        } else {
            r = mid - 1;
            for (int i : just_added) {
                remove(i);
            }
        }
    }
    return res;
}

/* 53.16
int min_cardinality(int n) {
    std::set<int> insides;  // set of insects inside machine
    // lambda functions {{{
    // lambda function to add one insect to machine
    auto add = [&] (int i) { move_inside(i); insides.insert(i); };
    // lambda function to remove one insect from machine
    auto remove = [&] (int i) { move_outside(i); insides.erase(i); };
    // }}}
 
    std::set<int> last_machine_state;  // store previous `good` state
 
    // Step 1: Find set containing unique insects
    for (int i = 0; i < n; ++i) {
        add(i);
        if (press_button() > 1) {  // this species appeared before
            remove(i);
        }
    }
    int unique_vals = SZ(insides);
 
    // Step 2: Binary search for min f
    int l = 2, r = n / unique_vals, res = 1;
    while (l <= r) {
        int mid = (l + r) / 2;
 
        assert(mid > res);
        std::set<int> just_added;
        for (int i = 0; i < n; ++i) if (!insides.count(i)) {
            add(i);
            if (press_button() > mid) remove(i);
            else just_added.insert(i);
        }
        int total = SZ(insides);
 
        if (total >= unique_vals * mid) {
            res = mid;
            l = mid + 1;
        } else {
            r = mid - 1;
            for (int i : just_added) {
                remove(i);
            }
        }
    }
    return res;
}
*/
# Verdict Execution time Memory Grader output
1 Correct 1 ms 208 KB Output is correct
2 Correct 0 ms 208 KB Output is correct
3 Correct 0 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 8 ms 296 KB Output is correct
7 Correct 3 ms 208 KB Output is correct
8 Correct 6 ms 320 KB Output is correct
9 Correct 6 ms 304 KB Output is correct
10 Correct 5 ms 208 KB Output is correct
11 Correct 3 ms 208 KB Output is correct
12 Correct 3 ms 208 KB Output is correct
13 Correct 7 ms 208 KB Output is correct
14 Correct 5 ms 336 KB Output is correct
15 Correct 5 ms 316 KB Output is correct
16 Correct 5 ms 336 KB Output is correct
17 Correct 6 ms 324 KB Output is correct
18 Correct 6 ms 208 KB Output is correct
19 Correct 5 ms 208 KB Output is correct
20 Correct 6 ms 304 KB Output is correct
21 Correct 2 ms 320 KB Output is correct
22 Correct 3 ms 208 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 208 KB Output is correct
2 Correct 0 ms 208 KB Output is correct
3 Correct 0 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 8 ms 296 KB Output is correct
7 Correct 3 ms 208 KB Output is correct
8 Correct 6 ms 320 KB Output is correct
9 Correct 6 ms 304 KB Output is correct
10 Correct 5 ms 208 KB Output is correct
11 Correct 3 ms 208 KB Output is correct
12 Correct 3 ms 208 KB Output is correct
13 Correct 7 ms 208 KB Output is correct
14 Correct 5 ms 336 KB Output is correct
15 Correct 5 ms 316 KB Output is correct
16 Correct 5 ms 336 KB Output is correct
17 Correct 6 ms 324 KB Output is correct
18 Correct 6 ms 208 KB Output is correct
19 Correct 5 ms 208 KB Output is correct
20 Correct 6 ms 304 KB Output is correct
21 Correct 2 ms 320 KB Output is correct
22 Correct 3 ms 208 KB Output is correct
23 Correct 34 ms 680 KB Output is correct
24 Correct 8 ms 336 KB Output is correct
25 Correct 32 ms 548 KB Output is correct
26 Correct 27 ms 404 KB Output is correct
27 Correct 24 ms 296 KB Output is correct
28 Correct 7 ms 312 KB Output is correct
29 Correct 27 ms 288 KB Output is correct
30 Correct 21 ms 372 KB Output is correct
31 Correct 27 ms 672 KB Output is correct
32 Correct 30 ms 520 KB Output is correct
33 Correct 23 ms 672 KB Output is correct
34 Correct 31 ms 548 KB Output is correct
35 Correct 32 ms 536 KB Output is correct
36 Correct 24 ms 496 KB Output is correct
37 Correct 33 ms 424 KB Output is correct
38 Correct 25 ms 372 KB Output is correct
39 Correct 14 ms 368 KB Output is correct
40 Correct 15 ms 308 KB Output is correct
41 Correct 9 ms 308 KB Output is correct
# 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 0 ms 208 KB Output is correct
5 Correct 1 ms 208 KB Output is correct
6 Correct 1 ms 208 KB Output is correct
7 Correct 46 ms 1312 KB Output is correct
8 Correct 13 ms 496 KB Output is correct
9 Correct 32 ms 856 KB Output is correct
10 Correct 50 ms 576 KB Output is correct
11 Correct 40 ms 424 KB Output is correct
12 Correct 33 ms 460 KB Output is correct
13 Correct 48 ms 412 KB Output is correct
14 Correct 57 ms 428 KB Output is correct
15 Correct 62 ms 1096 KB Output is correct
16 Correct 61 ms 1092 KB Output is correct
17 Correct 70 ms 1052 KB Output is correct
18 Correct 76 ms 840 KB Output is correct
19 Partially correct 45 ms 1056 KB Output is partially correct
20 Correct 71 ms 884 KB Output is correct
21 Correct 55 ms 676 KB Output is correct
22 Correct 59 ms 532 KB Output is correct
23 Correct 36 ms 420 KB Output is correct
24 Correct 48 ms 564 KB Output is correct
25 Correct 23 ms 560 KB Output is correct
26 Correct 19 ms 448 KB Output is correct
27 Correct 62 ms 724 KB Output is correct
28 Correct 62 ms 796 KB Output is correct
29 Partially correct 57 ms 936 KB Output is partially correct
30 Partially correct 56 ms 1056 KB Output is partially correct
31 Correct 55 ms 676 KB Output is correct
32 Correct 58 ms 688 KB Output is correct
33 Correct 49 ms 408 KB Output is correct
34 Correct 54 ms 412 KB Output is correct
35 Partially correct 68 ms 848 KB Output is partially correct
36 Partially correct 39 ms 832 KB Output is partially correct
37 Correct 61 ms 660 KB Output is correct
38 Correct 51 ms 668 KB Output is correct
39 Correct 26 ms 684 KB Output is correct
40 Correct 60 ms 780 KB Output is correct
41 Partially correct 54 ms 840 KB Output is partially correct
42 Partially correct 41 ms 840 KB Output is partially correct
43 Correct 18 ms 308 KB Output is correct
44 Correct 55 ms 288 KB Output is correct
45 Correct 69 ms 1188 KB Output is correct
46 Correct 30 ms 420 KB Output is correct
47 Correct 22 ms 468 KB Output is correct
48 Correct 33 ms 448 KB Output is correct