Submission #1298343

#TimeUsernameProblemLanguageResultExecution timeMemory
1298343sanduchicuMessage (IOI24_message)C++20
29.32 / 100
529 ms828 KiB
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>

std::string toBinary(int n) {
	if (n == 0) return "0";

	std::string result;
	while (n) {
		result += n%2 + 48;
		n /= 2;
	}
	std::reverse(result.begin(), result.end());
	return result;
}

int fromBinary(std::string S) {
	int result = 0;
	int multiple = 1;
	for (int i = S.size() - 1; i >= 0; i--) {
		if (S[i] - '0') result += multiple;
		multiple *= 2;
	}
	return result;
}

std::vector<bool> send_packet(std::vector<bool> A);

void send_message(std::vector<bool> M, std::vector<bool> C) {
	std::vector<int> offsets;
	for (int i = 0; i < C.size(); i++) {
		if (C[i] == 0) offsets.push_back(i);
		send_packet(std::vector<bool>(31, C[i]));
	}
	
	std::vector<bool> size_packet(31, 0);
	int i_size = 0;
	std::string size_string = toBinary(M.size());
	for (int i = offsets.size() - 1; i >= 0; i--) {
		size_packet[offsets[i]] = size_string[size_string.size() - i_size - 1] - 48;
		i_size++;
		if (i_size >= size_string.size()) break;
	}
	send_packet(size_packet);
	
	for (int i = 0; i < M.size();) {
		int j = std::min(16, (int)M.size() - i);
		std::vector<bool> message(31, 0);
		int i_message = 0;
		for (int offset : offsets) {
			message[offset] = M[i + i_message];
			if (++i_message >= j) break;
		}
		send_packet(message);
		i += j;
	}
}

std::vector<bool> receive_message(std::vector<std::vector<bool>> R) {
	std::vector<int> C;

	for (int i = 0; i < 31; i++) {
		int count = 0;
		for (bool c : R[i]) {
			if (c) count++;
			else count--;
		}
		if (count < 0) C.push_back(i);
	}

	std::string size_string;
	for (int offset : C) {
		size_string += R[31][offset] + 48;
	}
	int receiveSize = fromBinary(size_string);

	std::vector<bool> result;

	int i = 0;
	while (i < receiveSize) {
		result.push_back(R[i / 16 + 32][C[i % 16]]);
		i++;
	}
	return result;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...