Submission #948035

# Submission time Handle Problem Language Result Execution time Memory
948035 2024-03-17T13:55:34 Z Nhoksocqt1 Rarest Insects (IOI22_insects) C++17
53.13 / 100
100 ms 1172 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, 10); 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];

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
}

int countSize(int x) {
    for (int i = 0; i < nArr; ++i) {
        ask_move_inside(i);
        if(size_in_machine > x && ask_press_button() > x)
            ask_move_outside(i);
    }

    int res = size_in_machine;
    for (int i = 0; i < nArr; ++i) {
        if(dx[i])
            ask_move_outside(i);
    }

    return res / x;
}

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);
        if(size_in_machine > 1 && ask_press_button() > 1) {
            ask_move_outside(i);
            uniqueType.pop_back();
        }
    }

    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;

    //cout << max_cardinality << ' ' << sz(uniqueType) << '\n';
    for (int i = 0; i < nArr; ++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(countSize(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 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 1 ms 344 KB Output is correct
7 Correct 1 ms 596 KB Output is correct
8 Correct 4 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 4 ms 344 KB Output is correct
13 Correct 4 ms 596 KB Output is correct
14 Correct 5 ms 344 KB Output is correct
15 Correct 5 ms 344 KB Output is correct
16 Correct 4 ms 344 KB Output is correct
17 Correct 4 ms 344 KB Output is correct
18 Correct 4 ms 344 KB Output is correct
19 Correct 4 ms 344 KB Output is correct
20 Correct 2 ms 344 KB Output is correct
21 Correct 2 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 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 1 ms 344 KB Output is correct
7 Correct 1 ms 596 KB Output is correct
8 Correct 4 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 4 ms 344 KB Output is correct
13 Correct 4 ms 596 KB Output is correct
14 Correct 5 ms 344 KB Output is correct
15 Correct 5 ms 344 KB Output is correct
16 Correct 4 ms 344 KB Output is correct
17 Correct 4 ms 344 KB Output is correct
18 Correct 4 ms 344 KB Output is correct
19 Correct 4 ms 344 KB Output is correct
20 Correct 2 ms 344 KB Output is correct
21 Correct 2 ms 344 KB Output is correct
22 Correct 1 ms 344 KB Output is correct
23 Correct 4 ms 344 KB Output is correct
24 Correct 6 ms 600 KB Output is correct
25 Correct 28 ms 932 KB Output is correct
26 Correct 29 ms 704 KB Output is correct
27 Correct 4 ms 600 KB Output is correct
28 Correct 4 ms 344 KB Output is correct
29 Correct 42 ms 452 KB Output is correct
30 Correct 27 ms 452 KB Output is correct
31 Correct 42 ms 832 KB Output is correct
32 Correct 32 ms 844 KB Output is correct
33 Correct 38 ms 940 KB Output is correct
34 Correct 32 ms 1172 KB Output is correct
35 Correct 28 ms 920 KB Output is correct
36 Correct 33 ms 836 KB Output is correct
37 Correct 24 ms 680 KB Output is correct
38 Correct 17 ms 600 KB Output is correct
39 Correct 13 ms 444 KB Output is correct
40 Correct 9 ms 600 KB Output is correct
41 Correct 4 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 0 ms 352 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 10 ms 600 KB Output is correct
8 Correct 10 ms 600 KB Output is correct
9 Partially correct 70 ms 676 KB Output is partially correct
10 Partially correct 68 ms 684 KB Output is partially correct
11 Correct 10 ms 432 KB Output is correct
12 Correct 8 ms 692 KB Output is correct
13 Partially correct 100 ms 560 KB Output is partially correct
14 Partially correct 67 ms 592 KB Output is partially correct
15 Partially correct 78 ms 1160 KB Output is partially correct
16 Partially correct 87 ms 912 KB Output is partially correct
17 Partially correct 75 ms 932 KB Output is partially correct
18 Partially correct 87 ms 676 KB Output is partially correct
19 Partially correct 73 ms 900 KB Output is partially correct
20 Partially correct 78 ms 1164 KB Output is partially correct
21 Partially correct 61 ms 680 KB Output is partially correct
22 Partially correct 49 ms 692 KB Output is partially correct
23 Partially correct 41 ms 660 KB Output is partially correct
24 Partially correct 33 ms 600 KB Output is partially correct
25 Correct 24 ms 460 KB Output is correct
26 Correct 10 ms 716 KB Output is correct
27 Partially correct 60 ms 840 KB Output is partially correct
28 Partially correct 62 ms 908 KB Output is partially correct
29 Partially correct 77 ms 1156 KB Output is partially correct
30 Partially correct 68 ms 684 KB Output is partially correct
31 Partially correct 65 ms 680 KB Output is partially correct
32 Partially correct 50 ms 840 KB Output is partially correct
33 Partially correct 54 ms 592 KB Output is partially correct
34 Partially correct 47 ms 660 KB Output is partially correct
35 Partially correct 70 ms 796 KB Output is partially correct
36 Partially correct 63 ms 912 KB Output is partially correct
37 Partially correct 53 ms 444 KB Output is partially correct
38 Partially correct 57 ms 704 KB Output is partially correct
39 Partially correct 50 ms 432 KB Output is partially correct
40 Partially correct 50 ms 680 KB Output is partially correct
41 Partially correct 68 ms 676 KB Output is partially correct
42 Partially correct 70 ms 452 KB Output is partially correct
43 Partially correct 14 ms 452 KB Output is partially correct
44 Partially correct 57 ms 428 KB Output is partially correct
45 Correct 12 ms 432 KB Output is correct
46 Correct 9 ms 600 KB Output is correct
47 Correct 11 ms 456 KB Output is correct
48 Correct 11 ms 692 KB Output is correct