Submission #971108

# Submission time Handle Problem Language Result Execution time Memory
971108 2024-04-28T01:03:37 Z maksim1744 Rarest Insects (IOI22_insects) C++17
100 / 100
43 ms 11772 KB
/*
    author:  Maksim1744
    created: 28.04.2024 03:07:13
*/

#include "bits/stdc++.h"

using namespace std;

using ll = long long;
using ld = long double;

#define mp   make_pair
#define pb   push_back
#define eb   emplace_back

#define sum(a)     ( accumulate ((a).begin(), (a).end(), 0ll))
#define mine(a)    (*min_element((a).begin(), (a).end()))
#define maxe(a)    (*max_element((a).begin(), (a).end()))
#define mini(a)    ( min_element((a).begin(), (a).end()) - (a).begin())
#define maxi(a)    ( max_element((a).begin(), (a).end()) - (a).begin())
#define lowb(a, x) ( lower_bound((a).begin(), (a).end(), (x)) - (a).begin())
#define uppb(a, x) ( upper_bound((a).begin(), (a).end(), (x)) - (a).begin())

template<typename T>             vector<T>& operator--            (vector<T> &v){for (auto& i : v) --i;            return  v;}
template<typename T>             vector<T>& operator++            (vector<T> &v){for (auto& i : v) ++i;            return  v;}
template<typename T>             istream& operator>>(istream& is,  vector<T> &v){for (auto& i : v) is >> i;        return is;}
template<typename T>             ostream& operator<<(ostream& os,  vector<T>  v){for (auto& i : v) os << i << ' '; return os;}
template<typename T, typename U> pair<T,U>& operator--           (pair<T, U> &p){--p.first; --p.second;            return  p;}
template<typename T, typename U> pair<T,U>& operator++           (pair<T, U> &p){++p.first; ++p.second;            return  p;}
template<typename T, typename U> istream& operator>>(istream& is, pair<T, U> &p){is >> p.first >> p.second;        return is;}
template<typename T, typename U> ostream& operator<<(ostream& os, pair<T, U>  p){os << p.first << ' ' << p.second; return os;}
template<typename T, typename U> pair<T,U> operator-(pair<T,U> a, pair<T,U> b){return mp(a.first-b.first, a.second-b.second);}
template<typename T, typename U> pair<T,U> operator+(pair<T,U> a, pair<T,U> b){return mp(a.first+b.first, a.second+b.second);}
template<typename T, typename U> void umin(T& a, U b){if (a > b) a = b;}
template<typename T, typename U> void umax(T& a, U b){if (a < b) a = b;}

#ifdef HOME
#define SHOW_COLORS
#include "/mnt/c/Libs/tools/print.cpp"
#else
#define show(...) void(0)
#define debugf(fun)   fun
#define debugv(var)   var
#define mclock    void(0)
#define shows     void(0)
#define debug  if (false)
#define OSTREAM(...)    ;
#define OSTREAM0(...)   ;
#endif

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

#ifdef HOUSE
set<int> cur;
vector<int> col;
array<int, 3> que;

void move_inside(int i) {
    ++que[0];
    cur.insert(i);
}

void move_outside(int i) {
    ++que[1];
    cur.erase(i);
}

int press_button() {
    ++que[2];
    map<int, int> u;
    for (int i : cur)
        u[col[i]]++;
    int ans = 0;
    for (auto [a, b] : u)
        ans = max(ans, b);
    return ans;
}
#endif

int min_cardinality(int n) {
    mt19937_64 rng(11112);

    set<int> cur;
    map<string, int> mem;
    string scur(n, '0');

    pair<int, int> pot = {0, 0};

    auto add = [&](int i) {
        if (cur.count(i)) return;
        cur.insert(i);
        scur[i] ^= '0' ^ '1';
        move_inside(i);
        pot.second++;
        pot.second = min(pot.second, (int)cur.size());
    };
    auto rem = [&](int i) {
        if (!cur.count(i)) return;
        cur.erase(i);
        scur[i] ^= '0' ^ '1';
        move_outside(i);
        pot.first--;
        pot.first = max(pot.first, cur.empty() ? 0 : 1);
    };
    auto ask_int = [&]() -> int {
        if (cur.size() <= 1) return cur.size();
        if (mem.count(scur)) return mem[scur];
        return mem[scur] = press_button();
        // return press_button();
    };
    auto ask = [&]() -> int {
        int e = ask_int();
        pot = {e, e};
        return e;
    };
    auto clear = [&]() {
        while (!cur.empty())
            rem(*cur.begin());
    };

    for (int i = 0; i < n; ++i) {
        add(i);
        if (ask() > 1) {
            rem(i);
        }
    }
    if (cur.size() == n) return 1;
    int dif = cur.size();
    set<int> left;
    for (int i = 0; i < n; ++i) {
        left.insert(i);
    }

    int L = 1, R = 1;
    while (R * cur.size() <= n) ++R;
    bool st = true;
    int m = 0;
    for (int i : cur)
        left.erase(i);
    clear();
    m = 1;
    while (R - L > 1) {
        int C = (L + R + st) / 2;
        st = false;
        while (C <= L) ++C;
        while (C >= R) --C;
        int cleft = left.size();
        set<int> checked;
        vector<int> vleft(left.begin(), left.end());
        shuffle(vleft.begin(), vleft.end(), rng);
        for (int i : vleft) {
            if (cur.size() == (C - m) * dif) {
                break;
            }
            if (cur.size() + cleft < (C - m) * dif) {
                for (int u : left)
                    if (!checked.count(u))
                        add(u);
                        // cur.insert(u);
                break;
            }
            add(i);
            checked.insert(i);
            if (pot.second > C - m && ask() > C - m)
                rem(i);
            --cleft;
        }
        if (cur.size() == (C - m) * dif) {
            for (int i : cur)
                left.erase(i);
            clear();
            L = C;
            m = L;
        } else {
            // for (int i = 0; i < n; ++i) {
            //     if (!cur.count(i))
            //         left.erase(i);
            // }
            left = cur;
            clear();
            R = C;
        }
    }
    return L;
}

#ifdef HOUSE
// mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
mt19937_64 rng(3294793);
ll rnd (ll l, ll r) { return (ll)(rng() % (r - l + 1)) + l; }
ll rnd (ll r)       { return rng() % r; }
ll rnd ()           { return rng(); }
ld rndf()           { return (ld)rng() / (ld)ULLONG_MAX; }
ld rndf(ld l, ld r) { return rndf() * (r - l) + l; }

int main() {
    ios_base::sync_with_stdio(false); cin.tie(NULL);

    int mxq = 0;
    int T = 100000;
    int n = 2000;
    for (int t = 1; t <= T; ++t) {
        rng.seed(t);
        int C = rnd(1, 200);
        // int C = rnd(1, n);
        cur.clear();
        col.resize(n);
        for (int i = 0; i < n; ++i)
            col[i] = rnd(C);
        que.fill(0);

        int ans = 1e9;
        map<int, int> m;
        for (int i : col)
            m[i]++;
        for (auto [a, b] : m)
            ans = min(ans, b);
        int x = min_cardinality(n);
        // cerr <<  "ans=" << ans << " out=" << x << endl;
        // cerr << "que: ";
        // for (int x : que)
        //     cerr << x << ' ';
        // cerr << endl;
        // cerr << fixed << setprecision(3) << (ld)maxe(que) / n << endl;
        show(m);
        assert(ans == x);
        // assert(maxe(que) <= n * 3);
        mxq = max(mxq, maxe(que));
        cerr << "t=" << t << ' ' << "mxq: " << maxe(que) << " " << (ld)mxq / n << ' ' << "C=" << C << endl;
        if (mxq > n * 3) {
            cerr <<  "ans=" << ans << " out=" << x << endl;
            cerr << "que: ";
            for (int x : que)
                cerr << x << ' ';
            cerr << endl;
        }
        assert(mxq <= n * 3);
    }

    return 0;
}
#endif

Compilation message

insects.cpp: In function 'int min_cardinality(int)':
insects.cpp:130:20: warning: comparison of integer expressions of different signedness: 'std::set<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  130 |     if (cur.size() == n) return 1;
      |         ~~~~~~~~~~~^~~~
insects.cpp:138:27: warning: comparison of integer expressions of different signedness: 'std::set<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  138 |     while (R * cur.size() <= n) ++R;
      |            ~~~~~~~~~~~~~~~^~~~
insects.cpp:155:28: warning: comparison of integer expressions of different signedness: 'std::set<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  155 |             if (cur.size() == (C - m) * dif) {
      |                 ~~~~~~~~~~~^~~~~~~~~~~~~~~~
insects.cpp:158:36: warning: comparison of integer expressions of different signedness: 'std::set<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  158 |             if (cur.size() + cleft < (C - m) * dif) {
      |                 ~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~
insects.cpp:171:24: warning: comparison of integer expressions of different signedness: 'std::set<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  171 |         if (cur.size() == (C - m) * dif) {
      |             ~~~~~~~~~~~^~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Correct 2 ms 856 KB Output is correct
7 Correct 1 ms 856 KB Output is correct
8 Correct 3 ms 888 KB Output is correct
9 Correct 3 ms 476 KB Output is correct
10 Correct 3 ms 740 KB Output is correct
11 Correct 1 ms 728 KB Output is correct
12 Correct 3 ms 856 KB Output is correct
13 Correct 3 ms 600 KB Output is correct
14 Correct 3 ms 600 KB Output is correct
15 Correct 2 ms 600 KB Output is correct
16 Correct 3 ms 852 KB Output is correct
17 Correct 3 ms 736 KB Output is correct
18 Correct 3 ms 1112 KB Output is correct
19 Correct 3 ms 1112 KB Output is correct
20 Correct 2 ms 600 KB Output is correct
21 Correct 2 ms 748 KB Output is correct
22 Correct 2 ms 856 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Correct 2 ms 856 KB Output is correct
7 Correct 1 ms 856 KB Output is correct
8 Correct 3 ms 888 KB Output is correct
9 Correct 3 ms 476 KB Output is correct
10 Correct 3 ms 740 KB Output is correct
11 Correct 1 ms 728 KB Output is correct
12 Correct 3 ms 856 KB Output is correct
13 Correct 3 ms 600 KB Output is correct
14 Correct 3 ms 600 KB Output is correct
15 Correct 2 ms 600 KB Output is correct
16 Correct 3 ms 852 KB Output is correct
17 Correct 3 ms 736 KB Output is correct
18 Correct 3 ms 1112 KB Output is correct
19 Correct 3 ms 1112 KB Output is correct
20 Correct 2 ms 600 KB Output is correct
21 Correct 2 ms 748 KB Output is correct
22 Correct 2 ms 856 KB Output is correct
23 Correct 8 ms 1548 KB Output is correct
24 Correct 6 ms 2036 KB Output is correct
25 Correct 12 ms 3004 KB Output is correct
26 Correct 17 ms 3664 KB Output is correct
27 Correct 14 ms 2952 KB Output is correct
28 Correct 9 ms 1624 KB Output is correct
29 Correct 15 ms 3380 KB Output is correct
30 Correct 18 ms 3704 KB Output is correct
31 Correct 10 ms 1900 KB Output is correct
32 Correct 11 ms 2636 KB Output is correct
33 Correct 14 ms 2864 KB Output is correct
34 Correct 11 ms 2644 KB Output is correct
35 Correct 13 ms 2904 KB Output is correct
36 Correct 15 ms 3384 KB Output is correct
37 Correct 14 ms 2900 KB Output is correct
38 Correct 16 ms 3656 KB Output is correct
39 Correct 16 ms 3984 KB Output is correct
40 Correct 9 ms 2392 KB Output is correct
41 Correct 6 ms 1880 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 15 ms 4820 KB Output is correct
8 Correct 13 ms 4952 KB Output is correct
9 Correct 30 ms 8908 KB Output is correct
10 Correct 41 ms 11112 KB Output is correct
11 Correct 31 ms 8864 KB Output is correct
12 Correct 15 ms 5440 KB Output is correct
13 Correct 32 ms 9856 KB Output is correct
14 Correct 39 ms 10736 KB Output is correct
15 Correct 20 ms 6380 KB Output is correct
16 Correct 22 ms 7032 KB Output is correct
17 Correct 25 ms 7184 KB Output is correct
18 Correct 27 ms 7820 KB Output is correct
19 Correct 26 ms 7956 KB Output is correct
20 Correct 29 ms 8812 KB Output is correct
21 Correct 40 ms 11172 KB Output is correct
22 Correct 38 ms 11724 KB Output is correct
23 Correct 40 ms 11676 KB Output is correct
24 Correct 33 ms 10404 KB Output is correct
25 Correct 21 ms 6772 KB Output is correct
26 Correct 14 ms 5200 KB Output is correct
27 Correct 29 ms 9288 KB Output is correct
28 Correct 29 ms 9332 KB Output is correct
29 Correct 26 ms 8612 KB Output is correct
30 Correct 28 ms 7784 KB Output is correct
31 Correct 39 ms 11048 KB Output is correct
32 Correct 38 ms 10640 KB Output is correct
33 Correct 43 ms 11772 KB Output is correct
34 Correct 42 ms 11616 KB Output is correct
35 Correct 29 ms 9008 KB Output is correct
36 Correct 33 ms 8960 KB Output is correct
37 Correct 40 ms 10856 KB Output is correct
38 Correct 36 ms 11100 KB Output is correct
39 Correct 36 ms 10012 KB Output is correct
40 Correct 39 ms 10652 KB Output is correct
41 Correct 29 ms 10240 KB Output is correct
42 Correct 33 ms 9432 KB Output is correct
43 Correct 8 ms 1240 KB Output is correct
44 Correct 31 ms 8652 KB Output is correct
45 Correct 18 ms 4816 KB Output is correct
46 Correct 13 ms 4944 KB Output is correct
47 Correct 17 ms 4440 KB Output is correct
48 Correct 15 ms 4992 KB Output is correct