이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "communication.h"
#include <bits/stdc++.h>
using namespace std;
//
// --- Sample implementation for the task communication ---
//
// To compile this program with the sample grader, place:
// communication.h communication_sample.cpp sample_grader.cpp
// in a single folder, then open the terminal in this directory (right-click onto an empty spot in the directory,
// left click on "Open in terminal") and enter e.g.:
// g++ -std=c++17 communication_sample.cpp sample_grader.cpp
// in this folder. This will create a file a.out in the current directory which you can execute from the terminal
// as ./a.out
// See task statement or sample_grader.cpp for the input specification
//
vector<vector<array<int, 2>>> getSmallMoves() {
const int SMALL = 100;
const int INF = 1e9;
vector<vector<int>> dp(SMALL, vector<int>(SMALL, INF));
vector<vector<array<int, 2>>> move(SMALL, vector<array<int, 2>>(SMALL));
for (int s = 0; s < SMALL; ++s)
for (int a = 0; a <= s; ++a) {
int b = s - a;
if (s <= 2) {
dp[a][b] = 0;
continue;
}
for (int a0 = 0; a0 <= a; ++a0)
for (int b0 = 0; b0 <= b; ++b0) {
if (a != a0 && b != b0) assert(dp[a0][b0] <= dp[a][b]);
// Note we ignore a' + b' == a + b and a' >= a to prevent cycles,
// but that's OK since it is not needed to consider those. (no proof, so dp[a][b] may not be optimal)
int v = max(dp[a0 + b0][a - a0], dp[s - a0 - b0][a0]) + 1;
if (v < dp[a][b]) {
dp[a][b] = v;
move[a][b] = {a0, b0};
}
}
assert(dp[a][b] < INF);
}
return move;
}
void encode(int N, int X) {
auto move = getSmallMoves();
vector<int> lastTrue, lastFalse;
for (int i = 1; i <= N; ++i) lastTrue.push_back(i);
while (lastTrue.size() + lastFalse.size() > 2) {
int a = lastTrue.size(), b = lastFalse.size();
int a0 = a / 2, b0 = b / 2;
if (a + b < move.size()) {
a0 = move[a][b][0];
b0 = move[a][b][1];
}
vector<int> S;
S.insert(S.end(), lastTrue.begin(), lastTrue.begin() + a0);
S.insert(S.end(), lastFalse.begin(), lastFalse.begin() + b0);
int bit = find(S.begin(), S.end(), X) != S.end();
int bitSent = send(bit);
if (bitSent == 1) {
lastFalse = vector<int>(lastTrue.begin() + a0, lastTrue.end());
lastTrue = S;
} else {
vector<int> T;
T.insert(T.end(), lastTrue.begin() + a0, lastTrue.end());
T.insert(T.end(), lastFalse.begin() + b0, lastFalse.end());
lastFalse = vector<int>(lastTrue.begin(), lastTrue.begin() + a0);
lastTrue = T;
}
}
}
pair<int, int> decode(int N) {
auto move = getSmallMoves();
vector<int> lastTrue, lastFalse;
for (int i = 1; i <= N; ++i) lastTrue.push_back(i);
while (lastTrue.size() + lastFalse.size() > 2) {
int a = lastTrue.size(), b = lastFalse.size();
int a0 = a / 2, b0 = b / 2;
if (a + b < move.size()) {
a0 = move[a][b][0];
b0 = move[a][b][1];
}
vector<int> S;
S.insert(S.end(), lastTrue.begin(), lastTrue.begin() + a0);
S.insert(S.end(), lastFalse.begin(), lastFalse.begin() + b0);
int bit = receive();
if (bit == 1) {
lastFalse = vector<int>(lastTrue.begin() + a0, lastTrue.end());
lastTrue = S;
} else {
vector<int> T;
T.insert(T.end(), lastTrue.begin() + a0, lastTrue.end());
T.insert(T.end(), lastFalse.begin() + b0, lastFalse.end());
lastFalse = vector<int>(lastTrue.begin(), lastTrue.begin() + a0);
lastTrue = T;
}
}
vector<int> S = lastTrue;
S.insert(S.end(), lastFalse.begin(), lastFalse.end());
assert(S.size() <= 2);
if (S.size() == 1) S.push_back(S[0]);
return {S[0], S[1]};
}
컴파일 시 표준 에러 (stderr) 메시지
communication.cpp: In function 'void encode(int, int)':
communication.cpp:51:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<std::array<int, 2> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
51 | if (a + b < move.size()) {
| ~~~~~~^~~~~~~~~~~~~
communication.cpp: In function 'std::pair<int, int> decode(int)':
communication.cpp:80:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<std::array<int, 2> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
80 | if (a + b < move.size()) {
| ~~~~~~^~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |