Submission #760066

#TimeUsernameProblemLanguageResultExecution timeMemory
760066aryan12Counting Mushrooms (IOI20_mushrooms)C++17
91.87 / 100
8 ms340 KiB
#include "mushrooms.h"
#include <bits/stdc++.h>
using namespace std;

int count_mushrooms(int n)
{
	vector<int> a_type, b_type;
	a_type.push_back(0);
	int current_mushroom = 1;
	int answer = 0;
	const int brute_force = 60;
	while(current_mushroom < n)
	{
		if(max((int)a_type.size(), (int)b_type.size()) <= brute_force
			and max((int)a_type.size(), (int)b_type.size()) >= 2
			and current_mushroom != n - 1)
		{
			vector<int> query;
			if((int)a_type.size() >= 2)
			{
				query.push_back(a_type[0]);
				query.push_back(current_mushroom);
				query.push_back(a_type[1]);
				query.push_back(current_mushroom + 1);
				int response = use_machine(query);
				if(response >= 2)
				{
					b_type.push_back(current_mushroom);
				}
				else
				{
					a_type.push_back(current_mushroom);
				}
				if(response & 1)
				{
					b_type.push_back(current_mushroom + 1);
				}
				else
				{
					a_type.push_back(current_mushroom + 1);
				}
				current_mushroom += 2;
			}
			else if((int)b_type.size() >= 2)
			{
				query.push_back(b_type[0]);
				query.push_back(current_mushroom);
				query.push_back(b_type[1]);
				query.push_back(current_mushroom + 1);
				int response = use_machine(query);
				if(response >= 2)
				{
					a_type.push_back(current_mushroom);
				}
				else
				{
					b_type.push_back(current_mushroom);
				}
				if(response & 1)
				{
					a_type.push_back(current_mushroom + 1);
				}
				else
				{
					b_type.push_back(current_mushroom + 1);
				}
				current_mushroom += 2;
			}
			else
			{
				assert(false);
			}
			continue;
		}
		if(a_type.size() > b_type.size())
		{
			// use a_type.size()
			int cnt = min((int)a_type.size(), n - 1 - current_mushroom + 1);
			vector<int> query;
			for(int i = 0; i < cnt; i++)
			{
				query.push_back(a_type[i]);
				query.push_back(current_mushroom + i);
			}
			int response = use_machine(query);
			int last_one = current_mushroom + cnt - 1;
			if(response & 1) // last one is b_type
			{
				b_type.push_back(last_one);
				response -= 1;
			}
			else
			{
				a_type.push_back(last_one);
			}
			int total = query.size() - 2;
			answer += (total - response) / 2;
			current_mushroom += cnt;
		}
		else
		{
			int cnt = min((int)b_type.size(), n - 1 - current_mushroom + 1);
			vector<int> query;
			for(int i = 0; i < cnt; i++)
			{
				query.push_back(b_type[i]);
				query.push_back(current_mushroom + i);
			}
			int response = use_machine(query);
			int last_one = current_mushroom + cnt - 1;
			if(response & 1)
			{
				a_type.push_back(last_one);
				response -= 1;
			}
			else
			{
				b_type.push_back(last_one);
			}
			answer += response / 2;
			current_mushroom += cnt;
		}
	}
	return answer + (int)a_type.size();
}
#Verdict Execution timeMemoryGrader output
Fetching results...