#include"communication.h"
#include <vector>
#include <algorithm>
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
//
void encode(int N, int X) {
while(N > 0){
N /= 2;
send(x%2);
send(x%2);
send(x%2);
x /= 2;
}
}
vector<int> a, res;
void brute_force(int x, int i, bool f){
if(i*3 == a.size()){
res.push_back(x);
return;
}int j = i*3;
if(f){
if(a[j] != a[j+1] && a[j] != a[j+2])
return;
brute_force(x * 2 + a[i*3], i+1, a[i*3] == a[i*3+2]);
return;
}
if(a[j] == a[j+1])
brute_force(x * 2 + a[j], i+1, a[j] == a[j+2]);
else if(a[j+1] == a[j+2])
brute_force(x * 2 + a[j+1], i+1, true);
else{
brute_force(x*2+a[j], i+1, a[j] == a[j+2]);
brute_force(x*2+a[j+1], i+1, a[j+1] == a[j+2]);
}
}
std::pair<int, int> decode(int N) {
a.clear();
while(N > 0){
N /= 2;
a.push_back(receive());
a.push_back(receive());
a.push_back(receive());
}
res.clear();
brute_force(0, 0, 0);
sort(res.begin(), res.end());
if(res[0] == 0) res.erase(res.begin());
if(res.size() == 1) return {res[0], res[0]};
int i = rnd() % res.size();
while((j = rnd() % res.size()) != i);
return {res[i], res[j]};
}
Compilation message
communication.cpp: In function 'void encode(int, int)':
communication.cpp:20:14: error: 'x' was not declared in this scope
20 | send(x%2);
| ^
communication.cpp: In function 'void brute_force(int, int, bool)':
communication.cpp:28:12: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
28 | if(i*3 == a.size()){
| ~~~~^~~~~~~~~~~
communication.cpp: In function 'std::pair<int, int> decode(int)':
communication.cpp:62:13: error: 'rnd' was not declared in this scope; did you mean 'rand'?
62 | int i = rnd() % res.size();
| ^~~
| rand
communication.cpp:63:12: error: 'j' was not declared in this scope
63 | while((j = rnd() % res.size()) != i);
| ^
communication.cpp:64:25: error: 'j' was not declared in this scope
64 | return {res[i], res[j]};
| ^
communication.cpp:64:27: error: could not convert '{res.std::vector<int>::operator[](((std::vector<int>::size_type)i)), <expression error>}' from '<brace-enclosed initializer list>' to 'std::pair<int, int>'
64 | return {res[i], res[j]};
| ^
| |
| <brace-enclosed initializer list>