Submission #834860

# Submission time Handle Problem Language Result Execution time Memory
834860 2023-08-22T21:16:49 Z esomer Rarest Insects (IOI22_insects) C++17
99.84 / 100
55 ms 408 KB
#include<bits/stdc++.h>
#include "insects.h"

using namespace std;

mt19937 gen(time(0));

int groups, lft;
vector<bool> done;

int get_groups(int N){
	int groups = 0;
	for(int i = 0; i < N; i++){
		move_inside(i);
		int rep = press_button();
		if(rep > 1) { move_outside(i); }
		else { groups++; done[i] = 1; }
	}
	return groups;
}

int choose_best(int lo, int hi){
	//If I choose mid. Then in the case that all the groups are of size at least mid,
	//I discard (mid - lo + 1) * groups.
	//In the other case, I discard left - chosen (and chosen can be at most (mid - lo + 1) * groups).
	//So I want min((mid - lo + 1) * groups, left - (mid - lo + 1) * groups) to be the maximum.
	int x = lft / 2;
	int bst = x / groups;
	int mn1 = min(bst * groups, lft - bst * groups);
	bst++;
	int mn2 = min(bst * groups, lft - bst * groups);
	if(mn1 < mn2) bst--;
	return max(lo, min(hi, lo + bst - 1));
}

int min_cardinality(int N) {
	done.assign(N, 0);
	int ans = 1;
	groups = get_groups(N);
	lft = N - groups;
	int cnt = groups;
	int lo = 2;
	int hi = N / groups;
	while(lo <= hi && lft > 0) {
		int mid = choose_best(lo, hi);
		vector<int> good, bad;
		vector<int> possible;
		for(int i = 0; i < N; i++) {
			if(!done[i]) possible.push_back(i);
		}
		while((int)possible.size() > 0){
			if((cnt + (int)good.size()) == mid * groups) {
				bad.push_back(possible.back());
				possible.pop_back();
				continue;
			}
			int ind = gen() % ((int)possible.size());
			int i = possible[ind];
			move_inside(i);
			int rep = press_button();
			if(rep > mid) {
				bad.push_back(i);
				move_outside(i);
			} else { good.push_back(i); }
			if(ind != (int)possible.size() - 1) swap(possible[ind], possible[(int)possible.size() - 1]);
			possible.pop_back();
		}
		if((int)good.size() + cnt == mid * groups) {
			ans = mid;
			lo = mid + 1;
			for(int x : good) done[x] = 1;
			cnt += (int)good.size();
			lft -= (int)good.size();
		} else {
			hi = mid - 1;
			for(int x : good) move_outside(x);
			for(int x : bad) done[x] = 1;
			lft -= (int)bad.size();
		}
	}
	return ans;
}
# Verdict Execution time Memory 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 212 KB Output is correct
6 Correct 4 ms 208 KB Output is correct
7 Correct 2 ms 208 KB Output is correct
8 Correct 4 ms 304 KB Output is correct
9 Correct 5 ms 208 KB Output is correct
10 Correct 4 ms 208 KB Output is correct
11 Correct 1 ms 208 KB Output is correct
12 Correct 4 ms 336 KB Output is correct
13 Correct 4 ms 208 KB Output is correct
14 Correct 5 ms 208 KB Output is correct
15 Correct 5 ms 208 KB Output is correct
16 Correct 5 ms 208 KB Output is correct
17 Correct 3 ms 208 KB Output is correct
18 Correct 6 ms 208 KB Output is correct
19 Correct 4 ms 208 KB Output is correct
20 Correct 3 ms 208 KB Output is correct
21 Correct 3 ms 208 KB Output is correct
22 Correct 2 ms 208 KB Output is correct
# Verdict Execution time Memory 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 212 KB Output is correct
6 Correct 4 ms 208 KB Output is correct
7 Correct 2 ms 208 KB Output is correct
8 Correct 4 ms 304 KB Output is correct
9 Correct 5 ms 208 KB Output is correct
10 Correct 4 ms 208 KB Output is correct
11 Correct 1 ms 208 KB Output is correct
12 Correct 4 ms 336 KB Output is correct
13 Correct 4 ms 208 KB Output is correct
14 Correct 5 ms 208 KB Output is correct
15 Correct 5 ms 208 KB Output is correct
16 Correct 5 ms 208 KB Output is correct
17 Correct 3 ms 208 KB Output is correct
18 Correct 6 ms 208 KB Output is correct
19 Correct 4 ms 208 KB Output is correct
20 Correct 3 ms 208 KB Output is correct
21 Correct 3 ms 208 KB Output is correct
22 Correct 2 ms 208 KB Output is correct
23 Correct 16 ms 292 KB Output is correct
24 Correct 9 ms 208 KB Output is correct
25 Correct 19 ms 304 KB Output is correct
26 Correct 22 ms 328 KB Output is correct
27 Correct 20 ms 300 KB Output is correct
28 Correct 9 ms 208 KB Output is correct
29 Correct 24 ms 296 KB Output is correct
30 Correct 15 ms 312 KB Output is correct
31 Correct 20 ms 292 KB Output is correct
32 Correct 14 ms 208 KB Output is correct
33 Correct 23 ms 208 KB Output is correct
34 Correct 20 ms 208 KB Output is correct
35 Correct 17 ms 208 KB Output is correct
36 Correct 22 ms 280 KB Output is correct
37 Correct 18 ms 312 KB Output is correct
38 Correct 20 ms 296 KB Output is correct
39 Correct 24 ms 304 KB Output is correct
40 Correct 9 ms 304 KB Output is correct
41 Correct 7 ms 208 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 208 KB Output is correct
2 Correct 1 ms 208 KB Output is correct
3 Correct 1 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 0 ms 208 KB Output is correct
7 Correct 34 ms 284 KB Output is correct
8 Correct 12 ms 208 KB Output is correct
9 Correct 31 ms 292 KB Output is correct
10 Correct 37 ms 300 KB Output is correct
11 Correct 41 ms 300 KB Output is correct
12 Correct 19 ms 208 KB Output is correct
13 Correct 37 ms 308 KB Output is correct
14 Correct 41 ms 292 KB Output is correct
15 Correct 30 ms 292 KB Output is correct
16 Correct 29 ms 280 KB Output is correct
17 Correct 28 ms 296 KB Output is correct
18 Correct 43 ms 280 KB Output is correct
19 Correct 32 ms 208 KB Output is correct
20 Correct 35 ms 284 KB Output is correct
21 Correct 37 ms 284 KB Output is correct
22 Correct 24 ms 284 KB Output is correct
23 Correct 43 ms 288 KB Output is correct
24 Correct 27 ms 300 KB Output is correct
25 Correct 20 ms 300 KB Output is correct
26 Correct 13 ms 208 KB Output is correct
27 Correct 40 ms 284 KB Output is correct
28 Correct 24 ms 304 KB Output is correct
29 Correct 27 ms 288 KB Output is correct
30 Correct 31 ms 296 KB Output is correct
31 Correct 19 ms 296 KB Output is correct
32 Correct 55 ms 300 KB Output is correct
33 Partially correct 40 ms 408 KB Output is partially correct
34 Partially correct 50 ms 308 KB Output is partially correct
35 Correct 31 ms 300 KB Output is correct
36 Correct 31 ms 292 KB Output is correct
37 Correct 41 ms 304 KB Output is correct
38 Correct 40 ms 296 KB Output is correct
39 Correct 42 ms 300 KB Output is correct
40 Correct 42 ms 292 KB Output is correct
41 Correct 35 ms 300 KB Output is correct
42 Correct 36 ms 292 KB Output is correct
43 Correct 14 ms 208 KB Output is correct
44 Correct 32 ms 300 KB Output is correct
45 Correct 23 ms 284 KB Output is correct
46 Correct 27 ms 280 KB Output is correct
47 Correct 21 ms 208 KB Output is correct
48 Correct 17 ms 296 KB Output is correct