답안 #1098857

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1098857 2024-10-10T08:45:40 Z crafticat Flight to the Ford (BOI22_communication) C++17
0 / 100
538 ms 336 KB
#include <bits/stdc++.h>

using namespace std;
#define F0R(i, n) for (int i= 0; i < n;i++)
template<typename T>
using V = vector<T>;
using vi = V<int>;

int send(int bit);

int receive();
constexpr int TRIES = 10;

void sendArr(bitset<20> &bits) {
    F0R(i, 20)
        send(bits[i]);
}

bitset<20> readArr() {
    bitset<20> x;
    F0R(i, 20) {
        x[i] = receive();
    }
    return x;
}

void encode(int n, int x) {
    bitset<20> bits = x;

    F0R(i, TRIES) {
        sendArr(bits);
        send(0);
    }
}

std::pair<int, int> decode(int n) {
    vi count(20);
    F0R(i, TRIES) {
        bitset<20> input = readArr();
        F0R(j, 20) {
            count[j] += input[j];
        }
        receive();
    }

    bitset<20> ans;
    F0R(i, 20) {
        if (count[i] >= TRIES / 2) ans[i] = 1;
    }
    return {n, ans.to_ullong()};
}

#if DEBUG

using namespace std;

void __attribute__((noreturn)) __attribute__((format(printf, 1, 2))) result(const char *msg, ...)
{
    va_list args;
    va_start(args, msg);
    vfprintf(stdout, msg, args);
    fprintf(stdout, "\n");
    va_end(args);
    exit(0);
}

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 or (s != 0 and s != 1))
        result("Invalid send.");

    printf("send(%d) -> ", s); fflush(stdout);
    int answer = s;

    bool flipped_now = (s != answer);
    if(flipped and 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 or X < 1 or X > N)
        result("Invalid input.");

    current_phase = ENCODE;
    encode(N, X);
    current_phase = DECODE;
    auto r = decode(N);

    if(r.first < 1 or r.first > N or r.second < 1 or r.second > N)
        result("Invalid answer.");

    if(r.first == X or r.second == X)
        result("Correct: %d signals sent.", (int) signals.size());
    else
        result("Wrong answer.");
}
#endif
# 결과 실행 시간 메모리 Grader output
1 Incorrect 51 ms 336 KB Not correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 538 ms 332 KB Not correct
2 Halted 0 ms 0 KB -