답안 #752730

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
752730 2023-06-03T14:03:36 Z jakobrs Flight to the Ford (BOI22_communication) C++17
68 / 100
4994 ms 2848 KB
#include <array>
#include <iostream>

using i64 = int64_t;

int send(int s);
int receive();

constexpr const i64 FIB[32] = {
    1,      2,      3,      5,      8,      13,      21,      34,
    55,     89,     144,    233,    377,    610,     987,     1597,
    2584,   4181,   6765,   10946,  17711,  28657,   46368,   75025,
    121393, 196418, 317811, 514229, 832040, 1346269, 2178309, 3524578};

// 252, 149, 8, 104, 154
// ['11111100', '10010101', '00001000', '01101000', '10011010']
constexpr const int STRINGS[5][8] = {
    {1, 1, 1, 1, 1, 1, 0, 0}, {1, 0, 0, 1, 0, 1, 0, 1},
    {0, 0, 0, 0, 1, 0, 0, 0}, {0, 1, 1, 0, 1, 0, 0, 0},
    {1, 0, 0, 1, 1, 0, 1, 0},
};
constexpr const std::pair<int, int> STRINGS_BACKWARDS[256] = {
    {1, 2}, {1, 2}, {2, 0}, {0, 1}, {1, 0}, {1, 0}, {0, 1}, {1, 0}, {2, 4},
    {2, 0}, {2, 4}, {4, 0}, {2, 0}, {2, 0}, {4, 0}, {4, 0}, {1, 4}, {1, 0},
    {4, 0}, {4, 0}, {1, 0}, {1, 0}, {0, 1}, {1, 0}, {2, 4}, {2, 0}, {2, 4},
    {4, 0}, {1, 2}, {1, 2}, {4, 0}, {1, 4}, {2, 3}, {2, 3}, {2, 3}, {0, 1},
    {0, 1}, {0, 1}, {0, 1}, {0, 1}, {2, 3}, {2, 3}, {2, 3}, {0, 1}, {2, 3},
    {2, 3}, {0, 1}, {0, 1}, {1, 4}, {1, 0}, {4, 0}, {4, 0}, {1, 0}, {1, 0},
    {0, 1}, {1, 0}, {3, 4}, {3, 0}, {3, 4}, {4, 0}, {1, 3}, {1, 3}, {4, 0},
    {1, 4}, {2, 3}, {2, 3}, {2, 3}, {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1},
    {2, 3}, {2, 3}, {2, 3}, {0, 1}, {2, 3}, {2, 3}, {0, 1}, {0, 1}, {0, 1},
    {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 2}, {0, 2},
    {2, 0}, {0, 1}, {0, 2}, {0, 2}, {0, 1}, {0, 1}, {3, 0}, {3, 0}, {3, 0},
    {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 3}, {0, 3}, {3, 0}, {0, 1},
    {0, 3}, {0, 3}, {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1},
    {0, 1}, {0, 1}, {0, 1}, {0, 3}, {0, 3}, {3, 0}, {0, 1}, {0, 3}, {0, 3},
    {0, 1}, {0, 1}, {1, 2}, {1, 2}, {2, 0}, {0, 1}, {1, 0}, {1, 0}, {0, 1},
    {1, 0}, {2, 4}, {2, 0}, {2, 4}, {4, 0}, {2, 0}, {2, 0}, {4, 0}, {4, 0},
    {1, 4}, {1, 0}, {4, 0}, {4, 0}, {1, 0}, {1, 0}, {0, 1}, {1, 0}, {2, 4},
    {2, 0}, {2, 4}, {4, 0}, {1, 2}, {1, 2}, {4, 0}, {1, 4}, {2, 0}, {2, 0},
    {2, 0}, {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 2}, {0, 2}, {2, 0},
    {0, 1}, {0, 2}, {0, 2}, {0, 1}, {0, 1}, {1, 4}, {1, 0}, {4, 0}, {4, 0},
    {0, 1}, {0, 1}, {0, 1}, {1, 0}, {0, 4}, {0, 1}, {4, 0}, {4, 0}, {0, 1},
    {0, 1}, {0, 4}, {1, 4}, {1, 3}, {1, 3}, {3, 0}, {0, 1}, {1, 0}, {1, 0},
    {0, 1}, {1, 0}, {3, 4}, {3, 0}, {3, 4}, {4, 0}, {3, 0}, {3, 0}, {4, 0},
    {4, 0}, {1, 4}, {1, 0}, {4, 0}, {4, 0}, {0, 1}, {0, 1}, {0, 1}, {1, 0},
    {0, 4}, {0, 1}, {4, 0}, {4, 0}, {0, 1}, {0, 1}, {0, 4}, {1, 4}, {3, 0},
    {3, 0}, {3, 0}, {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 3}, {0, 3},
    {3, 0}, {0, 1}, {0, 3}, {0, 3}, {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1},
    {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 3}, {0, 3}, {3, 0}, {0, 1},
    {0, 3}, {0, 3}, {0, 1}, {0, 1}};

void encode(int n, int x) {
    x -= 1;
    if (n <= 5) {
        for (i64 i = 0; i < 8; i++) {
            send(STRINGS[x][i]);
        }
    } else {
        i64 error = 0;

        for (i64 i = 0; 1 << i < n; i++) {
            int value = x & (1 << i) ? 1 : 0;
            std::cerr << value << '\n';
            if (send(value) != value) {
                error += FIB[i];
            }
        }

        i64 bits = 0;
        while (1 << bits < n) bits += 1;

        encode(FIB[bits], error + 1);
    }
}

int decompress(int n) {
    int result = 0;

    for (i64 i = 31; i >= 0; i--) {
        if (n >= FIB[i]) {
            n -= FIB[i];
            result |= 1 << i;
        }
    }

    return result;
}

std::pair<int, int> decode(int n) {
    if (n <= 5) {
        i64 read = 0;
        for (i64 i = 0; i < 8; i++) {
            read |= receive() << (7 - i);
        }
        auto [x, y] = STRINGS_BACKWARDS[read];
        std::cerr << x << ' ' << y << '\n';
        return {x + 1, std::min(y + 1, n)};
    } else {
        int message = 0;

        for (i64 i = 0; 1 << i < n; i++) {
            message |= receive() << i;
        }

        i64 bits = 0;
        while (1 << bits < n) bits += 1;

        auto [corr1, corr2] = decode(FIB[bits]);
        auto [x, y] = std::array<int, 2>{message ^ decompress(corr1 - 1),
                                         message ^ decompress(corr2 - 1)};

        return {std::min(x + 1, n), std::min(y + 1, n)};
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 1672 KB Output is correct
2 Correct 18 ms 1696 KB Output is correct
3 Correct 27 ms 1680 KB Output is correct
4 Correct 12 ms 1708 KB Output is correct
5 Correct 15 ms 1760 KB Output is correct
6 Correct 41 ms 1800 KB Output is correct
7 Correct 69 ms 1768 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Partially correct 1050 ms 1840 KB Output is partially correct
2 Partially correct 470 ms 1828 KB Output is partially correct
3 Partially correct 710 ms 1924 KB Output is partially correct
4 Partially correct 1253 ms 1976 KB Output is partially correct
5 Partially correct 1001 ms 2068 KB Output is partially correct
6 Partially correct 681 ms 1824 KB Output is partially correct
7 Partially correct 3046 ms 2516 KB Output is partially correct
8 Partially correct 4994 ms 2736 KB Output is partially correct
9 Partially correct 4298 ms 2628 KB Output is partially correct
10 Partially correct 4442 ms 2624 KB Output is partially correct
11 Partially correct 4426 ms 2816 KB Output is partially correct
12 Partially correct 4445 ms 2704 KB Output is partially correct
13 Partially correct 4358 ms 2848 KB Output is partially correct
14 Partially correct 4477 ms 2728 KB Output is partially correct
15 Partially correct 2555 ms 2260 KB Output is partially correct
16 Partially correct 4819 ms 2600 KB Output is partially correct
17 Partially correct 964 ms 1984 KB Output is partially correct
18 Partially correct 1092 ms 2116 KB Output is partially correct
19 Partially correct 1232 ms 2116 KB Output is partially correct
20 Partially correct 1157 ms 2000 KB Output is partially correct
21 Partially correct 1142 ms 2156 KB Output is partially correct
22 Partially correct 1261 ms 2176 KB Output is partially correct
23 Partially correct 2051 ms 2060 KB Output is partially correct
24 Correct 17 ms 1696 KB Output is correct
25 Correct 16 ms 1704 KB Output is correct
26 Correct 17 ms 1716 KB Output is correct
27 Correct 16 ms 1744 KB Output is correct
28 Correct 22 ms 1700 KB Output is correct
29 Correct 39 ms 1940 KB Output is correct
30 Correct 45 ms 1672 KB Output is correct