#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;
for (auto &e : later) sOut.insert(e);
later.clear();
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();
for (auto &e : later) sOut.insert(e);
later.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);
later.insert(recent);
cur = mid;
}
}
up = true;
for (auto &e : later) sIn.insert(e);
later.clear();
goto try_up;
}
}
return left;
}
#ifdef TEST
#include "grader.cpp"
#endif
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 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 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
2 ms |
344 KB |
Output is correct |
9 |
Correct |
4 ms |
344 KB |
Output is correct |
10 |
Correct |
6 ms |
456 KB |
Output is correct |
11 |
Correct |
2 ms |
344 KB |
Output is correct |
12 |
Correct |
5 ms |
452 KB |
Output is correct |
13 |
Correct |
6 ms |
344 KB |
Output is correct |
14 |
Correct |
2 ms |
344 KB |
Output is correct |
15 |
Correct |
2 ms |
344 KB |
Output is correct |
16 |
Correct |
3 ms |
440 KB |
Output is correct |
17 |
Correct |
2 ms |
344 KB |
Output is correct |
18 |
Correct |
4 ms |
344 KB |
Output is correct |
19 |
Correct |
4 ms |
344 KB |
Output is correct |
20 |
Correct |
4 ms |
344 KB |
Output is correct |
21 |
Correct |
2 ms |
448 KB |
Output is correct |
22 |
Correct |
2 ms |
444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 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 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
2 ms |
344 KB |
Output is correct |
9 |
Correct |
4 ms |
344 KB |
Output is correct |
10 |
Correct |
6 ms |
456 KB |
Output is correct |
11 |
Correct |
2 ms |
344 KB |
Output is correct |
12 |
Correct |
5 ms |
452 KB |
Output is correct |
13 |
Correct |
6 ms |
344 KB |
Output is correct |
14 |
Correct |
2 ms |
344 KB |
Output is correct |
15 |
Correct |
2 ms |
344 KB |
Output is correct |
16 |
Correct |
3 ms |
440 KB |
Output is correct |
17 |
Correct |
2 ms |
344 KB |
Output is correct |
18 |
Correct |
4 ms |
344 KB |
Output is correct |
19 |
Correct |
4 ms |
344 KB |
Output is correct |
20 |
Correct |
4 ms |
344 KB |
Output is correct |
21 |
Correct |
2 ms |
448 KB |
Output is correct |
22 |
Correct |
2 ms |
444 KB |
Output is correct |
23 |
Correct |
4 ms |
472 KB |
Output is correct |
24 |
Correct |
5 ms |
344 KB |
Output is correct |
25 |
Correct |
14 ms |
344 KB |
Output is correct |
26 |
Correct |
26 ms |
600 KB |
Output is correct |
27 |
Correct |
52 ms |
592 KB |
Output is correct |
28 |
Correct |
7 ms |
960 KB |
Output is correct |
29 |
Correct |
65 ms |
848 KB |
Output is correct |
30 |
Correct |
25 ms |
344 KB |
Output is correct |
31 |
Correct |
8 ms |
456 KB |
Output is correct |
32 |
Correct |
10 ms |
344 KB |
Output is correct |
33 |
Correct |
21 ms |
704 KB |
Output is correct |
34 |
Correct |
10 ms |
344 KB |
Output is correct |
35 |
Correct |
12 ms |
476 KB |
Output is correct |
36 |
Correct |
17 ms |
344 KB |
Output is correct |
37 |
Correct |
20 ms |
444 KB |
Output is correct |
38 |
Correct |
20 ms |
600 KB |
Output is correct |
39 |
Correct |
17 ms |
600 KB |
Output is correct |
40 |
Correct |
13 ms |
468 KB |
Output is correct |
41 |
Correct |
5 ms |
448 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 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 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
12 ms |
440 KB |
Output is correct |
8 |
Correct |
13 ms |
344 KB |
Output is correct |
9 |
Correct |
19 ms |
344 KB |
Output is correct |
10 |
Partially correct |
38 ms |
544 KB |
Output is partially correct |
11 |
Partially correct |
164 ms |
848 KB |
Output is partially correct |
12 |
Correct |
18 ms |
464 KB |
Output is correct |
13 |
Partially correct |
130 ms |
1080 KB |
Output is partially correct |
14 |
Partially correct |
84 ms |
792 KB |
Output is partially correct |
15 |
Correct |
14 ms |
344 KB |
Output is correct |
16 |
Correct |
23 ms |
592 KB |
Output is correct |
17 |
Correct |
19 ms |
344 KB |
Output is correct |
18 |
Correct |
18 ms |
644 KB |
Output is correct |
19 |
Correct |
39 ms |
600 KB |
Output is correct |
20 |
Correct |
24 ms |
512 KB |
Output is correct |
21 |
Partially correct |
55 ms |
860 KB |
Output is partially correct |
22 |
Partially correct |
60 ms |
600 KB |
Output is partially correct |
23 |
Partially correct |
42 ms |
804 KB |
Output is partially correct |
24 |
Correct |
33 ms |
760 KB |
Output is correct |
25 |
Correct |
16 ms |
344 KB |
Output is correct |
26 |
Correct |
10 ms |
344 KB |
Output is correct |
27 |
Correct |
24 ms |
344 KB |
Output is correct |
28 |
Correct |
26 ms |
592 KB |
Output is correct |
29 |
Correct |
16 ms |
748 KB |
Output is correct |
30 |
Correct |
20 ms |
516 KB |
Output is correct |
31 |
Partially correct |
40 ms |
344 KB |
Output is partially correct |
32 |
Partially correct |
41 ms |
600 KB |
Output is partially correct |
33 |
Partially correct |
58 ms |
568 KB |
Output is partially correct |
34 |
Partially correct |
56 ms |
568 KB |
Output is partially correct |
35 |
Correct |
35 ms |
728 KB |
Output is correct |
36 |
Correct |
19 ms |
732 KB |
Output is correct |
37 |
Partially correct |
46 ms |
712 KB |
Output is partially correct |
38 |
Partially correct |
33 ms |
756 KB |
Output is partially correct |
39 |
Correct |
23 ms |
344 KB |
Output is correct |
40 |
Correct |
24 ms |
344 KB |
Output is correct |
41 |
Correct |
25 ms |
448 KB |
Output is correct |
42 |
Correct |
21 ms |
960 KB |
Output is correct |
43 |
Partially correct |
17 ms |
344 KB |
Output is partially correct |
44 |
Partially correct |
137 ms |
1252 KB |
Output is partially correct |
45 |
Correct |
11 ms |
344 KB |
Output is correct |
46 |
Correct |
23 ms |
480 KB |
Output is correct |
47 |
Correct |
20 ms |
612 KB |
Output is correct |
48 |
Correct |
17 ms |
708 KB |
Output is correct |