Submission #702710

#TimeUsernameProblemLanguageResultExecution timeMemory
702710boris_mihovRarest Insects (IOI22_insects)C++17
45.19 / 100
157 ms784 KiB
#include "insects.h"
#include <algorithm>
#include <iostream>
#include <numeric>
#include <vector>
#include <queue>
#include <set>

typedef long long llong;
const int MAXN = 2000 + 10;
const int INF  = 1e9;

int n;
int cnt;
int a[MAXN];
int perm[MAXN];
std::vector <int> diff;
std::vector <std::pair <int,int>> tree[4*MAXN];

inline int pressButton()
{
    return press_button();
}

inline void moveInside(int idx)
{
    move_inside(perm[idx]);
}

inline void moveOutside(int idx)
{
    move_outside(perm[idx]);
}

struct Range
{
    int l, r;
    int node;
};

std::queue <Range> q;
void divide()
{
    int ptrL = 0;
    int ptrR = diff.size() - 1;
    q.push({0, diff.size() - 1, 1});
    while (!q.empty())
    {
        auto [l, r, node] = q.front();
        q.pop();

        if (l == r)
        {
            for (const auto &[i, to] : tree[node])
            {
                a[i] = a[diff[l]];
            }

            continue;
        }

        int mid = (l + r) / 2;
        while (ptrL < l) moveOutside(diff[ptrL++]);
        while (ptrL > l) moveInside (diff[--ptrL]);
        while (ptrR < mid) moveInside (diff[++ptrR]);
        while (ptrR > mid) moveOutside(diff[ptrR--]);
    
        while (!tree[node].empty())
        {
            if (tree[node].back().second <= mid)
            {
                tree[2*node].push_back(tree[node].back());
                tree[node].pop_back();
                continue;
            }

            moveInside(tree[node].back().first);
            if (pressButton() != 1)
            {
                tree[2*node].push_back(tree[node].back());
            } else
            {
                tree[2*node + 1].push_back(tree[node].back());
            }

            moveOutside(tree[node].back().first);
            tree[node].pop_back();
        }

        if (tree[2*node + 1].size())
        {
            q.push({mid + 1, r, 2*node + 1});
        }

        if (tree[2*node].size())
        {
            q.push({l, mid, 2*node});
        }
    }
}

int min_cardinality(int N) 
{
    n = N;
    a[1] = ++cnt;
    diff.push_back(1);
    srand(79049059);
    std::iota(perm + 1, perm + 1 + n, 0);
    std::random_shuffle(perm + 1, perm + 1 + n);
    moveInside(1);

    for (int i = 2 ; i <= n ; ++i)
    {
        moveInside(i);
        if (pressButton() == 2)
        {
            if (diff.size() < 2)
            {
                int currPtr = diff.size() - 1;
                while (currPtr > 0)
                {
                    moveOutside(diff[currPtr]);
                    if (pressButton() == 1)
                    {
                        moveInside(diff[currPtr]);
                        break;
                    }
                    
                    currPtr--;
                }

                a[i] = a[diff[currPtr]];
                for (int i = currPtr + 1 ; i < diff.size() ; ++i)
                {
                    moveInside(diff[i]);    
                }

                moveOutside(i);
                continue;
            }

            tree[1].push_back({i, diff.size() - 1});
            moveOutside(i);
            continue;
        }

        diff.push_back(i);
        a[i] = ++cnt;
    }

    if (diff.size() > n / 2)
    {
        return 1;
    }

    divide();
    std::sort(a + 1, a + 1 + n);

    int curr = 1, ans = n;
    for (int i = 2 ; i <= n ; ++i)
    {
        if (a[i] == a[i - 1]) curr++;
        else
        {
            ans = std::min(ans, curr);
            curr = 1;
        }
    }

    ans = std::min(ans, curr);
    return ans;
}

Compilation message (stderr)

insects.cpp: In function 'void divide()':
insects.cpp:46:28: warning: narrowing conversion of '(diff.std::vector<int>::size() - 1)' from 'std::vector<int>::size_type' {aka 'long unsigned int'} to 'int' [-Wnarrowing]
   46 |     q.push({0, diff.size() - 1, 1});
      |                ~~~~~~~~~~~~^~~
insects.cpp: In function 'int min_cardinality(int)':
insects.cpp:133:46: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  133 |                 for (int i = currPtr + 1 ; i < diff.size() ; ++i)
      |                                            ~~^~~~~~~~~~~~~
insects.cpp:151:21: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  151 |     if (diff.size() > n / 2)
      |         ~~~~~~~~~~~~^~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...