Submission #461205

# Submission time Handle Problem Language Result Execution time Memory
461205 2021-08-09T13:57:31 Z denniskim Counting Mushrooms (IOI20_mushrooms) C++14
100 / 100
9 ms 432 KB
//mushrooms.cpp
#include "mushrooms.h"
#include <bits/stdc++.h>

using namespace std;
typedef int ll;
typedef double ld;
#define MAX 9223372036854775807LL
#define MIN -9223372036854775808LL

int count_mushrooms(int n)
{
	if(n <= 200)
	{
		ll ans = 1;
		vector<ll> tmp;
		
		for(ll i = 1 ; i < n - 1 ; i += 2)
		{
			tmp.clear();
			tmp.push_back(i + 1);
			tmp.push_back(0);
			tmp.push_back(i);
			ans += 2 - use_machine(tmp);
		}
		
		if(n % 2 == 0)
		{
			tmp.clear();
			tmp.push_back(0);
			tmp.push_back(n - 1);
			ans += 1 - use_machine(tmp);
		}
		
		return ans;
	}
	
	//0 ~ 4
	
	vector<ll> A, B;
	vector<ll> tmp;
	
	tmp.push_back(0);
	tmp.push_back(1);
	
	A.push_back(0);
	
	if(use_machine(tmp) == 1)
		B.push_back(1);
	else
		A.push_back(1);
	
	tmp.clear();
	tmp.push_back(0);
	tmp.push_back(2);
	
	if(use_machine(tmp) == 1)
		B.push_back(2);
	else
		A.push_back(2);
	
	if(A.size() >= 2)
	{
		tmp.clear();
		tmp.push_back(A[0]);
		tmp.push_back(3);
		tmp.push_back(A[1]);
		tmp.push_back(4);
		
		ll gap = use_machine(tmp);
		
		if(gap % 2 == 0)
			A.push_back(4);
		else
			B.push_back(4);
		
		if(gap / 2 == 0)
			A.push_back(3);
		else
			B.push_back(3);
	}
	
	else
	{
		tmp.clear();
		tmp.push_back(B[0]);
		tmp.push_back(3);
		tmp.push_back(B[1]);
		tmp.push_back(4);
		
		ll gap = use_machine(tmp);
		
		if(gap % 2 == 0)
			B.push_back(4);
		else
			A.push_back(4);
		
		if(gap / 2 == 0)
			B.push_back(3);
		else
			A.push_back(3);
	}
	
	ll buck = 45;
	
	for(ll i = 1 ; i < buck ; i++)
	{
		ll bun1 = 5 * i;
		ll bun2 = 5 * i + 1;
		ll bun3 = 5 * i + 2;
		ll bun4 = 5 * i + 3;
		ll bun5 = 5 * i + 4;
		
		if(A.size() >= 3)
		{
			tmp.clear();
			tmp.push_back(A[0]);
			tmp.push_back(bun1);
			tmp.push_back(A[1]);
			tmp.push_back(bun2);
			tmp.push_back(A[2]);
			tmp.push_back(bun3);
			
			ll gap = use_machine(tmp);
			
			if(gap < 2)
			{
				//bun1 = 0, bun2 = 0
				A.push_back(bun1);
				A.push_back(bun2);
				
				if(gap % 2 == 0)
					A.push_back(bun3);
				else
					B.push_back(bun3);
				
				tmp.clear();
				tmp.push_back(A[0]);
				tmp.push_back(bun4);
				tmp.push_back(A[1]);
				tmp.push_back(bun5);
				
				gap = use_machine(tmp);
				
				if(gap % 2 == 0)
					A.push_back(bun5);
				else
					B.push_back(bun5);
				
				if(gap / 2 == 0)
					A.push_back(bun4);
				else
					B.push_back(bun4);
			}
			
			else if(gap >= 4)
			{
				//bun1 = 1, bun2 = 1
				B.push_back(bun1);
				B.push_back(bun2);
				
				if(gap % 2 == 0)
					A.push_back(bun3);
				else
					B.push_back(bun3);
				
				tmp.clear();
				tmp.push_back(A[0]);
				tmp.push_back(bun4);
				tmp.push_back(A[1]);
				tmp.push_back(bun5);
				
				gap = use_machine(tmp);
				
				if(gap % 2 == 0)
					A.push_back(bun5);
				else
					B.push_back(bun5);
				
				if(gap / 2 == 0)
					A.push_back(bun4);
				else
					B.push_back(bun4);
			}
			
			else
			{
				//bun1 != bun2, bun3 OK
				
				if(gap % 2 == 0)
					A.push_back(bun3);
				else
					B.push_back(bun3);
				
				if(B.size() < 2)
				{
					tmp.clear();
					tmp.push_back(A[0]);
					tmp.push_back(bun1);
					tmp.push_back(A[1]);
					tmp.push_back(bun2);
					
					ll gapp = use_machine(tmp);
					
					if(gapp % 2 == 0)
						A.push_back(bun2);
					else
						B.push_back(bun2);
					
					if(gapp / 2 == 0)
						A.push_back(bun1);
					else
						B.push_back(bun1);
					
					tmp.clear();
					tmp.push_back(A[0]);
					tmp.push_back(bun4);
					tmp.push_back(A[1]);
					tmp.push_back(bun5);
					
					gapp = use_machine(tmp);
					
					if(gapp % 2 == 0)
						A.push_back(bun5);
					else
						B.push_back(bun5);
					
					if(gapp / 2 == 0)
						A.push_back(bun4);
					else
						B.push_back(bun4);
				}
				
				else
				{
					tmp.clear();
					tmp.push_back(B[0]);
					tmp.push_back(bun1);
					tmp.push_back(B[1]);
					tmp.push_back(A[0]);
					tmp.push_back(bun2);
					tmp.push_back(A[1]);
					tmp.push_back(bun4);
					tmp.push_back(A[2]);
					tmp.push_back(bun5);
					
					ll gapp = use_machine(tmp) - 1;
					
					if(gapp % 2 == 0)
						A.push_back(bun5);
					else
						B.push_back(bun5), gapp--;
					
					if(gapp == 0)
					{
						B.push_back(bun1);
						A.push_back(bun2);
						A.push_back(bun4);
					}
					
					else if(gapp == 2)
					{
						B.push_back(bun1);
						A.push_back(bun2);
						B.push_back(bun4);
					}
					
					else if(gapp == 4)
					{
						A.push_back(bun1);
						B.push_back(bun2);
						A.push_back(bun4);
					}
					
					else if(gapp == 6)
					{
						A.push_back(bun1);
						B.push_back(bun2);
						B.push_back(bun4);
					}
				}
			}
		}
		
		else if(B.size() >= 3)
		{
			tmp.clear();
			tmp.push_back(B[0]);
			tmp.push_back(bun1);
			tmp.push_back(B[1]);
			tmp.push_back(bun2);
			tmp.push_back(B[2]);
			tmp.push_back(bun3);
			
			ll gap = use_machine(tmp);
			
			if(gap < 2)
			{
				//bun1 = 1, bun2 = 1
				B.push_back(bun1);
				B.push_back(bun2);
				
				if(gap % 2 == 0)
					B.push_back(bun3);
				else
					A.push_back(bun3);
				
				tmp.clear();
				tmp.push_back(B[0]);
				tmp.push_back(bun4);
				tmp.push_back(B[1]);
				tmp.push_back(bun5);
				
				gap = use_machine(tmp);
				
				if(gap % 2 == 0)
					B.push_back(bun5);
				else
					A.push_back(bun5);
				
				if(gap / 2 == 0)
					B.push_back(bun4);
				else
					A.push_back(bun4);
			}
			
			else if(gap >= 4)
			{
				//bun1 = 0, bun2 = 0
				A.push_back(bun1);
				A.push_back(bun2);
				
				if(gap % 2 == 0)
					B.push_back(bun3);
				else
					A.push_back(bun3);
				
				tmp.clear();
				tmp.push_back(B[0]);
				tmp.push_back(bun4);
				tmp.push_back(B[1]);
				tmp.push_back(bun5);
				
				gap = use_machine(tmp);
				
				if(gap % 2 == 0)
					B.push_back(bun5);
				else
					A.push_back(bun5);
				
				if(gap / 2 == 0)
					B.push_back(bun4);
				else
					A.push_back(bun4);
			}
			
			else
			{
				//bun1 != bun2, bun3 OK
				
				if(gap % 2 == 0)
					B.push_back(bun3);
				else
					A.push_back(bun3);
				
				if(A.size() < 2)
				{
					tmp.clear();
					tmp.push_back(B[0]);
					tmp.push_back(bun1);
					tmp.push_back(B[1]);
					tmp.push_back(bun2);
					
					ll gapp = use_machine(tmp);
					
					if(gapp % 2 == 0)
						B.push_back(bun2);
					else
						A.push_back(bun2);
					
					if(gapp / 2 == 0)
						B.push_back(bun1);
					else
						A.push_back(bun1);
					
					tmp.clear();
					tmp.push_back(B[0]);
					tmp.push_back(bun4);
					tmp.push_back(B[1]);
					tmp.push_back(bun5);
					
					gapp = use_machine(tmp);
					
					if(gapp % 2 == 0)
						B.push_back(bun5);
					else
						A.push_back(bun5);
					
					if(gapp / 2 == 0)
						B.push_back(bun4);
					else
						A.push_back(bun4);
				}
				
				else
				{
					tmp.clear();
					tmp.push_back(A[0]);
					tmp.push_back(bun1);
					tmp.push_back(A[1]);
					tmp.push_back(B[0]);
					tmp.push_back(bun2);
					tmp.push_back(B[1]);
					tmp.push_back(bun4);
					tmp.push_back(B[2]);
					tmp.push_back(bun5);
					
					ll gapp = use_machine(tmp) - 1;
					
					if(gapp % 2 == 0)
						B.push_back(bun5);
					else
						A.push_back(bun5), gapp--;
					
					if(gapp == 0)
					{
						A.push_back(bun1);
						B.push_back(bun2);
						B.push_back(bun4);
					}
					
					else if(gapp == 2)
					{
						A.push_back(bun1);
						B.push_back(bun2);
						A.push_back(bun4);
					}
					
					else if(gapp == 4)
					{
						B.push_back(bun1);
						A.push_back(bun2);
						B.push_back(bun4);
					}
					
					else if(gapp == 6)
					{
						B.push_back(bun1);
						A.push_back(bun2);
						A.push_back(bun4);
					}
				}
			}
		}
	}
	
	ll gaet = 0;
	ll num = 0;
	ll ans = A.size();
	
	for(ll i = 5 * buck ; i < n ; i += gaet)
	{
		if(A.size() >= B.size())
		{
			gaet = A.size();
			num = 0;
		}
		
		else
		{
			gaet = B.size();
			num = 1;
		}
		
		tmp.clear();
		ll siz = gaet;
		ll cc = 0;
		
		if(i + siz > n)
			siz = n - i;
		
		for(ll j = i ; j < i + siz ; j++)
		{
			if(num == 0)
				tmp.push_back(A[cc++]);
			else
				tmp.push_back(B[cc++]);
			
			tmp.push_back(j);
		}
		
		ll gappp = use_machine(tmp);
		
		if(num == 0)
			ans += siz - (gappp + 1) / 2;
		else
			ans += (gappp + 1) / 2;
		
		if(num == 0)
		{
			if(gappp % 2 == 0)
				A.push_back(i + siz - 1);
			else
				B.push_back(i + siz - 1);
		}
		
		else
		{
			if(gappp % 2 == 0)
				B.push_back(i + siz - 1);
			else
				A.push_back(i + siz - 1);
		}
	}
	
	return ans;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 200 KB Output is correct
2 Correct 0 ms 200 KB Output is correct
3 Correct 1 ms 200 KB Output is correct
4 Correct 0 ms 200 KB Output is correct
5 Correct 2 ms 200 KB Output is correct
6 Correct 2 ms 200 KB Output is correct
7 Correct 6 ms 200 KB Output is correct
8 Correct 8 ms 200 KB Output is correct
9 Correct 7 ms 200 KB Output is correct
10 Correct 9 ms 200 KB Output is correct
11 Correct 8 ms 200 KB Output is correct
12 Correct 6 ms 200 KB Output is correct
13 Correct 8 ms 284 KB Output is correct
14 Correct 5 ms 200 KB Output is correct
15 Correct 8 ms 200 KB Output is correct
16 Correct 7 ms 312 KB Output is correct
17 Correct 4 ms 200 KB Output is correct
18 Correct 9 ms 296 KB Output is correct
19 Correct 7 ms 200 KB Output is correct
20 Correct 7 ms 200 KB Output is correct
21 Correct 8 ms 200 KB Output is correct
22 Correct 7 ms 312 KB Output is correct
23 Correct 8 ms 200 KB Output is correct
24 Correct 5 ms 200 KB Output is correct
25 Correct 7 ms 432 KB Output is correct
26 Correct 8 ms 200 KB Output is correct
27 Correct 7 ms 308 KB Output is correct
28 Correct 8 ms 200 KB Output is correct
29 Correct 6 ms 312 KB Output is correct
30 Correct 7 ms 200 KB Output is correct
31 Correct 7 ms 200 KB Output is correct
32 Correct 8 ms 312 KB Output is correct
33 Correct 9 ms 200 KB Output is correct
34 Correct 8 ms 200 KB Output is correct
35 Correct 8 ms 200 KB Output is correct
36 Correct 9 ms 200 KB Output is correct
37 Correct 7 ms 200 KB Output is correct
38 Correct 7 ms 200 KB Output is correct
39 Correct 7 ms 200 KB Output is correct
40 Correct 9 ms 200 KB Output is correct
41 Correct 7 ms 200 KB Output is correct
42 Correct 7 ms 200 KB Output is correct
43 Correct 7 ms 308 KB Output is correct
44 Correct 8 ms 200 KB Output is correct
45 Correct 8 ms 308 KB Output is correct
46 Correct 8 ms 200 KB Output is correct
47 Correct 9 ms 304 KB Output is correct
48 Correct 8 ms 200 KB Output is correct
49 Correct 8 ms 200 KB Output is correct
50 Correct 9 ms 200 KB Output is correct
51 Correct 8 ms 200 KB Output is correct
52 Correct 8 ms 200 KB Output is correct
53 Correct 6 ms 308 KB Output is correct
54 Correct 7 ms 200 KB Output is correct
55 Correct 7 ms 200 KB Output is correct
56 Correct 8 ms 200 KB Output is correct
57 Correct 9 ms 304 KB Output is correct
58 Correct 7 ms 200 KB Output is correct
59 Correct 9 ms 200 KB Output is correct
60 Correct 8 ms 200 KB Output is correct
61 Correct 8 ms 200 KB Output is correct
62 Correct 0 ms 200 KB Output is correct
63 Correct 1 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 1 ms 200 KB Output is correct
70 Correct 1 ms 200 KB Output is correct
71 Correct 0 ms 200 KB Output is correct
72 Correct 1 ms 200 KB Output is correct
73 Correct 1 ms 200 KB Output is correct
74 Correct 1 ms 200 KB Output is correct
75 Correct 1 ms 200 KB Output is correct
76 Correct 0 ms 200 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 200 KB Output is correct
80 Correct 1 ms 200 KB Output is correct
81 Correct 1 ms 200 KB Output is correct
82 Correct 0 ms 200 KB Output is correct
83 Correct 1 ms 200 KB Output is correct
84 Correct 1 ms 200 KB Output is correct
85 Correct 0 ms 200 KB Output is correct
86 Correct 1 ms 200 KB Output is correct
87 Correct 1 ms 200 KB Output is correct
88 Correct 1 ms 200 KB Output is correct
89 Correct 1 ms 200 KB Output is correct
90 Correct 1 ms 200 KB Output is correct
91 Correct 0 ms 200 KB Output is correct