Submission #388303

# Submission time Handle Problem Language Result Execution time Memory
388303 2021-04-10T20:25:50 Z JimmyZJX Counting Mushrooms (IOI20_mushrooms) C++14
100 / 100
11 ms 348 KB
#include <cassert>
#include <vector>

using namespace std;

#define forR(i, n) for (int i = 0; i < (n); i++)

int use_machine(vector<int> x);

int use_machine_safe(vector<int> x) {
	int cnt = 0;

	for (int i : x) for (int j : x) {
		if (i == j) cnt++;
	}

	assert(cnt == x.size());

	return use_machine(x);
}

int count_mushrooms(int n) {
	if (n < 400) {
		int cntA = 1;
		for (int i = 1; i < n; i++) {
			if (use_machine(vector<int>{0, i}) == 0)
				cntA++;
		}
		return cntA;
	}

	vector<int> As, Bs; As.push_back(0);

	int i = 1;
	while (As.size() < 2 && Bs.size() < 2) {
		int m = use_machine(vector<int>{0, i});
		if (m == 0) As.push_back(i);
		else Bs.push_back(i);
		i++;
	}

	while (i <= 200) {
		auto as = &As, bs = &Bs;
		if (As.size() < Bs.size()) swap(as, bs);

		if (as->size() >= 4 && bs->size() >= 2) {
			// smart: 5/4 m
			int a1 = (*as)[0], a2 = (*as)[1], a3 = (*as)[2], a4 = (*as)[3];
			int b1 = (*bs)[0], b2 = (*bs)[1];
			int i1 = i, i2 = i + 1, i3 = i + 2, i4 = i + 3, i5 = i + 4;
			int c1, c2, c3, c4, c5;

			int m1 = use_machine(vector<int>{ i1, a1, i2, a2, i3, a3, i4, a4, i5 });

			if (m1 % 2 == 0) {
				// c1 == c5
				int m2 = use_machine(vector<int>{ a1, i1, a2, i5, a3, i2, a4, i3 });
				c1 = c5 = (m2 & 4) > 0;
				c3 = (m2 & 1) > 0;
				c2 = (m2 & 2) > 0;
			} else {
				// c1 != c5
				int m2 = use_machine(vector<int>{ b1, i1, b2, a2, i5, a3, i2, a4, i3 });
				m2--; // b2 - a2
				c5 = (m2 & 4) > 0; c1 = 1 - c5;
				c3 = (m2 & 1) > 0;
				c2 = (m2 & 2) > 0;
			}

			// c4 is unknown
			c4 = (m1 - c1 - c5) / 2 - c2 - c3;

			vector<int> colors{ c1,c2,c3,c4,c5 };
			forR(j, colors.size()) {
				if (colors[j] == 0) {
					as->push_back(i + j);
				} else {
					bs->push_back(i + j);
				}
			}

			i += 5;

		} else {
			// normal: 2m
			int m = use_machine(vector<int>{ (*as)[0], i, (*as)[1], i + 1 });

			if ((m & 2) == 0) {
				as->push_back(i);
			} else {
				bs->push_back(i);
			}

			if ((m & 1) == 0) {
				as->push_back(i + 1);
			} else {
				bs->push_back(i + 1);
			}

			i += 2;
		}
	}

	int cntA = 0;
	while (i < n) {
		int rest = n - i;
		auto as = &As, bs = &Bs;
		if (As.size() < Bs.size()) swap(as, bs); // as is larger

		int l = min(rest, (int)as->size());
		vector<int> test;
		for (int j = 0; j < l; j++) {
			test.push_back((*as)[j]);
			test.push_back(i + j);
		}
		int m = use_machine(test);

		if ((m & 1) == 0) {
			as->push_back(test.back());
		} else {
			bs->push_back(test.back());
		}

		int cnt_b = m / 2;
		int cnt_a = l - 1 - cnt_b;
		if ((*as)[0] == 0) {
			cntA += cnt_a;
		} else {
			cntA += cnt_b;
		}

		i += l;
	}

	return As.size() + cntA;
}

Compilation message

In file included from /usr/include/c++/9/cassert:44,
                 from mushrooms.cpp:2:
mushrooms.cpp: In function 'int use_machine_safe(std::vector<int>)':
mushrooms.cpp:18:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   18 |  assert(cnt == x.size());
      |         ~~~~^~~~~~~~~~~
mushrooms.cpp: In function 'int count_mushrooms(int)':
mushrooms.cpp:7:38: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    7 | #define forR(i, n) for (int i = 0; i < (n); i++)
      |                                      ^
mushrooms.cpp:75:4: note: in expansion of macro 'forR'
   75 |    forR(j, colors.size()) {
      |    ^~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 200 KB Output is correct
2 Correct 0 ms 200 KB Output is correct
3 Correct 0 ms 200 KB Output is correct
4 Correct 1 ms 200 KB Output is correct
5 Correct 3 ms 200 KB Output is correct
6 Correct 2 ms 328 KB Output is correct
7 Correct 7 ms 200 KB Output is correct
8 Correct 6 ms 300 KB Output is correct
9 Correct 6 ms 304 KB Output is correct
10 Correct 8 ms 200 KB Output is correct
11 Correct 9 ms 312 KB Output is correct
12 Correct 8 ms 200 KB Output is correct
13 Correct 7 ms 280 KB Output is correct
14 Correct 6 ms 300 KB Output is correct
15 Correct 8 ms 296 KB Output is correct
16 Correct 8 ms 200 KB Output is correct
17 Correct 5 ms 200 KB Output is correct
18 Correct 7 ms 200 KB Output is correct
19 Correct 9 ms 200 KB Output is correct
20 Correct 8 ms 200 KB Output is correct
21 Correct 10 ms 204 KB Output is correct
22 Correct 8 ms 300 KB Output is correct
23 Correct 7 ms 200 KB Output is correct
24 Correct 6 ms 276 KB Output is correct
25 Correct 11 ms 200 KB Output is correct
26 Correct 8 ms 200 KB Output is correct
27 Correct 8 ms 200 KB Output is correct
28 Correct 8 ms 200 KB Output is correct
29 Correct 8 ms 308 KB Output is correct
30 Correct 8 ms 296 KB Output is correct
31 Correct 9 ms 296 KB Output is correct
32 Correct 7 ms 200 KB Output is correct
33 Correct 8 ms 200 KB Output is correct
34 Correct 8 ms 200 KB Output is correct
35 Correct 8 ms 292 KB Output is correct
36 Correct 7 ms 304 KB Output is correct
37 Correct 8 ms 304 KB Output is correct
38 Correct 8 ms 200 KB Output is correct
39 Correct 6 ms 348 KB Output is correct
40 Correct 11 ms 200 KB Output is correct
41 Correct 9 ms 200 KB Output is correct
42 Correct 8 ms 200 KB Output is correct
43 Correct 7 ms 200 KB Output is correct
44 Correct 7 ms 200 KB Output is correct
45 Correct 9 ms 292 KB Output is correct
46 Correct 8 ms 200 KB Output is correct
47 Correct 8 ms 292 KB Output is correct
48 Correct 8 ms 200 KB Output is correct
49 Correct 8 ms 200 KB Output is correct
50 Correct 6 ms 308 KB Output is correct
51 Correct 9 ms 312 KB Output is correct
52 Correct 9 ms 200 KB Output is correct
53 Correct 10 ms 200 KB Output is correct
54 Correct 9 ms 292 KB Output is correct
55 Correct 7 ms 200 KB Output is correct
56 Correct 9 ms 304 KB Output is correct
57 Correct 8 ms 200 KB Output is correct
58 Correct 8 ms 200 KB Output is correct
59 Correct 8 ms 300 KB Output is correct
60 Correct 8 ms 200 KB Output is correct
61 Correct 10 ms 200 KB Output is correct
62 Correct 0 ms 200 KB Output is correct
63 Correct 0 ms 200 KB Output is correct
64 Correct 0 ms 200 KB Output is correct
65 Correct 0 ms 200 KB Output is correct
66 Correct 0 ms 200 KB Output is correct
67 Correct 0 ms 200 KB Output is correct
68 Correct 0 ms 200 KB Output is correct
69 Correct 0 ms 200 KB Output is correct
70 Correct 0 ms 200 KB Output is correct
71 Correct 0 ms 200 KB Output is correct
72 Correct 0 ms 200 KB Output is correct
73 Correct 0 ms 200 KB Output is correct
74 Correct 0 ms 200 KB Output is correct
75 Correct 0 ms 200 KB Output is correct
76 Correct 0 ms 204 KB Output is correct
77 Correct 0 ms 200 KB Output is correct
78 Correct 0 ms 200 KB Output is correct
79 Correct 0 ms 204 KB Output is correct
80 Correct 0 ms 200 KB Output is correct
81 Correct 0 ms 200 KB Output is correct
82 Correct 0 ms 200 KB Output is correct
83 Correct 0 ms 200 KB Output is correct
84 Correct 0 ms 200 KB Output is correct
85 Correct 0 ms 200 KB Output is correct
86 Correct 0 ms 200 KB Output is correct
87 Correct 0 ms 200 KB Output is correct
88 Correct 0 ms 200 KB Output is correct
89 Correct 0 ms 200 KB Output is correct
90 Correct 0 ms 200 KB Output is correct
91 Correct 0 ms 200 KB Output is correct