제출 #1356860

#제출 시각아이디문제언어결과실행 시간메모리
1356860yogesh_saneData Transfer (IOI19_transfer)C++20
100 / 100
43 ms1724 KiB
#include "transfer.h"
#include <vector>
#include <numeric>
#include <cmath>

using namespace std;

/**
 * Calculates how many bits are needed to represent the indices of the source.
 * N = 63 needs 6 bits (2^6 = 64)
 * N = 255 needs 8 bits (2^8 = 256)
 */
int get_checksum_size(int n) {
    return (n <= 63) ? 6 : 8;
}

vector<int> get_attachment(vector<int> source) {
    int n = source.size();
    int k = get_checksum_size(n);
    vector<int> attachment;

    // 1. Calculate Global Parity (Is the total number of 1s even or odd?)
    int total_sum = accumulate(source.begin(), source.end(), 0);
    attachment.push_back(total_sum % 2);

    // 2. Calculate XOR Checksum (The "address" of the 1s)
    int xor_sum = 0;
    for (int i = 0; i < n; i++) {
        if (source[i] == 1) {
            xor_sum ^= (i + 1); // Use 1-based indexing so 0 doesn't disappear
        }
    }

    // 3. Convert XOR sum to individual bits (Binary representation)
    // We iterate from most significant bit to least significant
    for (int bit = k - 1; bit >= 0; bit--) {
        int bit_value = (xor_sum >> bit) & 1;
        attachment.push_back(bit_value);
    }

    return attachment;
}

vector<int> retrieve(vector<int> data) {
    // Determine N and K from the total size received
    // The attachment was 1 (parity) + K (checksum) bits
    int k = (data.size() <= 70) ? 6 : 8;
    int n = data.size() - k - 1;

    // 1. Extract the sent data and the checksum parts
    vector<int> message;
    for (int i = 0; i < n; i++) {
        message.push_back(data[i]);
    }

    int sent_parity = data[n];
    
    int sent_xor_sum = 0;
    for (int i = 0; i < k; i++) {
        int bit_value = data[n + 1 + i];
        // Reconstruct the integer from the bits
        if (bit_value == 1) {
            sent_xor_sum |= (1 << (k - 1 - i));
        }
    }

    // 2. Calculate what the parity and XOR sum SHOULD be based on received message
    int actual_parity = accumulate(message.begin(), message.end(), 0) % 2;
    int actual_xor_sum = 0;
    for (int i = 0; i < n; i++) {
        if (message[i] == 1) {
            actual_xor_sum ^= (i + 1);
        }
    }

    // 3. Error Detection Logic
    bool parity_matches = (actual_parity == sent_parity);
    bool xor_matches = (actual_xor_sum == sent_xor_sum);

    // If parity matches, either there's no error or the error is in the attachment bits.
    // If XOR matches, the message is clean.
    if (parity_matches || xor_matches) {
        return message;
    }

    // 4. Error Correction
    // If both parity and XOR differ, a bit in the message flipped.
    // The XOR of the 'sent' and 'actual' sums gives the exact position of the flip.
    int error_index = (actual_xor_sum ^ sent_xor_sum) - 1;
    
    // Safety check: only flip if the index is within the message range
    if (error_index >= 0 && error_index < n) {
        message[error_index] = 1 - message[error_index]; // Flip 0 to 1 or 1 to 0
    }

    return message;
}

컴파일 시 표준 에러 (stderr) 메시지

grader.cpp: In instantiation of 'void shuffle(std::vector<T>&) [with T = Scenario]':
grader.cpp:200:10:   required from here
grader.cpp:28:23: warning: 'void std::random_shuffle(_RAIter, _RAIter) [with _RAIter = __gnu_cxx::__normal_iterator<Scenario*, vector<Scenario> >]' is deprecated: use 'std::shuffle' instead [-Wdeprecated-declarations]
   28 |         random_shuffle(v.begin(), v.end());
      |         ~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/13/algorithm:61,
                 from grader.cpp:8:
/usr/include/c++/13/bits/stl_algo.h:4581:5: note: declared here
 4581 |     random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last)
      |     ^~~~~~~~~~~~~~
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…