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 <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;
inline friend bool operator < (const Range &a, const Range &b)
{
return a.l < b.l;
}
};
std::set <Range> q;
void divide()
{
int ptrL = 0;
int ptrR = diff.size() - 1;
q.insert({0, diff.size() - 1, 1});
while (!q.empty())
{
auto [l, r, node] = *q.begin();
q.erase(q.begin());
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.insert({mid + 1, r, 2*node + 1});
}
if (tree[2*node].size())
{
q.insert({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:51:30: warning: narrowing conversion of '(diff.std::vector<int>::size() - 1)' from 'std::vector<int>::size_type' {aka 'long unsigned int'} to 'int' [-Wnarrowing]
51 | q.insert({0, diff.size() - 1, 1});
| ~~~~~~~~~~~~^~~
insects.cpp: In function 'int min_cardinality(int)':
insects.cpp:138:46: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
138 | for (int i = currPtr + 1 ; i < diff.size() ; ++i)
| ~~^~~~~~~~~~~~~
insects.cpp:156:21: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
156 | if (diff.size() > n / 2)
| ~~~~~~~~~~~~^~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |