Submission #1291790

#TimeUsernameProblemLanguageResultExecution timeMemory
1291790lucas110550Message (IOI24_message)C++20
Compilation error
0 ms0 KiB
#include <vector>
#include <array>
#include <algorithm>
#include <cstdint>
#include <cassert>
#include "message.h"

using Packet = std::array<int, 31>;


void send_message(const std::vector<int>& M, const std::array<int, 31>& C) {
    const int S = static_cast<int>(M.size());
    const int P = (S + 15) / 16; // Number of message packets

    // fixed_indices = sorted([i for i in range(31) if C[i] == 0])
    std::vector<int> fixed_indices;
    fixed_indices.reserve(31);
    for (int i = 0; i < 31; ++i) {
        if (C[i] == 0) fixed_indices.push_back(i);
    }
    std::sort(fixed_indices.begin(), fixed_indices.end());
    assert(static_cast<int>(fixed_indices.size()) >= 16 && "Need at least 16 fixed positions");

    // Send P data packets
    for (int k = 0; k < P; ++k) {
        Packet packet{}; // zero-initialized
        const int start = 16 * k;
        for (int j = 0; j < 16; ++j) {
            const int pos = start + j;
            packet[ fixed_indices[j] ] = (pos < S) ? M[pos] : 0;
        }
        send_packet(packet);
    }

    // Send extra packet 0
    {
        Packet packet0{};
        for (int j = 0; j < 11; ++j) {
            int bit = (S >> j) & 1;
            packet0[ fixed_indices[j] ] = bit;
        }
        for (int j = 11; j < 16; ++j) {
            packet0[ fixed_indices[j] ] = 1;
        }
        send_packet(packet0);
    }

    // Send extra packet 1
    {
        Packet packet1{};
        for (int j = 0; j < 11; ++j) {
            int bit = (S >> j) & 1;
            packet1[ fixed_indices[j] ] = 1 - bit;
        }
        for (int j = 11; j < 16; ++j) {
            packet1[ fixed_indices[j] ] = 0;
        }
        send_packet(packet1);
    }
}

std::vector<int> receive_message(const std::vector<Packet>& R) {
    std::vector<int> M;
    if (R.size() < 2) return M; // not enough packets

    const Packet& extra0 = R[R.size() - 2];
    const Packet& extra1 = R[R.size() - 1];

    // fixed_indices: positions where extra0 and extra1 differ
    std::vector<int> fixed_indices;
    fixed_indices.reserve(31);
    for (int i = 0; i < 31; ++i) {
        if (extra0[i] != extra1[i]) fixed_indices.push_back(i);
    }
    std::sort(fixed_indices.begin(), fixed_indices.end());

    // Recover S (message length)
    int S = 0;
    for (int j = 0; j < 11; ++j) {
        if (j < static_cast<int>(fixed_indices.size())) {
            int bit = extra0[ fixed_indices[j] ];
            S |= ((bit & 1) << j);
        } else {
            break;
        }
    }

    const int P = (S + 15) / 16;
    M.reserve(S);

    for (int k = 0; k < P; ++k) {
        if (k >= static_cast<int>(R.size())) break; // not enough packets provided
        const Packet& packet = R[k];
        const int start = 16 * k;
        if (start >= S) break;

        for (int j = 0; j < 16; ++j) {
            const int pos = start + j;
            if (pos >= S) break;
            // Assumes fixed_indices has at least 16 entries (as produced by sender)
            int bit = packet[ fixed_indices[j] ];
            M.push_back(bit);
        }
    }

    return M;
}

Compilation message (stderr)

message.cpp: In function 'void send_message(const std::vector<int>&, const std::array<int, 31>&)':
message.cpp:32:21: error: could not convert 'packet' from 'Packet' {aka 'std::array<int, 31>'} to 'std::vector<bool>'
   32 |         send_packet(packet);
      |                     ^~~~~~
      |                     |
      |                     Packet {aka std::array<int, 31>}
message.cpp:45:21: error: could not convert 'packet0' from 'Packet' {aka 'std::array<int, 31>'} to 'std::vector<bool>'
   45 |         send_packet(packet0);
      |                     ^~~~~~~
      |                     |
      |                     Packet {aka std::array<int, 31>}
message.cpp:58:21: error: could not convert 'packet1' from 'Packet' {aka 'std::array<int, 31>'} to 'std::vector<bool>'
   58 |         send_packet(packet1);
      |                     ^~~~~~~
      |                     |
      |                     Packet {aka std::array<int, 31>}