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 <iostream>
#include <vector>
#include <stack>
#include <set>
#include <algorithm>
#include <cassert>
#define ff first
#define ss second
using namespace std;
typedef pair<int, int> pi;
//vector<int> ans;
//stack<pi> S, v;
vector<int> v, w, u[2020];
vector<pi> p;
set<pi> s;
int d[2020];
int min_cardinality(int N) {
int num = 0;
for(int i = 0; i < N; i++) {
move_inside(i);
num++;
if(i == 0) continue;
if(press_button() > 1) {
move_outside(i);
num--;
s.insert(pi(2, i));
}
}
if(num == 1) return N;
if(num == 2) {
for(pi i : s) move_inside(i.ss);
int mx = press_button();
return N-mx;
}
//cout<<num<<endl;
int sum = num;
int lb = 1, ub = N/num, mid;
while(lb < ub) {
mid = lb+ub+1>>1;
for(pi i : s) {
move_inside(i.ss);
int x = press_button();
if(x > mid) {
w.push_back(i.ss);
move_outside(i.ss);
d[i.ss] = mid+1;
p.push_back(pi(d[i.ss], i.ss));
continue;
}
u[x].push_back(i.ss);
d[i.ss] = x;
p.push_back(pi(x, i.ss));
sum++;
}
s.clear();
for(pi t : p) s.insert(t);
p.clear();
bool flag = false;
while(lb < ub) {
mid = lb+ub+1>>1;
// for(int i = lb; i <= ub+1; i++) {
// cout<<i<<" : ";
// for(int t:u[i])cout<<t<<" ";
// cout<<endl;
// }
// cout<<endl;
if(flag) {
for(int i = ub+1; i > mid; i--) {
for(int t : u[i]) {
move_outside(t);
sum--;
}
}
//assert(press_button() == mid);
for(int i = mid+1; i <= ub+1; i++) {
w.push_back(u[i][0]);
for(int j = 1; j < u[i].size(); j++) {
int t = u[i][j];
move_inside(t);
if(press_button() > mid) {
move_outside(t);
w.push_back(t);
}
else {
sum++;
u[mid].push_back(t);
p.push_back(pi(d[t], t));
}
}
}
for(pi t : p) {
s.erase(t);
d[t.ss] = mid;
s.insert(pi(d[t.ss], t.ss));
}
p.clear();
}
flag = true;
//cout<<sum<<endl;
if(sum == mid*num) {
for(int i = lb+1; i <= mid; i++)
for(int t : u[i])
s.erase(pi(d[t], t));
for(int i = lb+1; i <= ub+1; i++) u[i].clear();
lb = mid;
w.clear();
break;
}
else {
for(int t : w) s.erase(pi(d[t], t));
// for(int t : u[mid]) move_outside(t);
// sum -= u[mid].size();
for(int i = mid+1; i <= ub+1; i++) u[i].clear();
w.clear();
ub = mid-1;
}
}
}
return lb;
}
Compilation message (stderr)
insects.cpp: In function 'int min_cardinality(int)':
insects.cpp:44:20: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
44 | mid = lb+ub+1>>1;
| ~~~~~^~
insects.cpp:67:24: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
67 | mid = lb+ub+1>>1;
| ~~~~~^~
insects.cpp:86:38: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
86 | for(int j = 1; j < u[i].size(); j++) {
| ~~^~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |