Submission #628760

# Submission time Handle Problem Language Result Execution time Memory
628760 2022-08-13T16:39:45 Z tranxuanbach Rarest Insects (IOI22_insects) C++17
99.89 / 100
76 ms 1320 KB
#include "insects.h"
#include "bits/stdc++.h"
 
#define SZ(s) ((int) s.size())
using namespace std;
 
#ifndef RR
#define DEBUG(X)
#endif

mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
mt19937_64 rngll(chrono::high_resolution_clock::now().time_since_epoch().count());
using randint_t = uniform_int_distribution<int>;
using randll_t = uniform_int_distribution<long long>;
using randd_t = uniform_real_distribution<double>;
// return x with probability p, y with probability 1-p
template<class T>
T pick(T x, T y, double p = 0.5){
    assert(-0.0001 <= p && p <= 1.0001);
    return randd_t(0, 1)(rng) <= p ? x : y;
}
int randr(int l, int r){
    return randint_t(l, r)(rng);
}
 
/*
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::vector<int> perm(n); iota(perm.begin(), perm.end(), 0); shuffle(perm.begin(), perm.end(), rng);
    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(perm[i]); insides.insert(i); };
    // lambda function to remove one insect from machine
    auto remove = [&] (int i) { move_outside(perm[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;
            l = max(l, mid - (unique_vals * mid - total));
            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 0 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 6 ms 324 KB Output is correct
7 Correct 2 ms 320 KB Output is correct
8 Correct 6 ms 336 KB Output is correct
9 Correct 5 ms 208 KB Output is correct
10 Correct 5 ms 308 KB Output is correct
11 Correct 2 ms 308 KB Output is correct
12 Correct 5 ms 308 KB Output is correct
13 Correct 6 ms 208 KB Output is correct
14 Correct 6 ms 308 KB Output is correct
15 Correct 8 ms 332 KB Output is correct
16 Correct 6 ms 332 KB Output is correct
17 Correct 6 ms 336 KB Output is correct
18 Correct 6 ms 324 KB Output is correct
19 Correct 5 ms 336 KB Output is correct
20 Correct 3 ms 336 KB Output is correct
21 Correct 3 ms 308 KB Output is correct
22 Correct 3 ms 312 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 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 6 ms 324 KB Output is correct
7 Correct 2 ms 320 KB Output is correct
8 Correct 6 ms 336 KB Output is correct
9 Correct 5 ms 208 KB Output is correct
10 Correct 5 ms 308 KB Output is correct
11 Correct 2 ms 308 KB Output is correct
12 Correct 5 ms 308 KB Output is correct
13 Correct 6 ms 208 KB Output is correct
14 Correct 6 ms 308 KB Output is correct
15 Correct 8 ms 332 KB Output is correct
16 Correct 6 ms 332 KB Output is correct
17 Correct 6 ms 336 KB Output is correct
18 Correct 6 ms 324 KB Output is correct
19 Correct 5 ms 336 KB Output is correct
20 Correct 3 ms 336 KB Output is correct
21 Correct 3 ms 308 KB Output is correct
22 Correct 3 ms 312 KB Output is correct
23 Correct 25 ms 696 KB Output is correct
24 Correct 9 ms 332 KB Output is correct
25 Correct 30 ms 584 KB Output is correct
26 Correct 28 ms 384 KB Output is correct
27 Correct 18 ms 368 KB Output is correct
28 Correct 9 ms 320 KB Output is correct
29 Correct 24 ms 316 KB Output is correct
30 Correct 20 ms 388 KB Output is correct
31 Correct 20 ms 464 KB Output is correct
32 Correct 15 ms 576 KB Output is correct
33 Correct 27 ms 556 KB Output is correct
34 Correct 30 ms 536 KB Output is correct
35 Correct 24 ms 432 KB Output is correct
36 Correct 29 ms 440 KB Output is correct
37 Correct 29 ms 476 KB Output is correct
38 Correct 32 ms 308 KB Output is correct
39 Correct 24 ms 312 KB Output is correct
40 Correct 17 ms 328 KB Output is correct
41 Correct 10 ms 332 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 1 ms 208 KB Output is correct
7 Correct 64 ms 1304 KB Output is correct
8 Correct 20 ms 456 KB Output is correct
9 Correct 50 ms 952 KB Output is correct
10 Correct 47 ms 680 KB Output is correct
11 Correct 46 ms 456 KB Output is correct
12 Correct 22 ms 452 KB Output is correct
13 Correct 57 ms 552 KB Output is correct
14 Correct 29 ms 480 KB Output is correct
15 Correct 64 ms 1072 KB Output is correct
16 Correct 54 ms 948 KB Output is correct
17 Correct 67 ms 948 KB Output is correct
18 Correct 59 ms 948 KB Output is correct
19 Correct 62 ms 716 KB Output is correct
20 Correct 53 ms 820 KB Output is correct
21 Correct 68 ms 680 KB Output is correct
22 Correct 38 ms 556 KB Output is correct
23 Correct 44 ms 432 KB Output is correct
24 Correct 42 ms 564 KB Output is correct
25 Correct 27 ms 588 KB Output is correct
26 Correct 11 ms 492 KB Output is correct
27 Correct 52 ms 788 KB Output is correct
28 Correct 48 ms 788 KB Output is correct
29 Partially correct 66 ms 948 KB Output is partially correct
30 Partially correct 66 ms 948 KB Output is partially correct
31 Correct 64 ms 712 KB Output is correct
32 Correct 55 ms 676 KB Output is correct
33 Correct 56 ms 456 KB Output is correct
34 Correct 28 ms 444 KB Output is correct
35 Partially correct 68 ms 864 KB Output is partially correct
36 Partially correct 51 ms 852 KB Output is partially correct
37 Correct 70 ms 636 KB Output is correct
38 Correct 76 ms 608 KB Output is correct
39 Correct 65 ms 664 KB Output is correct
40 Correct 55 ms 672 KB Output is correct
41 Partially correct 64 ms 824 KB Output is partially correct
42 Partially correct 69 ms 864 KB Output is partially correct
43 Correct 16 ms 320 KB Output is correct
44 Correct 56 ms 408 KB Output is correct
45 Correct 56 ms 1320 KB Output is correct
46 Correct 29 ms 452 KB Output is correct
47 Correct 30 ms 432 KB Output is correct
48 Correct 36 ms 484 KB Output is correct