Submission #1335754

#TimeUsernameProblemLanguageResultExecution timeMemory
1335754yhkhooRarest Insects (IOI22_insects)C++17
99.89 / 100
17 ms456 KiB
#include "insects.h"
#include <bits/stdc++.h>
using namespace std;

#define qry() press_button()
#define vi basic_string<int>
#define cnt count(cur.begin(), cur.end(), 1)
vector<int> o;

void in(int x){
    move_inside(o[x]);
}

void out(int x){
    move_outside(o[x]);
}

int min_cardinality(int n) {
    mt19937 rnd(676767);
    o.clear();
    o.resize(n);
    for(int i=0; i<n; i++){
        o[i] = i;
    }
    shuffle(o.begin(), o.end(), rnd);

    vi f, nf;
    for(int i=0; i<n; i++){
        in(i);
        if(qry() == 1){
            f += i;
        }
        else{
            out(i);
            nf += i;
        }
    }
    int l = 1, r = n / f.size(), m;
    vector<bool> cur(n), good(n), bad(n);
    bool down = 0;
    while(l < r){
        m = (l+r+1)/2;
        for(auto i: nf){
            if(bad[i]) continue;
            if(down){
                if((!good[i])){
                    in(i);
                    if(qry() > m){
                        out(i);
                        cur[i] = 0;
                    }
                    else{
                        cur[i] = 1;
                    }
                }
            }
            else{
                if(!good[i]){
                    in(i);
                    if(qry() > m){
                        out(i);
                        cur[i] = 0;
                    }
                    else{
                        cur[i] = 1;
                    }
                }
            }
        }
        if(cnt < (m-1) * f.size()){ // m too big
            r = m-1;
            down = 1;
            for(auto i: nf){
                if(!cur[i]){
                    bad[i] = 1;
                }
                if(cur[i] && (!good[i])){
                    out(i);
                    cur[i] = 0;
                }
            }
        }
        else{ // m ok
            l = m;
            down = 0;
            for(auto i: nf){
                if(cur[i]) good[i] = 1;
            }
        }
    }
    return l;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...