This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "insects.h"
#include <algorithm>
#include <iostream>
#include <numeric>
#include <vector>
#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]);
}
int ptr;
void divide(int l, int r, int node)
{
if (l == r)
{
for (const auto &[i, to] : tree[node])
{
a[i] = a[diff[l]];
}
return;
}
int mid = (l + r) / 2;
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())
{
int mid2 = (r + mid + 1) / 2;
while (ptr < mid2)
{
moveInside(diff[ptr + 1]);
ptr++;
}
divide(mid + 1, r, 2*node + 1);
}
if (tree[2*node].size())
{
int mid2 = (l + mid) / 2;
while (ptr > mid2)
{
moveOutside(diff[ptr]);
ptr--;
}
divide(l, mid, 2*node);
}
}
int min_cardinality(int N)
{
n = N;
a[1] = ++cnt;
diff.push_back(1);
srand(92098359);
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() < 22)
{
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;
}
ptr = diff.size() - 1;
int mid = (diff.size() - 1) / 2;
while (ptr > mid)
{
moveOutside(diff[ptr]);
ptr--;
}
divide(0, diff.size() - 1, 1);
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 'int min_cardinality(int)':
insects.cpp:126:46: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
126 | for (int i = currPtr + 1 ; i < diff.size() ; ++i)
| ~~^~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |