Submission #800592

#TimeUsernameProblemLanguageResultExecution timeMemory
800592QwertyPiCounting Mushrooms (IOI20_mushrooms)C++14
96.58 / 100
6 ms304 KiB
#include "mushrooms.h"

std::vector<int> a[2];
int b[2];

int id, n;
void query_1(){
	if(id >= n) return;
	std::vector<int> m {0, id};
	int c = use_machine(m);
	a[c].push_back(id);
	id++;
}

void query_2a(){
	if(id >= n) return;
	if(a[0].size() <= 1 && a[1].size() <= 1 || id == n - 1){ query_1(); return; }
	int majority = a[0].size() >= a[1].size() ? 0 : 1;
	std::vector<int> m = {a[majority][0], id, a[majority][1], id + 1};
	int c = use_machine(m);
	id += 2;
	a[majority ^ (c / 2)].push_back(m[1]);
	a[majority ^ (c % 2)].push_back(m[3]);
}

void query_2b(){
	if(id >= n) return;
	if(a[0].size() <= 3 && a[1].size() <= 3 || n - id <= 7){ query_2a(); return; }
	int majority = a[0].size() >= a[1].size() ? 0 : 1;
	std::vector<int> x {id + 3, id + 4, id + 5, id + 6};
	std::vector<int> m1{id + 0, a[majority][0], x[0], a[majority][1], x[1], a[majority][2], x[2], a[majority][3]};
	std::vector<int> m2{id + 1, a[majority][0], x[0], a[majority][1], x[1], a[majority][2], x[3], a[majority][3]};
	std::vector<int> m3{id + 2, a[majority][0], x[0], a[majority][1], x[2], a[majority][2], x[3], a[majority][3]};
	int c1 = use_machine(m1), c2 = use_machine(m2), c3 = use_machine(m3);
	a[majority ^ (c1 % 2)].push_back(id + 0);
	a[majority ^ (c2 % 2)].push_back(id + 1);
	a[majority ^ (c3 % 2)].push_back(id + 2);
	for(int mask = 0; mask < 16; mask++){
		std::vector<int> c;
		for(int j = 0; j < 4; j++){
			c.push_back((mask & (1 << j)) > 0);
		}
		if(c[0] + c[1] + c[2] == c1 / 2 && c[0] + c[1] + c[3] == c2 / 2 && c[0] + c[2] + c[3] == c3 / 2){
			for(int j = 0; j < 4; j++){
				a[majority ^ c[j]].push_back(x[j]);
			}
		}
	}
	id += 7;
}

void query_3(){
	if(id >= n) return;
	if(a[0].size() <= 1 && a[1].size() <= 1){ query_1(); return; }
	int majority = a[0].size() >= a[1].size() ? 0 : 1;
	int k = a[majority].size(); k = std::min(k, n - id);
	std::vector<int> m;
	for(int i = id; i < id + k; i++){
		m.push_back(a[majority][i - id]); m.push_back(i);
	}
	int c = use_machine(m);
	id += k;
	a[majority ^ (c % 2)].push_back(m.back());
	b[majority ^ 1] += c / 2;
	b[majority] += (k - 1) - c / 2;
}

int count_mushrooms(int N) {
	a[0].clear(); a[1].clear(); b[0] = b[1] = 0;
	n = N; a[0].push_back(0); id = 1;
	if(n <= 1000){
		while(id < n) query_3();
		return a[0].size() + b[0];
	}
	const int SQ = 250;
	while(id < SQ) query_2b();
	while(id < n) query_3();
	return a[0].size() + b[0];
}

Compilation message (stderr)

mushrooms.cpp: In function 'void query_2a()':
mushrooms.cpp:17:22: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
   17 |  if(a[0].size() <= 1 && a[1].size() <= 1 || id == n - 1){ query_1(); return; }
      |     ~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~
mushrooms.cpp: In function 'void query_2b()':
mushrooms.cpp:28:22: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
   28 |  if(a[0].size() <= 3 && a[1].size() <= 3 || n - id <= 7){ query_2a(); return; }
      |     ~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...