답안 #668216

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
668216 2022-12-03T10:29:17 Z Sohsoh84 Flight to the Ford (BOI22_communication) C++17
0 / 100
235 ms 252 KB
#include"communication.h"
#include <bits/stdc++.h>
//
// --- Sample implementation for the task communication ---
//
// To compile this program with the sample grader, place:
//     communication.h communication_sample.cpp sample_grader.cpp
// in a single folder, then open the terminal in this directory (right-click onto an empty spot in the directory,
// left click on "Open in terminal") and enter e.g.:
//     g++ -std=c++17 communication_sample.cpp sample_grader.cpp
// in this folder. This will create a file a.out in the current directory which you can execute from the terminal
// as ./a.out
// See task statement or sample_grader.cpp for the input specification

using namespace std;

const int LOG = 30;

int f(int x) {
	return 31 - __builtin_clz(x);
}

void encode(int N, int X) {
	int ans1 = 0, ans2 = 1, lst = -1;
	for (int i = 1; i < LOG; i++) {
		if (ans2 < ans1) swap(ans1, ans2);

		if (ans1 == ans2) {
			ans2 ^= (1 << i);
			continue;
		}

		int x = f(ans1 ^ ans2);
		int a = (lst >= 0 ? lst : send((X >> x) & 1)), b = send((X >> i) & 1), c = send((X >> i) & 1);
		lst = -1;
		if (b == c) {
			ans1 ^= (b << i);
			ans2 ^= (b << i);
			continue;
		}

		int d = send((X >> x) & 1);
		if (a == d) {
			if (d) ans1 = ans2;
			else ans2 = ans1;
			ans2 ^= (1 << i);
			continue;
		}
	
		lst = d;
		int tans1 = ((d ? ans2 : ans1) ^ (b << i)); // 0101
		int tans2 = ((a ? ans2 : ans1) ^ (c << i)); // 1010
		ans1 = tans1, ans2 = tans2;
	}
}

pair<int, int> decode(int N) {
	int ans1 = 0, ans2 = 1, lst = -1;
	for (int i = 1; i < LOG; i++) {
		if (ans2 < ans1) swap(ans1, ans2);

		if (ans1 == ans2) {
			ans2 ^= (1 << i);
			continue;
		}

		int x = f(ans1 ^ ans2);
		int a = (lst >= 0 ? lst : receive()), b = receive(), c = receive();
		lst = -1;
		if (b == c) {
			ans1 ^= (b << i);
			ans2 ^= (b << i);
			continue;
		}

		int d = receive();
		if (a == d) {
			if (d) ans1 = ans2;
			else ans2 = ans1;
			ans2 ^= (1 << i);
			continue;
		}
	
		lst = d;
		int tans1 = ((d ? ans2 : ans1) ^ (b << i)); // 0101
		int tans2 = ((a ? ans2 : ans1) ^ (c << i)); // 1010
		ans1 = tans1, ans2 = tans2;
	}

	if (ans1 <= 0 || ans1 > N) ans1 = 1;
	if (ans2 <= 0 || ans2 > N) ans2 = 1;
	return {ans1, ans2};
}

Compilation message

communication.cpp: In function 'std::pair<int, int> decode(int)':
communication.cpp:67:7: warning: unused variable 'x' [-Wunused-variable]
   67 |   int x = f(ans1 ^ ans2);
      |       ^
# 결과 실행 시간 메모리 Grader output
1 Incorrect 19 ms 252 KB Not correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 235 ms 200 KB Not correct
2 Halted 0 ms 0 KB -