Submission #948045

# Submission time Handle Problem Language Result Execution time Memory
948045 2024-03-17T14:06:05 Z Nhoksocqt1 Rarest Insects (IOI22_insects) C++17
74.46 / 100
55 ms 936 KB
#ifndef Nhoksocqt1
    #include "insects.h"
#endif // Nhoksocqt1
#include<bits/stdc++.h>
using namespace std;

#define inf 0x3f3f3f3f
#define sz(x) int((x).size())
#define fi first
#define se second
typedef long long ll;
typedef pair<int, int> ii;

template<class X, class Y>
	inline bool maximize(X &x, const Y &y) {return (x < y ? x = y, 1 : 0);}
template<class X, class Y>
	inline bool minimize(X &x, const Y &y) {return (x > y ? x = y, 1 : 0);}

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int Random(int l, int r) {
    return uniform_int_distribution<int>(l, r)(rng);
}

const bool isMultiTest = 0;
const int MAXN = 2003;

struct Jury {
    set<int> Set;
    multiset<int> mCnt;
    vector<int> type;
    map<int, int> Map;

    void init(int _N) {
        type.resize(_N);
        for (int i = 0; i < _N; ++i) {
            cin >> type[i];
            type[i] = Random(1, 100); cout << type[i] << " \n"[i + 1 == _N];
        }
    }

    void move_inside(int i) {
        if(Set.find(i) == Set.end()) {
            int &Mapt(Map[type[i]]);
            if(Mapt > 0)
                mCnt.erase(mCnt.find(Mapt));

            mCnt.insert(++Mapt);
            Set.insert(i);
        }
    }

    void move_outside(int i) {
        if(Set.find(i) != Set.end()) {
            int &Mapt(Map[type[i]]);
            mCnt.erase(mCnt.find(Mapt));
            if(--Mapt > 0)
                mCnt.insert(Mapt);

            Set.erase(i);
        }
    }

    int press_button(void) {
        return *mCnt.rbegin();
    }

    int answer(void) {
        int ans(sz(type));
        for (int i = 0; i < sz(type); ++i) {
            int cnt(0);
            for (int j = 0; j < sz(type); ++j)
                cnt += (type[i] == type[j]);

            ans = min(ans, cnt);
        }

        return ans;
    }

} jury;

vector<int> id;
int size_in_machine, nArr;
bool dx[MAXN], dx2[MAXN], is1[MAXN];

int cnt_move_inside, cnt_move_outside, cnt_press_button;
void ask_move_inside(int i) {
    dx[i] = 1, i = id[i];
    ++size_in_machine;
    ++cnt_move_inside;
    #ifdef Nhoksocqt1
        jury.move_inside(i);
    #else
        move_inside(i);
    #endif // Nhoksocqt1
}

void ask_move_outside(int i) {
    dx[i] = 0, i = id[i];
    --size_in_machine;
    ++cnt_move_outside;
    #ifdef Nhoksocqt1
        jury.move_outside(i);
    #else
        move_outside(i);
    #endif // Nhoksocqt1
}

int ask_press_button(void) {
    ++cnt_press_button;
    #ifdef Nhoksocqt1
        return jury.press_button();
    #else
        return press_button();
    #endif // Nhoksocqt1
}

bool check(int x, int k) {
    for (int i = 0; i < nArr; ++i) {
        if(dx[i] || dx2[i])
            continue;

        ask_move_inside(i);
        if(size_in_machine > x && ask_press_button() > x)
            ask_move_outside(i);
    }

    int res = size_in_machine;
    if(res / x == k)
        return true;

    for (int i = 0; i < nArr; ++i) {
        if(!dx[i])
            dx2[i] = 1;

        if(dx[i] && !is1[i])
            ask_move_outside(i);
    }

    return false;
}

int min_cardinality(int _N) {
    nArr = _N;

    id.clear();
    for (int i = 0; i < nArr; ++i)
        id.push_back(i);

    shuffle(id.begin(), id.end(), rng);
    size_in_machine = 0;

    vector<int> uniqueType;
    for (int i = 0; i < nArr; ++i) {
        ask_move_inside(i);
        uniqueType.push_back(i);
        is1[i] = 1;

        if(size_in_machine > 1 && ask_press_button() > 1) {
            ask_move_outside(i);
            uniqueType.pop_back();
            is1[i] = 0;
        }
    }

    for (int i = 0; i < nArr; ++i) {
        if(!dx[i])
            ask_move_inside(i);
    }

    int max_cardinality = ask_press_button();
    if(max_cardinality == nArr)
        return max_cardinality;

    if(sz(uniqueType) == 2)
        return nArr - max_cardinality;

    if(sz(uniqueType) == nArr || nArr - max_cardinality < 2 * sz(uniqueType))
        return 1;

    for (int i = 0; i < nArr; ++i) {
        if(dx[i] && !is1[i])
            ask_move_outside(i);
    }

    int l(2), r = min(max_cardinality, nArr - max_cardinality - sz(uniqueType) + 2), ans(1);
    r = min(r, (nArr - max_cardinality) / (sz(uniqueType) - 1));

    while(l <= r) {
        int mid = (l + r) >> 1;
        if(check(mid, sz(uniqueType))) {
            ans = mid;
            l = mid + 1;
        } else {
            r = mid - 1;
        }
    }

    return ans;
}

#ifdef Nhoksocqt1

int main(void) {
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);

    #define TASK "insects"
    if(fopen(TASK".inp", "r")) {
        freopen(TASK".inp", "r", stdin);
        freopen(TASK".out", "w", stdout);
    }

    int N;
    cin >> N;
    jury.init(N);

    int ans = min_cardinality(N);
    cout << "ANSWER: " << ans << '\n';
    cout << "JURY ANSWER: " << jury.answer() << '\n';
    cout << "CNT MOVE INSIDE: " << cnt_move_inside << '\n';
    cout << "CNT MOVE OUTSIDE: " << cnt_move_outside << '\n';
    cout << "CNT PRESS BUTTON: " << cnt_press_button << '\n';

    cnt_move_inside = cnt_move_outside = cnt_press_button = 0;

    return 0;
}

#endif // Nhoksocqt1
# 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 1 ms 344 KB Output is correct
4 Correct 1 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 1 ms 344 KB Output is correct
8 Correct 2 ms 344 KB Output is correct
9 Correct 3 ms 344 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 2 ms 344 KB Output is correct
13 Correct 2 ms 344 KB Output is correct
14 Correct 4 ms 344 KB Output is correct
15 Correct 2 ms 344 KB Output is correct
16 Correct 3 ms 344 KB Output is correct
17 Correct 4 ms 344 KB Output is correct
18 Correct 2 ms 344 KB Output is correct
19 Correct 3 ms 344 KB Output is correct
20 Correct 2 ms 344 KB Output is correct
21 Correct 1 ms 344 KB Output is correct
22 Correct 1 ms 344 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 1 ms 344 KB Output is correct
4 Correct 1 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 1 ms 344 KB Output is correct
8 Correct 2 ms 344 KB Output is correct
9 Correct 3 ms 344 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 2 ms 344 KB Output is correct
13 Correct 2 ms 344 KB Output is correct
14 Correct 4 ms 344 KB Output is correct
15 Correct 2 ms 344 KB Output is correct
16 Correct 3 ms 344 KB Output is correct
17 Correct 4 ms 344 KB Output is correct
18 Correct 2 ms 344 KB Output is correct
19 Correct 3 ms 344 KB Output is correct
20 Correct 2 ms 344 KB Output is correct
21 Correct 1 ms 344 KB Output is correct
22 Correct 1 ms 344 KB Output is correct
23 Correct 4 ms 456 KB Output is correct
24 Correct 4 ms 600 KB Output is correct
25 Correct 15 ms 456 KB Output is correct
26 Correct 21 ms 600 KB Output is correct
27 Correct 5 ms 456 KB Output is correct
28 Correct 6 ms 344 KB Output is correct
29 Correct 13 ms 596 KB Output is correct
30 Correct 16 ms 344 KB Output is correct
31 Correct 23 ms 680 KB Output is correct
32 Correct 22 ms 684 KB Output is correct
33 Correct 21 ms 708 KB Output is correct
34 Correct 16 ms 840 KB Output is correct
35 Correct 21 ms 680 KB Output is correct
36 Correct 14 ms 600 KB Output is correct
37 Correct 16 ms 680 KB Output is correct
38 Correct 13 ms 600 KB Output is correct
39 Correct 8 ms 600 KB Output is correct
40 Correct 6 ms 600 KB Output is correct
41 Correct 5 ms 600 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 1 ms 340 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Correct 0 ms 344 KB Output is correct
7 Correct 9 ms 436 KB Output is correct
8 Correct 9 ms 600 KB Output is correct
9 Partially correct 39 ms 700 KB Output is partially correct
10 Partially correct 45 ms 684 KB Output is partially correct
11 Correct 9 ms 460 KB Output is correct
12 Correct 9 ms 344 KB Output is correct
13 Partially correct 24 ms 448 KB Output is partially correct
14 Partially correct 26 ms 596 KB Output is partially correct
15 Partially correct 42 ms 440 KB Output is partially correct
16 Partially correct 35 ms 436 KB Output is partially correct
17 Partially correct 46 ms 456 KB Output is partially correct
18 Partially correct 42 ms 664 KB Output is partially correct
19 Partially correct 55 ms 680 KB Output is partially correct
20 Partially correct 40 ms 440 KB Output is partially correct
21 Partially correct 38 ms 912 KB Output is partially correct
22 Partially correct 38 ms 904 KB Output is partially correct
23 Partially correct 23 ms 664 KB Output is partially correct
24 Correct 19 ms 452 KB Output is correct
25 Correct 15 ms 856 KB Output is correct
26 Correct 9 ms 600 KB Output is correct
27 Partially correct 25 ms 436 KB Output is partially correct
28 Partially correct 32 ms 680 KB Output is partially correct
29 Partially correct 32 ms 592 KB Output is partially correct
30 Partially correct 31 ms 448 KB Output is partially correct
31 Partially correct 43 ms 936 KB Output is partially correct
32 Partially correct 37 ms 676 KB Output is partially correct
33 Partially correct 34 ms 700 KB Output is partially correct
34 Partially correct 28 ms 448 KB Output is partially correct
35 Partially correct 26 ms 432 KB Output is partially correct
36 Partially correct 29 ms 928 KB Output is partially correct
37 Partially correct 42 ms 600 KB Output is partially correct
38 Partially correct 45 ms 688 KB Output is partially correct
39 Partially correct 22 ms 700 KB Output is partially correct
40 Partially correct 26 ms 600 KB Output is partially correct
41 Partially correct 27 ms 448 KB Output is partially correct
42 Partially correct 32 ms 456 KB Output is partially correct
43 Partially correct 6 ms 600 KB Output is partially correct
44 Partially correct 24 ms 600 KB Output is partially correct
45 Correct 9 ms 600 KB Output is correct
46 Correct 8 ms 716 KB Output is correct
47 Correct 10 ms 712 KB Output is correct
48 Correct 13 ms 468 KB Output is correct