#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]};
}
Compilation message
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()) {
| ~~~~~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
925 ms |
2092 KB |
Output is correct |
2 |
Correct |
1330 ms |
2024 KB |
Output is correct |
3 |
Correct |
1846 ms |
1972 KB |
Output is correct |
4 |
Correct |
904 ms |
2032 KB |
Output is correct |
5 |
Correct |
1365 ms |
1968 KB |
Output is correct |
6 |
Correct |
3769 ms |
2236 KB |
Output is correct |
7 |
Correct |
7032 ms |
2044 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Memory limit exceeded |
130 ms |
118784 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |