Submission #726829

#TimeUsernameProblemLanguageResultExecution timeMemory
726829aykhnRarest Insects (IOI22_insects)C++17
100 / 100
79 ms412 KiB
#include <bits/stdc++.h>
#include "insects.h"
 
using namespace std;
 
typedef long long ll;
 
void mn(vector<int> &v)
{
    random_shuffle(v.begin(), v.end());
}
 
#define OPT ios_base::sync_with_stdio(0); \
            cin.tie(0); \
            cout.tie(0)
#define pii pair<int,int>
#define pll pair<ll,ll>
#define all(v) v.begin(), v.end()
#define mpr make_pair
#define pb push_back
#define ts to_string
#define fi first
#define se second
#define inf 0x3F3F3F3F
#define infll 0x3F3F3F3F3F3F3F3FLL
#define bpc __builtin_popcount
#define print(v) for(int i = 0; i < v.size(); i++) cout << v[i] << " "; cout<<endl;
/*
vector<int> type;
set<pii> s;
 
void move_inside(int i)
{
    auto it = s.lower_bound(mpr(type[i], -1));
 
    if (it == s.end() || (*it).fi != type[i]) s.insert(mpr(type[i], 1));
    else
    {
        pii y = *it;
        s.erase(it);
        s.insert(mpr(type[i], y.se + 1));
    }
}
 
void move_outside(int i)
{
    auto it = s.lower_bound(mpr(type[i], -1));
 
    if (it == s.end() || (*it).fi != type[i]) return;
    pii y = *it;
    s.erase(it);
    if (y.se > 1) s.insert(mpr(type[i], y.se - 1));
}
 
 
 
int press_button()
{
    int mx = 0;
    for (pii x : s)
    {
        mx = max(mx, x.se);
    }
    return mx;
}
int it1, it2, it3;*/
int min_cardinality(int n)
{
 
    int sz = 0;
 
    vector<int> out;
 
    for (int i = 0; i < n; i++)
    {
        move_inside(i);
        int x = press_button();
        if (x > 1)
        {
            move_outside(i);
            out.pb(i);
        }
        else sz++;
    }
 
    if (sz == 1) return n;
    else if (sz == n) return 1;
 
    int best = 1;
    int rn = sz;
    int l = 2;
    int r = n/sz;
 
    mn(out);
    while (l <= r)
    {
        int mid = (l + r) >> 1;
        vector<int> temp;
        vector<int> nout;

 
        for (int i = 0; i < out.size(); i++)
        {
            if (rn == mid * sz)
            { // fill out to next out (nout)
                for (int j = i; j < out.size(); j++) nout.pb(out[j]);
                break;
            }
            move_inside(out[i]);
 
            int x = press_button();
 
            if (x > mid)
            {
                nout.pb(out[i]);
                move_outside(out[i]);
            }
            else
            {
                rn++;
                temp.pb(out[i]);
            }
        }
 // if true, previous elements not needed
 // current elements not needed ?? yes
 // (*) extras not needed (nout) if min is already lower <- important
 
        if (rn == mid * sz)
        {
            best = max(best, mid);
            l = mid + 1;
            out = nout;
        }
        else
        {
            r = mid - 1;
            rn -= (int)(temp.size());
            for (int x : temp)
            {
                move_outside(x);
            }
            out = temp; // (*) <-
        }
    }
 
    return best;
}
 
 
/*
int main()
{
    int n;
    cin >> n;
 
    type.assign(n, 0);
 
    for (int i = 0; i < n; i++)
    {
        cin >> type[i];
    }
 
    cout << min_cardinality(n) << endl;
 
}
*/

Compilation message (stderr)

insects.cpp: In function 'int min_cardinality(int)':
insects.cpp:102:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  102 |         for (int i = 0; i < out.size(); i++)
      |                         ~~^~~~~~~~~~~~
insects.cpp:106:35: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  106 |                 for (int j = i; j < out.size(); j++) nout.pb(out[j]);
      |                                 ~~^~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...