# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
1099035 | LIA | Flight to the Ford (BOI22_communication) | C++17 | 0 ms | 0 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <cstdarg>
#include <cassert>
#include "communication.h"
void __attribute__((noreturn)) __attribute__((format(printf, 1, 2))) result(const char *msg, ...);
void encode(int N, int X);
pair<int, int> decode(int N);
namespace {
enum { ENCODE, DECODE } current_phase;
int N, X;
vector<int> signals;
size_t cursor = 0;
bool flipped = false;
}
int send(int s) {
if (current_phase == DECODE || (s != 0 && s != 1))
result("Invalid send.");
printf("send(%d) -> ", s); fflush(stdout);
int answer;
if (scanf("%d", &answer) != 1 || (answer != 0 && answer != 1))
result("Invalid reply to send.");
bool flipped_now = (s != answer);
if (flipped && flipped_now)
result("Invalid reply to send");
flipped = flipped_now;
signals.push_back(answer);
if (signals.size() > (size_t)250)
result("Looks (and smells) fishy.");
return signals.back();
}
int receive() {
if (current_phase == ENCODE) result("Invalid receive.");
if (cursor >= signals.size()) result("Assistant waiting for Godot.");
int r = signals[cursor++];
printf("receive() -> %d\n", r);
return r;
}
int main() {
if (scanf("%d %d", &N, &X) != 2 || X < 1 || X > N)
result("Invalid input.");
current_phase = ENCODE;
encode(N, X);
current_phase = DECODE;
auto r = decode(N);
if (r.first < 1 || r.first > N || r.second < 1 || r.second > N)
result("Invalid answer.");
if (r.first == X || r.second == X)
result("Correct: %d signals sent.", (int)signals.size());
else
result("Wrong answer.");
}
void encode(int N, int X) {
if (X == 1) {
send(0);
send(1);
} else if (X == 2) {
send(1);
send(0);
} else if (X == 3) {
send(1);
send(1);
}
}
pair<int, int> decode(int N) {
int first_bit = receive();
int second_bit = receive();
if (first_bit == 0 && second_bit == 1) {
return {1, 1};
} else if (first_bit == 1 && second_bit == 0) {
return {2, 2};
} else if (first_bit == 1 && second_bit == 1) {
return {3, 3};
}
return {1, 2};
}