Submission #627571

# Submission time Handle Problem Language Result Execution time Memory
627571 2022-08-12T17:02:12 Z I_love_Hoang_Yen Rarest Insects (IOI22_insects) C++17
83.67 / 100
119 ms 552 KB
#include "insects.h"
#include "bits/stdc++.h"
#define SZ(s) ((int) s.size())
using namespace std;

#ifndef RR
#define DEBUG(X)
#endif

/* sub1
// Sub 1 {{{
int min_cardinality(int n) {
    std::vector<int> v_ids(n);
    std::iota(v_ids.begin(), v_ids.end(), 0);

    std::set<int> ids(v_ids.begin(), v_ids.end());
    int freq = 0, last_cnt = 0;

    while (true) {
        ++freq;
        std::set<int> new_ids;

        for (int id : ids) {
            move_inside(id);
            if (press_button() > freq) {
                move_outside(id);
                new_ids.insert(id);
            }
        }

        int unique_values = SZ(ids) - SZ(new_ids);
        if (unique_values < last_cnt) {
            return freq - 1;
        }
        last_cnt = unique_values;

        if (unique_values > SZ(new_ids)) {
            return freq;
        }

        ids = std::move(new_ids);
    }

    return 0;
}
// }}}
*/

int solve_small(int n, const std::set<int>& seeds) {
    std::set<int> used;
    int res = n;
    DEBUG(seeds);

    int n_unique = SZ(seeds);
    for (int turn = 0; turn + 1 < n_unique; ++turn) {
        std::set<int> cur;

        for (int i = 0; i < n; ++i) {
            if (!seeds.count(i) && !used.count(i)) {
                cur.insert(i);
                move_inside(i);

                if (press_button() != SZ(cur) + 1) {
                    move_outside(i);
                    cur.erase(i);
                }
            }
        }

        used.insert(cur.begin(), cur.end());
        res = std::min(res, SZ(cur) + 1);

        for (int i : cur) move_outside(i);
    }

    int remaining = n - SZ(used) - SZ(seeds) + 1;
    DEBUG(remaining);
    res = std::min(res, remaining);

    return res;
}

int min_cardinality(int n) {
    std::set<int> insides;
    int freq = 1;

    auto add = [&] (int i) {
        insides.insert(i);
        move_inside(i);
    };
    auto remove = [&] (int i) {
        insides.erase(i);
        move_outside(i);
    };

    for (int i = 0; i < n; ++i) {
        add(i);
        if (press_button() > 1) {
            remove(i);
        }
    }
    int unique_vals = SZ(insides);
    DEBUG(unique_vals);

    if (unique_vals <= 4) {
        return solve_small(n, insides);
    }

    int l = 2, r = n / unique_vals, res = 1;
    while (l <= r) {
        int mid = l + (r - l + 1) / 2;

        assert(mid != freq);
        if (mid > freq) {
            for (int i = 0; i < n; ++i) {
                if (!insides.count(i)) {
                    add(i);
                    if (press_button() > mid) {
                        remove(i);
                    }
                }
            }
        } else {
            auto ids = insides;
            for (int id : ids) remove(id);

            for (int i : ids) {
                add(i);
                if (press_button() > mid) {
                    remove(i);
                }
            }
        }
        freq = mid;
        int total = SZ(insides);

        if (total >= unique_vals * mid) {
            // all values have at least frequency = mid
            res = mid;
            l = mid + 1;
        } else {
            r = mid - 1;
        }
    }
    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 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 3 ms 208 KB Output is correct
8 Correct 5 ms 300 KB Output is correct
9 Correct 5 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 6 ms 208 KB Output is correct
13 Correct 6 ms 208 KB Output is correct
14 Correct 9 ms 300 KB Output is correct
15 Correct 6 ms 208 KB Output is correct
16 Correct 6 ms 208 KB Output is correct
17 Correct 5 ms 208 KB Output is correct
18 Correct 9 ms 296 KB Output is correct
19 Correct 7 ms 304 KB Output is correct
20 Correct 5 ms 208 KB Output is correct
21 Correct 4 ms 208 KB Output is correct
22 Correct 3 ms 304 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 1 ms 208 KB Output is correct
6 Correct 1 ms 208 KB Output is correct
7 Correct 3 ms 208 KB Output is correct
8 Correct 5 ms 300 KB Output is correct
9 Correct 5 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 6 ms 208 KB Output is correct
13 Correct 6 ms 208 KB Output is correct
14 Correct 9 ms 300 KB Output is correct
15 Correct 6 ms 208 KB Output is correct
16 Correct 6 ms 208 KB Output is correct
17 Correct 5 ms 208 KB Output is correct
18 Correct 9 ms 296 KB Output is correct
19 Correct 7 ms 304 KB Output is correct
20 Correct 5 ms 208 KB Output is correct
21 Correct 4 ms 208 KB Output is correct
22 Correct 3 ms 304 KB Output is correct
23 Correct 10 ms 208 KB Output is correct
24 Correct 10 ms 336 KB Output is correct
25 Correct 25 ms 292 KB Output is correct
26 Correct 33 ms 300 KB Output is correct
27 Correct 17 ms 276 KB Output is correct
28 Correct 12 ms 208 KB Output is correct
29 Correct 20 ms 312 KB Output is correct
30 Correct 29 ms 304 KB Output is correct
31 Correct 49 ms 328 KB Output is correct
32 Correct 52 ms 292 KB Output is correct
33 Correct 48 ms 432 KB Output is correct
34 Correct 32 ms 288 KB Output is correct
35 Correct 29 ms 296 KB Output is correct
36 Correct 40 ms 292 KB Output is correct
37 Correct 37 ms 300 KB Output is correct
38 Correct 19 ms 296 KB Output is correct
39 Correct 32 ms 304 KB Output is correct
40 Correct 12 ms 316 KB Output is correct
41 Correct 10 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 1 ms 208 KB Output is correct
7 Correct 10 ms 208 KB Output is correct
8 Correct 20 ms 336 KB Output is correct
9 Correct 49 ms 356 KB Output is correct
10 Partially correct 77 ms 332 KB Output is partially correct
11 Correct 32 ms 456 KB Output is correct
12 Correct 20 ms 440 KB Output is correct
13 Correct 26 ms 312 KB Output is correct
14 Partially correct 65 ms 292 KB Output is partially correct
15 Partially correct 119 ms 508 KB Output is partially correct
16 Partially correct 84 ms 456 KB Output is partially correct
17 Partially correct 114 ms 540 KB Output is partially correct
18 Partially correct 94 ms 448 KB Output is partially correct
19 Partially correct 67 ms 420 KB Output is partially correct
20 Partially correct 109 ms 440 KB Output is partially correct
21 Partially correct 102 ms 412 KB Output is partially correct
22 Partially correct 72 ms 376 KB Output is partially correct
23 Partially correct 49 ms 340 KB Output is partially correct
24 Correct 50 ms 468 KB Output is correct
25 Correct 32 ms 316 KB Output is correct
26 Correct 19 ms 436 KB Output is correct
27 Correct 49 ms 296 KB Output is correct
28 Correct 47 ms 328 KB Output is correct
29 Correct 56 ms 292 KB Output is correct
30 Correct 45 ms 292 KB Output is correct
31 Partially correct 58 ms 324 KB Output is partially correct
32 Partially correct 75 ms 296 KB Output is partially correct
33 Partially correct 56 ms 328 KB Output is partially correct
34 Partially correct 66 ms 428 KB Output is partially correct
35 Correct 56 ms 356 KB Output is correct
36 Correct 44 ms 328 KB Output is correct
37 Partially correct 102 ms 412 KB Output is partially correct
38 Partially correct 86 ms 428 KB Output is partially correct
39 Correct 51 ms 352 KB Output is correct
40 Correct 70 ms 288 KB Output is correct
41 Correct 47 ms 300 KB Output is correct
42 Correct 50 ms 292 KB Output is correct
43 Correct 11 ms 208 KB Output is correct
44 Correct 42 ms 292 KB Output is correct
45 Correct 28 ms 552 KB Output is correct
46 Correct 24 ms 304 KB Output is correct
47 Correct 24 ms 328 KB Output is correct
48 Correct 26 ms 208 KB Output is correct