#include "insects.h"
#include "bits/stdc++.h"
using namespace std;
#define rep(i,a,b) for(int i=(a);i<(b);++i)
#define sz(x) (int)x.size()
#define all(x) begin(x), end(x)
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef long long ll;
#ifdef DEBUG
auto& operator<<(auto& os, pair<auto, auto> &p) {
return os<<"("<<p.first<<", "<<p.second<<")";
}
auto& operator<<(auto& os, const auto &v) {
os<<"{";
for(auto it=begin(v);it!=end(v);++it) {
if(it != begin(v)) os<<", ";
os<<(*it);
}
return os<<"}";
}
void dbg_out(auto... x) {
((cerr<<' '<<x), ...) << endl;
}
#define dbg(x...) cerr<<"("<<#x<<"):", dbg_out(x);
#else
#define dbg(...)
#endif
int n;
vi perm;
vi added;
void in(int i) {
if(added[i]) return;
added[i] = true;
move_inside(perm[i]);
}
void out(int i) {
if(!added[i]) return;
added[i] = false;
move_outside(perm[i]);
}
int button() {
int res = press_button();
dbg(added, res);
return res;
}
vi dsu, siz;
int find(int x) {
if(dsu[x] == x) return x;
return dsu[x] = find(dsu[x]);
}
void merge(int a, int b) {
dbg("merge", a, b);
a = find(a); b = find(b);
if(a == b) return;
if(siz[a] < siz[b]) swap(a, b);
siz[a] += siz[b];
dsu[b] = a;
}
vi merge_dsu(vi a, vi b) {
if(sz(a) < sz(b)) swap(a, b);
vector<tuple<int,int,int>> bin;
for(auto x : a) in(x);
rep(i,0,sz(b)) {
in(b[i]);
if(button() != 1) {
bin.emplace_back(0,sz(a)-1,i);
}
out(b[i]);
}
while(!bin.empty()) {
sort(all(bin));
pii last = {-1, -1};
vector<tuple<int,int,int>> new_bin;
for(auto [l,r,ind] : bin) {
dbg(l, r, ind);
if(l == r) {
merge(a[l], b[ind]);
continue;
}
int mid = (l + r + 1) / 2;
if(make_pair(l, r) != last) {
rep(i,0,sz(a)) {
if(l <= i && i < mid) in(a[i]);
else out(a[i]);
}
last = {l, r};
}
in(b[ind]);
if(button() == 1)
l = mid;
else
r = mid - 1;
out(b[ind]);
new_bin.emplace_back(l, r, ind);
}
bin = new_bin;
}
rep(i,0,sz(a))
out(a[i]);
set<int> s;
rep(i,0,sz(a)) s.insert(find(a[i]));
rep(i,0,sz(b)) s.insert(find(b[i]));
dbg(a, b, s);
return vi(all(s));
}
vi solve(int l, int r) {
if(l == r) return {l};
int mid = (l + r) / 2;
auto a = solve(l, mid);
auto b = solve(mid+1, r);
auto res = merge_dsu(a, b);
dbg(l, r, res);
return res;
}
int min_cardinality(int N) {
n = N;
perm.resize(n);
iota(all(perm), 0);
// random_shuffle(all(perm));
added.assign(n, 0);
siz.assign(n, 1);
dsu.resize(n);
iota(all(dsu), 0);
solve(0, n-1);
dbg(dsu);
dbg(siz);
int mini = n;
rep(i,0,n)
mini = min(mini, siz[find(i)]);
return mini;
}
# |
Verdict |
Execution time |
Memory |
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 |
3 ms |
344 KB |
Output is correct |
8 |
Correct |
6 ms |
344 KB |
Output is correct |
9 |
Correct |
5 ms |
344 KB |
Output is correct |
10 |
Correct |
2 ms |
344 KB |
Output is correct |
11 |
Correct |
4 ms |
440 KB |
Output is correct |
12 |
Correct |
3 ms |
344 KB |
Output is correct |
13 |
Correct |
5 ms |
344 KB |
Output is correct |
14 |
Correct |
5 ms |
344 KB |
Output is correct |
15 |
Correct |
6 ms |
344 KB |
Output is correct |
16 |
Correct |
7 ms |
344 KB |
Output is correct |
17 |
Correct |
5 ms |
344 KB |
Output is correct |
18 |
Correct |
5 ms |
344 KB |
Output is correct |
19 |
Correct |
6 ms |
436 KB |
Output is correct |
20 |
Correct |
6 ms |
344 KB |
Output is correct |
21 |
Correct |
6 ms |
652 KB |
Output is correct |
22 |
Correct |
4 ms |
440 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
3 ms |
344 KB |
Output is correct |
8 |
Correct |
6 ms |
344 KB |
Output is correct |
9 |
Correct |
5 ms |
344 KB |
Output is correct |
10 |
Correct |
2 ms |
344 KB |
Output is correct |
11 |
Correct |
4 ms |
440 KB |
Output is correct |
12 |
Correct |
3 ms |
344 KB |
Output is correct |
13 |
Correct |
5 ms |
344 KB |
Output is correct |
14 |
Correct |
5 ms |
344 KB |
Output is correct |
15 |
Correct |
6 ms |
344 KB |
Output is correct |
16 |
Correct |
7 ms |
344 KB |
Output is correct |
17 |
Correct |
5 ms |
344 KB |
Output is correct |
18 |
Correct |
5 ms |
344 KB |
Output is correct |
19 |
Correct |
6 ms |
436 KB |
Output is correct |
20 |
Correct |
6 ms |
344 KB |
Output is correct |
21 |
Correct |
6 ms |
652 KB |
Output is correct |
22 |
Correct |
4 ms |
440 KB |
Output is correct |
23 |
Correct |
6 ms |
344 KB |
Output is correct |
24 |
Correct |
26 ms |
440 KB |
Output is correct |
25 |
Correct |
43 ms |
344 KB |
Output is correct |
26 |
Correct |
29 ms |
344 KB |
Output is correct |
27 |
Correct |
4 ms |
344 KB |
Output is correct |
28 |
Correct |
20 ms |
460 KB |
Output is correct |
29 |
Correct |
16 ms |
344 KB |
Output is correct |
30 |
Correct |
44 ms |
340 KB |
Output is correct |
31 |
Correct |
16 ms |
344 KB |
Output is correct |
32 |
Correct |
24 ms |
344 KB |
Output is correct |
33 |
Correct |
21 ms |
344 KB |
Output is correct |
34 |
Correct |
31 ms |
344 KB |
Output is correct |
35 |
Correct |
27 ms |
344 KB |
Output is correct |
36 |
Correct |
29 ms |
344 KB |
Output is correct |
37 |
Correct |
52 ms |
600 KB |
Output is correct |
38 |
Correct |
53 ms |
460 KB |
Output is correct |
39 |
Correct |
45 ms |
600 KB |
Output is correct |
40 |
Correct |
52 ms |
596 KB |
Output is correct |
41 |
Correct |
54 ms |
848 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
10 ms |
344 KB |
Output is correct |
8 |
Partially correct |
77 ms |
704 KB |
Output is partially correct |
9 |
Partially correct |
88 ms |
344 KB |
Output is partially correct |
10 |
Partially correct |
113 ms |
600 KB |
Output is partially correct |
11 |
Correct |
12 ms |
344 KB |
Output is correct |
12 |
Partially correct |
51 ms |
736 KB |
Output is partially correct |
13 |
Partially correct |
32 ms |
344 KB |
Output is partially correct |
14 |
Partially correct |
88 ms |
344 KB |
Output is partially correct |
15 |
Partially correct |
56 ms |
344 KB |
Output is partially correct |
16 |
Partially correct |
49 ms |
344 KB |
Output is partially correct |
17 |
Partially correct |
53 ms |
344 KB |
Output is partially correct |
18 |
Partially correct |
70 ms |
344 KB |
Output is partially correct |
19 |
Partially correct |
66 ms |
448 KB |
Output is partially correct |
20 |
Partially correct |
90 ms |
344 KB |
Output is partially correct |
21 |
Partially correct |
104 ms |
592 KB |
Output is partially correct |
22 |
Partially correct |
123 ms |
724 KB |
Output is partially correct |
23 |
Partially correct |
127 ms |
728 KB |
Output is partially correct |
24 |
Partially correct |
128 ms |
732 KB |
Output is partially correct |
25 |
Partially correct |
114 ms |
732 KB |
Output is partially correct |
26 |
Partially correct |
118 ms |
988 KB |
Output is partially correct |
27 |
Partially correct |
121 ms |
472 KB |
Output is partially correct |
28 |
Partially correct |
129 ms |
476 KB |
Output is partially correct |
29 |
Partially correct |
84 ms |
460 KB |
Output is partially correct |
30 |
Partially correct |
89 ms |
344 KB |
Output is partially correct |
31 |
Partially correct |
111 ms |
344 KB |
Output is partially correct |
32 |
Partially correct |
88 ms |
344 KB |
Output is partially correct |
33 |
Partially correct |
131 ms |
344 KB |
Output is partially correct |
34 |
Partially correct |
136 ms |
600 KB |
Output is partially correct |
35 |
Partially correct |
105 ms |
344 KB |
Output is partially correct |
36 |
Partially correct |
112 ms |
344 KB |
Output is partially correct |
37 |
Partially correct |
130 ms |
344 KB |
Output is partially correct |
38 |
Partially correct |
109 ms |
344 KB |
Output is partially correct |
39 |
Partially correct |
124 ms |
344 KB |
Output is partially correct |
40 |
Partially correct |
126 ms |
724 KB |
Output is partially correct |
41 |
Partially correct |
104 ms |
344 KB |
Output is partially correct |
42 |
Partially correct |
110 ms |
344 KB |
Output is partially correct |
43 |
Partially correct |
5 ms |
344 KB |
Output is partially correct |
44 |
Partially correct |
30 ms |
344 KB |
Output is partially correct |
45 |
Partially correct |
16 ms |
344 KB |
Output is partially correct |
46 |
Partially correct |
38 ms |
704 KB |
Output is partially correct |
47 |
Partially correct |
42 ms |
608 KB |
Output is partially correct |
48 |
Partially correct |
35 ms |
848 KB |
Output is partially correct |