# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
627794 | peti1234 | Rarest Insects (IOI22_insects) | C++17 | 0 ms | 0 KiB |
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 <bits/stdc++.h>
using namespace std;
#include "insects.h"
const int c=2005;
int cnt, si, ord[c];
bool v[c], fix[c];
int add(int a) {
assert(!v[a]);
v[a]=true;
si++;
move_inside(a);
return press_button();
}
void sub(int a) {
assert(v[a]);
v[a]=false;
si--;
move_outside(a);
}
int min_cardinality(int N) {
for (int i=0; i<N; i++) {
ord[i]=i;
}
random_shuffle(ord, ord+N);
for (int i=0; i<N; i++) {
if (add(i)==2) {
sub(i);
} else {
cnt++;
}
}
for (int i=0; i<N; i++) {
if (v[i]) {
fix[i]=1;
}
}
int lo=1, hi=N/cnt+1, mid;
while (hi-lo>1) {
mid=(hi+lo)/2;
for (int i=0; i<N; i++) {
int pos=ord[i];
if (fix[pos] || si==cnt*mid) continue;
if (add(pos)>mid) {
sub(pos);
}
}
if (si==cnt*mid) {
lo=mid;
for (int i=0; i<N; i++) {
if (v[i]) {
fix[i]=1;
}
}
} else {
hi=mid;
for (int i=0; i<N; i++) {
if (fix[i]) continue;
if (!v[i]) fix[i]=1;
else {
sub(i);
}
}
}
}
return lo;
}
/*
int main()
{
return 0;
}
*/#include <bits/stdc++.h>
using namespace std;
#include "insects.h"
const int c=2005;
int cnt, si, ord[c];
bool v[c], fix[c];
int add(int a) {
assert(!v[a]);
v[a]=true;
si++;
move_inside(a);
return press_button();
}
void sub(int a) {
assert(v[a]);
v[a]=false;
si--;
move_outside(a);
}
int min_cardinality(int N) {
for (int i=0; i<N; i++) {
ord[i]=i;
}
random_shuffle(ord, ord+N);
for (int i=0; i<N; i++) {
if (add(i)==2) {
sub(i);
} else {
cnt++;
}
}
for (int i=0; i<N; i++) {
if (v[i]) {
fix[i]=1;
}
}
int lo=1, hi=N/cnt+1, mid;
while (hi-lo>1) {
mid=(hi+lo)/2;
for (int i=0; i<N; i++) {
int pos=ord[i];
if (fix[pos] || si==cnt*mid) continue;
if (add(pos)>mid) {
sub(pos);
}
}
if (si==cnt*mid) {
lo=mid;
for (int i=0; i<N; i++) {
if (v[i]) {
fix[i]=1;
}
}
} else {
hi=mid;
for (int i=0; i<N; i++) {
if (fix[i]) continue;
if (!v[i]) fix[i]=1;
else {
sub(i);
}
}
}
}
return lo;
}
/*
int main()
{
return 0;
}
*/