Submission #629226

# Submission time Handle Problem Language Result Execution time Memory
629226 2022-08-14T09:35:24 Z Gullesnuffs Rarest Insects (IOI22_insects) C++17
100 / 100
83 ms 1744 KB
#include "insects.h"
#include <bits/stdc++.h>
using namespace std;

#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;

vector<deque<int>> at_h;
vector<int> min_num_at_or_below_h;
vector<int> max_num_at_or_below_h;
int num_inside = 0;
vector<int> v;
int n;
int H = 1;
int h_lo;
int h_hi;
int target_h;
int num_move_inside = 0;
int num_queries = 0;

void debug() {
	cerr << endl << endl;
	rep(h,1,sz(at_h)) {
		if (!at_h[h].empty() || max_num_at_or_below_h[h]) {
			cerr << "h = " << h << ". Num at or below = " << min_num_at_or_below_h[h] << " - " << max_num_at_or_below_h[h]+num_inside << ". Insects = [";
			for (int x : at_h[h]) {
				cerr << x << ", ";
			}
			cerr << "]" << endl;
		}
	}
	cerr << "num_inside = " << num_inside << endl;
	cerr << "h_lo = " << h_lo << endl;
	cerr << "h_hi = " << h_hi << endl;
	cerr << "target_h = " << target_h << endl;
	cerr << "num_move_inside = " << num_move_inside << endl;
	cerr << "num_queries = " << num_queries << endl;
}

int press_button_cache = -1;
void _move_inside(int i) {
	++num_inside;
	++num_move_inside;
	press_button_cache = -1;
	move_inside(i);
}
void _move_outside(int i) {
	--num_inside;
	press_button_cache = -1;
	move_outside(i);
}
int cur_h() {
	if (press_button_cache == -1) {
		press_button_cache = press_button();
		++num_queries;
	}
	return press_button_cache;
}


int min_cardinality(int N) {
	srand(2);
	n = N;
	at_h.resize(N+3);
	min_num_at_or_below_h.resize(N+2);
	max_num_at_or_below_h.resize(N+2);
	rep(i,0,N)
		v.push_back(i);
	random_shuffle(all(v));
	deque<int> remaining;
	int last_added = 0;
	for (int x : v) {
		if (num_queries > N*0.99 && last_added < N/2) {
			++num_queries;
			remaining.push_front(x);
			continue;
		}
		_move_inside(x);
		if (cur_h() > 1) {
			_move_outside(x);
			remaining.push_front(x);
		} else {
			at_h[1].push_back(x);
			last_added = num_queries;
		}
	}
	vector<int> min_count_inside(N, 1);
	int num_different = num_inside;
	h_hi = N/num_different;
	h_lo = 1;
	if (num_different == 1) {
		h_lo = N;
	}
	target_h = h_lo+1;
	bool start_binary_search = false;
	bool initial_search = true;
	target_h = 3;
	while (h_lo < h_hi) {
		max_num_at_or_below_h[0] = 0;
		min_num_at_or_below_h[0] = 0;
		vector<int> tmp(N+2);
		for (int x : remaining) {
			tmp[min_count_inside[x]]++;
		}
		rep(i,1,h_hi+1) {
			min_num_at_or_below_h[i] = min_num_at_or_below_h[i-1] + sz(at_h[i]);
			max_num_at_or_below_h[i] = max_num_at_or_below_h[i-1] + tmp[i-1];
			if (max_num_at_or_below_h[i]+num_inside < i*num_different) {
				h_hi = min(h_hi, i-1);
			}
			if (min_num_at_or_below_h[i] >= i*num_different) {
				assert(min_num_at_or_below_h[i] == i*num_different);
				h_lo = max(h_lo, i);
			}
		}
		h_lo = max(h_lo, H-(H*num_different-num_inside));
		//h_hi = min(h_hi, (num_inside+sz(remaining))/num_different);
		/*if (target_h > h_hi) {
			cerr << "Using binary search. target_h = " << target_h << " and h_hi = " << h_hi << endl;
			start_binary_search = true;
			target_h = (3*h_lo+h_hi+3)/4;
		}*/
		/*if (!start_binary_search && target_h*num_different < num_inside+num_different/2) {
			target_h++;
		}*/
		/*if (num_queries < 2*N && h_lo >= 1) {
			target_h = max(target_h, int(h_lo+(2*N-num_queries)*(h_lo-0.0)/num_queries));
		}*/
		/*if (!start_binary_search && target_h*num_different < num_inside+h_lo*2) {
			target_h++;
		}*/
		/*if (initial_search) {
			if (h_lo >= target_h) {
				target_h = target_h*2+1;
				if (target_h > h_hi) {
					initial_search = false;
				}
			}
		}
		if (!initial_search) {
			//target_h = (h_lo+h_hi)/2;
			target_h = h_hi;
		}*/
		if (target_h*num_different < num_inside+max(N/100, num_different)) {
			target_h++;
		}
		if (h_lo < 4 && last_added > N/5) {
			target_h = 4;
		}
		target_h = min(target_h, h_hi);
		target_h = max(target_h, h_lo+1);
		//debug();
		if (H > target_h) {
			for (int h = H; h > target_h; --h) {
				while (!at_h[h].empty()) {
					int x = at_h[h].front();
					at_h[h].pop_front();
					_move_outside(x);
					remaining.push_front(x);
				}
			}
			rep(i,0,N)
				min_count_inside[i] = min(min_count_inside[i], target_h);
			H = target_h;
			// assert(H == cur_h());
		}
		if (remaining.empty()) {
			return h_lo;
		}
		int x = remaining.front();
		remaining.pop_front();
		if (min_count_inside[x] >= target_h) {
			remaining.push_back(x);
		} else {
			_move_inside(x);
			if (cur_h() <= target_h) {
				H = cur_h();
				at_h[H].push_back(x);
			} else {
				min_count_inside[x] = H;
				remaining.push_back(x);
				_move_outside(x);
			}
		}
	}
	return h_lo;
}

Compilation message

insects.cpp: In function 'int min_cardinality(int)':
insects.cpp:98:7: warning: unused variable 'start_binary_search' [-Wunused-variable]
   98 |  bool start_binary_search = false;
      |       ^~~~~~~~~~~~~~~~~~~
insects.cpp:99:7: warning: unused variable 'initial_search' [-Wunused-variable]
   99 |  bool initial_search = true;
      |       ^~~~~~~~~~~~~~
# 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 0 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 2 ms 336 KB Output is correct
7 Correct 2 ms 384 KB Output is correct
8 Correct 4 ms 336 KB Output is correct
9 Correct 9 ms 336 KB Output is correct
10 Correct 7 ms 436 KB Output is correct
11 Correct 2 ms 336 KB Output is correct
12 Correct 5 ms 336 KB Output is correct
13 Correct 5 ms 336 KB Output is correct
14 Correct 4 ms 336 KB Output is correct
15 Correct 4 ms 336 KB Output is correct
16 Correct 7 ms 336 KB Output is correct
17 Correct 7 ms 336 KB Output is correct
18 Correct 5 ms 464 KB Output is correct
19 Correct 3 ms 424 KB Output is correct
20 Correct 5 ms 336 KB Output is correct
21 Correct 3 ms 336 KB Output is correct
22 Correct 3 ms 336 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 0 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 2 ms 336 KB Output is correct
7 Correct 2 ms 384 KB Output is correct
8 Correct 4 ms 336 KB Output is correct
9 Correct 9 ms 336 KB Output is correct
10 Correct 7 ms 436 KB Output is correct
11 Correct 2 ms 336 KB Output is correct
12 Correct 5 ms 336 KB Output is correct
13 Correct 5 ms 336 KB Output is correct
14 Correct 4 ms 336 KB Output is correct
15 Correct 4 ms 336 KB Output is correct
16 Correct 7 ms 336 KB Output is correct
17 Correct 7 ms 336 KB Output is correct
18 Correct 5 ms 464 KB Output is correct
19 Correct 3 ms 424 KB Output is correct
20 Correct 5 ms 336 KB Output is correct
21 Correct 3 ms 336 KB Output is correct
22 Correct 3 ms 336 KB Output is correct
23 Correct 7 ms 988 KB Output is correct
24 Correct 11 ms 984 KB Output is correct
25 Correct 28 ms 984 KB Output is correct
26 Correct 35 ms 988 KB Output is correct
27 Correct 24 ms 976 KB Output is correct
28 Correct 9 ms 984 KB Output is correct
29 Correct 30 ms 992 KB Output is correct
30 Correct 31 ms 988 KB Output is correct
31 Correct 31 ms 976 KB Output is correct
32 Correct 17 ms 992 KB Output is correct
33 Correct 27 ms 1108 KB Output is correct
34 Correct 22 ms 996 KB Output is correct
35 Correct 28 ms 976 KB Output is correct
36 Correct 33 ms 980 KB Output is correct
37 Correct 44 ms 976 KB Output is correct
38 Correct 23 ms 976 KB Output is correct
39 Correct 27 ms 976 KB Output is correct
40 Correct 14 ms 976 KB Output is correct
41 Correct 12 ms 976 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 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 20 ms 1688 KB Output is correct
8 Correct 10 ms 1680 KB Output is correct
9 Correct 65 ms 1692 KB Output is correct
10 Correct 83 ms 1688 KB Output is correct
11 Correct 67 ms 1680 KB Output is correct
12 Correct 23 ms 1684 KB Output is correct
13 Correct 65 ms 1692 KB Output is correct
14 Correct 61 ms 1688 KB Output is correct
15 Correct 60 ms 1680 KB Output is correct
16 Correct 60 ms 1676 KB Output is correct
17 Correct 40 ms 1672 KB Output is correct
18 Correct 70 ms 1684 KB Output is correct
19 Correct 62 ms 1676 KB Output is correct
20 Correct 72 ms 1688 KB Output is correct
21 Correct 82 ms 1668 KB Output is correct
22 Correct 81 ms 1684 KB Output is correct
23 Correct 66 ms 1692 KB Output is correct
24 Correct 43 ms 1684 KB Output is correct
25 Correct 33 ms 1684 KB Output is correct
26 Correct 16 ms 1688 KB Output is correct
27 Correct 51 ms 1660 KB Output is correct
28 Correct 50 ms 1672 KB Output is correct
29 Correct 74 ms 1672 KB Output is correct
30 Correct 58 ms 1664 KB Output is correct
31 Correct 77 ms 1672 KB Output is correct
32 Correct 62 ms 1664 KB Output is correct
33 Correct 66 ms 1676 KB Output is correct
34 Correct 54 ms 1664 KB Output is correct
35 Correct 47 ms 1664 KB Output is correct
36 Correct 56 ms 1656 KB Output is correct
37 Correct 72 ms 1656 KB Output is correct
38 Correct 77 ms 1744 KB Output is correct
39 Correct 60 ms 1688 KB Output is correct
40 Correct 60 ms 1672 KB Output is correct
41 Correct 63 ms 1676 KB Output is correct
42 Correct 59 ms 1696 KB Output is correct
43 Correct 13 ms 676 KB Output is correct
44 Correct 49 ms 1488 KB Output is correct
45 Correct 64 ms 1692 KB Output is correct
46 Correct 21 ms 1680 KB Output is correct
47 Correct 16 ms 1688 KB Output is correct
48 Correct 14 ms 1688 KB Output is correct