#include <bits/stdc++.h>
#include "insects.h"
using namespace std;
typedef long long ll;
int n;
vector<bool> in;
unordered_set<int> sIn, sOut;
int cnt;
int numA, numP;
mt19937 mt;
void add(int i) {
if (in[i]) return;
numA++;
move_inside(i);
cnt++;
in[i] = true;
}
int add_rand() {
int id = mt() % sOut.size();
auto iter = sOut.begin();
while (id--) iter++;
int i = *iter;
add(i);
sOut.erase(i);
return i;
}
void remove(int i) {
if (!in[i]) return;
move_outside(i);
cnt--;
in[i] = false;
}
int remove_rand() {
int id = mt() % sIn.size();
auto iter = sIn.begin();
while (id--) iter++;
int i = *iter;
remove(i);
sIn.erase(i);
return i;
}
int press() {
numP++;
return press_button();
}
int min_cardinality(int N) {
n = N;
cnt = 0;
numA = numP = 0;
mt = mt19937(time(0));
in = vector<bool>(n);
int cur = 0;
for (int i = 0; i < n; i++) {
add(i);
cur = press();
if (cur > 1) {
remove(i);
sOut.insert(i);
cur = 1;
}
}
int typeCnt = cnt;
int left = 1, right = n / typeCnt;
bool up = true;
restart:
while (left < right) {
int mid = (left + right + 1) / 2;
//unordered_set<int> later;
try_up:
if (up) {
while (cnt < typeCnt * mid) {
if (sOut.empty()) {
right = mid - 1;
up = false;
goto restart;
}
int inc = mid - cur + 1;
int recent = -1;
while (!sOut.empty() && inc--) {
recent = add_rand();
sIn.insert(recent);
}
cur = press();
if (cur > mid) {
remove(recent);
sIn.erase(recent);
//later.insert(recent);
cur = mid;
}
}
left = mid;
up = true;
sIn.clear();
}
else {
while (cur > mid) {
int dec = cur - mid + 1;
int recent = -1;
while (dec--) {
recent = remove_rand();
sOut.insert(recent);
}
cur = press();
if (cur < mid) {
add(recent);
sOut.erase(recent);
//sIn.insert(recent);
cur = mid;
}
}
up = true;
goto try_up;
}
}
return left;
}
#ifdef TEST
#include "grader.cpp"
#endif
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Incorrect |
1 ms |
344 KB |
Wrong answer. |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Incorrect |
1 ms |
344 KB |
Wrong answer. |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Incorrect |
17 ms |
600 KB |
Wrong answer. |
8 |
Halted |
0 ms |
0 KB |
- |