#include "insects.h"
#include <bits/stdc++.h>
using namespace std;
using vi = vector<int>;
using vvi = vector<vi>;
#define sz(x) int(x.size())
const int mx = 2'000;
vi ins(mx, 0);
int ct = 0;
vi dp(mx + 5, 0);
vi opt(mx + 5, 0);
int getRandom(int l, int r)
{
return rand() % (r-l+1);
}
void move_in(int i)
{
// cerr << "insert " << i << '\n';
if(ins[i])
return;
ins[i] = 1;
move_inside(i);
ct++;
}
void move_out(int i)
{
// cerr << "erase " << i << '\n';
if(!ins[i])
return;
ins[i] = 0;
move_outside(i);
ct--;
}
int Z = 0; //number of species
int ans = -1;
void solve(vi lst, int b) //b = basic value
{
if(sz(lst) < Z)
return;
// cerr << "solve : ";
// for(int u : lst)
// cerr << u << ' ';
// cerr << '\n';
int mx = sz(lst)/Z;
// int I = sz(lst);
// int med = opt[sz(lst)];
int med = (sz(lst)/Z)/2;
if(med == 0)
med++;
// cerr << "med = " << med << '\n';
vi ins, notins;
int to_exc = -1;
bool flag = 0;
for(int t = 0; t < 10*sz(lst); t++)
{
int u = getRandom(0, sz(lst)-1);
int v = getRandom(0, sz(lst)-1);
swap(lst[u], lst[v]);
}
for(int i : lst)
{
if(ct == (med + b) * Z)
{
notins.push_back(i);
continue;
}
move_in(i);
int pb = press_button();
if(pb - b > med)
{
move_out(i);
notins.push_back(i);
}
else
{
if(pb - b == med && flag == 0)
{
to_exc = i;
flag = 1;
}
ins.push_back(i);
}
}
if(ct == (med + b) * Z)
{
solve(notins, b + med);
}
else
{
if(sz(lst) >= 2*Z)
{
for(int i : ins)
move_out(i);
vi new_ins;
for(int i : ins)
if(i != to_exc)
new_ins.push_back(i);
solve(new_ins, b);
}
else
ans = (ct - sz(ins))/Z;
}
}
int min_cardinality(int N)
{
vi lst;
for(int i = 0; i < N; i++)
{
move_in(i);
if(press_button() >= 2)
{
move_out(i);
lst.push_back(i);
}
else
Z++;
}
// cerr << "Z = " << Z << '\n';
for(int i = 0; i <= N; i++)
{
dp[i] = 5'000'000;
opt[i] = -1;
if(i < Z)
{
dp[i] = 0;
opt[i] = 0;
}
else if(i < 2*Z)
{
dp[i] = i;
opt[i] = 1;
}
else
{
for(int j = 1; j <= i/Z; j++)
{
int cand = i + max(dp[i - j*Z], dp[j*Z]);
if(cand < dp[i])
{
dp[i] = cand;
opt[i] = j;
}
dp[i] = min(dp[i], i + max(dp[i - j*Z], dp[j*Z]));
}
}
// cerr << i << " : " << dp[i] << ' ' << opt[i] << "->" << opt[i]*Z << '\n';
}
solve(lst, 1);
if(ans != -1)
return ans;
return ct / Z;
}
Compilation message
insects.cpp: In function 'void solve(vi, int)':
insects.cpp:58:6: warning: unused variable 'mx' [-Wunused-variable]
58 | int mx = sz(lst)/Z;
| ^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
220 KB |
Output is correct |
2 |
Correct |
1 ms |
208 KB |
Output is correct |
3 |
Correct |
1 ms |
208 KB |
Output is correct |
4 |
Correct |
1 ms |
208 KB |
Output is correct |
5 |
Correct |
1 ms |
208 KB |
Output is correct |
6 |
Correct |
5 ms |
320 KB |
Output is correct |
7 |
Correct |
2 ms |
208 KB |
Output is correct |
8 |
Correct |
6 ms |
208 KB |
Output is correct |
9 |
Correct |
6 ms |
208 KB |
Output is correct |
10 |
Correct |
5 ms |
244 KB |
Output is correct |
11 |
Correct |
2 ms |
208 KB |
Output is correct |
12 |
Correct |
6 ms |
208 KB |
Output is correct |
13 |
Correct |
7 ms |
208 KB |
Output is correct |
14 |
Correct |
6 ms |
324 KB |
Output is correct |
15 |
Correct |
8 ms |
208 KB |
Output is correct |
16 |
Correct |
4 ms |
208 KB |
Output is correct |
17 |
Correct |
8 ms |
324 KB |
Output is correct |
18 |
Correct |
7 ms |
328 KB |
Output is correct |
19 |
Correct |
6 ms |
208 KB |
Output is correct |
20 |
Correct |
3 ms |
208 KB |
Output is correct |
21 |
Correct |
4 ms |
208 KB |
Output is correct |
22 |
Correct |
2 ms |
208 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
220 KB |
Output is correct |
2 |
Correct |
1 ms |
208 KB |
Output is correct |
3 |
Correct |
1 ms |
208 KB |
Output is correct |
4 |
Correct |
1 ms |
208 KB |
Output is correct |
5 |
Correct |
1 ms |
208 KB |
Output is correct |
6 |
Correct |
5 ms |
320 KB |
Output is correct |
7 |
Correct |
2 ms |
208 KB |
Output is correct |
8 |
Correct |
6 ms |
208 KB |
Output is correct |
9 |
Correct |
6 ms |
208 KB |
Output is correct |
10 |
Correct |
5 ms |
244 KB |
Output is correct |
11 |
Correct |
2 ms |
208 KB |
Output is correct |
12 |
Correct |
6 ms |
208 KB |
Output is correct |
13 |
Correct |
7 ms |
208 KB |
Output is correct |
14 |
Correct |
6 ms |
324 KB |
Output is correct |
15 |
Correct |
8 ms |
208 KB |
Output is correct |
16 |
Correct |
4 ms |
208 KB |
Output is correct |
17 |
Correct |
8 ms |
324 KB |
Output is correct |
18 |
Correct |
7 ms |
328 KB |
Output is correct |
19 |
Correct |
6 ms |
208 KB |
Output is correct |
20 |
Correct |
3 ms |
208 KB |
Output is correct |
21 |
Correct |
4 ms |
208 KB |
Output is correct |
22 |
Correct |
2 ms |
208 KB |
Output is correct |
23 |
Correct |
23 ms |
328 KB |
Output is correct |
24 |
Correct |
11 ms |
208 KB |
Output is correct |
25 |
Correct |
21 ms |
332 KB |
Output is correct |
26 |
Correct |
18 ms |
328 KB |
Output is correct |
27 |
Correct |
25 ms |
208 KB |
Output is correct |
28 |
Correct |
9 ms |
324 KB |
Output is correct |
29 |
Correct |
27 ms |
312 KB |
Output is correct |
30 |
Correct |
27 ms |
332 KB |
Output is correct |
31 |
Correct |
13 ms |
336 KB |
Output is correct |
32 |
Correct |
28 ms |
316 KB |
Output is correct |
33 |
Correct |
29 ms |
312 KB |
Output is correct |
34 |
Correct |
20 ms |
208 KB |
Output is correct |
35 |
Correct |
19 ms |
336 KB |
Output is correct |
36 |
Correct |
19 ms |
332 KB |
Output is correct |
37 |
Correct |
19 ms |
324 KB |
Output is correct |
38 |
Correct |
27 ms |
332 KB |
Output is correct |
39 |
Correct |
24 ms |
324 KB |
Output is correct |
40 |
Correct |
16 ms |
208 KB |
Output is correct |
41 |
Correct |
11 ms |
208 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
208 KB |
Output is correct |
2 |
Correct |
0 ms |
208 KB |
Output is correct |
3 |
Correct |
0 ms |
208 KB |
Output is correct |
4 |
Correct |
0 ms |
208 KB |
Output is correct |
5 |
Correct |
1 ms |
208 KB |
Output is correct |
6 |
Correct |
1 ms |
208 KB |
Output is correct |
7 |
Correct |
46 ms |
448 KB |
Output is correct |
8 |
Correct |
11 ms |
208 KB |
Output is correct |
9 |
Correct |
49 ms |
328 KB |
Output is correct |
10 |
Correct |
49 ms |
336 KB |
Output is correct |
11 |
Correct |
52 ms |
308 KB |
Output is correct |
12 |
Correct |
22 ms |
324 KB |
Output is correct |
13 |
Correct |
56 ms |
328 KB |
Output is correct |
14 |
Correct |
55 ms |
328 KB |
Output is correct |
15 |
Correct |
46 ms |
452 KB |
Output is correct |
16 |
Correct |
37 ms |
324 KB |
Output is correct |
17 |
Correct |
51 ms |
328 KB |
Output is correct |
18 |
Correct |
34 ms |
328 KB |
Output is correct |
19 |
Correct |
35 ms |
340 KB |
Output is correct |
20 |
Correct |
49 ms |
324 KB |
Output is correct |
21 |
Correct |
44 ms |
344 KB |
Output is correct |
22 |
Correct |
53 ms |
344 KB |
Output is correct |
23 |
Correct |
39 ms |
320 KB |
Output is correct |
24 |
Correct |
33 ms |
324 KB |
Output is correct |
25 |
Correct |
25 ms |
324 KB |
Output is correct |
26 |
Correct |
16 ms |
208 KB |
Output is correct |
27 |
Correct |
47 ms |
324 KB |
Output is correct |
28 |
Correct |
48 ms |
336 KB |
Output is correct |
29 |
Correct |
34 ms |
460 KB |
Output is correct |
30 |
Correct |
38 ms |
336 KB |
Output is correct |
31 |
Correct |
38 ms |
320 KB |
Output is correct |
32 |
Correct |
38 ms |
320 KB |
Output is correct |
33 |
Correct |
49 ms |
456 KB |
Output is correct |
34 |
Correct |
50 ms |
340 KB |
Output is correct |
35 |
Correct |
38 ms |
336 KB |
Output is correct |
36 |
Correct |
42 ms |
324 KB |
Output is correct |
37 |
Correct |
42 ms |
332 KB |
Output is correct |
38 |
Correct |
51 ms |
324 KB |
Output is correct |
39 |
Correct |
48 ms |
328 KB |
Output is correct |
40 |
Correct |
51 ms |
328 KB |
Output is correct |
41 |
Correct |
48 ms |
324 KB |
Output is correct |
42 |
Correct |
33 ms |
344 KB |
Output is correct |
43 |
Correct |
11 ms |
208 KB |
Output is correct |
44 |
Correct |
38 ms |
324 KB |
Output is correct |
45 |
Correct |
39 ms |
284 KB |
Output is correct |
46 |
Correct |
24 ms |
208 KB |
Output is correct |
47 |
Correct |
27 ms |
316 KB |
Output is correct |
48 |
Correct |
27 ms |
320 KB |
Output is correct |