#include<bits/stdc++.h>
using namespace std;
#include"communication.h"
//
// --- 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
//
typedef pair<int,int> pi;
pair<int,int> sendtrit(int X){
int a,b,c;
if (X==0){
a=send(0);b=send(0);c=send(0);
} else if (X==1){
a=send(1);b=send(1);c=send(1);
} else if (X==2){
a = send(0);
if (a==1){
b=send(1);c=send(1);
} else {
b=send(0);c=send(1);
}
}
pi ans[8] = {{0,2},{0,2},{0,1},{1,2},{0,0},{0,1},{1,2},{1,2}};
return ans[(a<<2)+(b<<1)+c];
}
pair<int,int> recvtrit(){
pi ans[8] = {{0,2},{0,2},{0,1},{1,2},{0,0},{0,1},{1,2},{1,2}};
return ans[(receive() << 2) + (receive() << 1) + (receive())];
}
void encode(int N, int X) {
int sz = ceil(log(N+1)/log(3));
int act[sz];
pi tr[sz];
int j = X;
for (int i=0;i<sz;i++){
act[i] = j%3;
tr[i] = sendtrit(j%3);
j/=3;
}
int A=tr[0].first,B=tr[0].second;
bool A_actual = A==act[0];
int pow = 1;
for (int i=1;i<sz;i++){
pow*=3;
int c = tr[i].first,d = tr[i].second;
int opt1[] = {c*pow+A,d*pow+A,c*pow+B,d*pow+B};
int actual = opt1[A_actual ? d==act[i] : 2 + (d==act[i])];
pi curr;
if (actual == opt1[0] || actual == opt1[1]){
curr = sendtrit(0);
} else if (actual == opt1[2]){
curr = sendtrit(1);
} else if (actual == opt1[3]){
curr = sendtrit(2);
}
vector<int> opt2;
if (curr.first == 0){
opt2.push_back(opt1[0]);
opt2.push_back(opt1[1]);
} else {
opt2.push_back(opt1[1+curr.first]);
}
opt2.push_back(opt1[1+curr.second]);
if (opt2.size() == 2){
A = opt2[0];
B = opt2[1];
A_actual = (A==actual);
} else {
if (actual==opt2[0]){
curr = sendtrit(0);
} else if (actual==opt2[1]){
curr = sendtrit(1);
} else {
curr = sendtrit(2);
}
A = opt2[curr.first];
B = opt2[curr.second];
A_actual = (A==actual);
}
}
}
std::pair<int, int> decode(int N) {
int sz = ceil(log(N+1)/log(3));
pi tr[sz];
for (int i=0;i<sz;i++){
tr[i] = recvtrit();
}
int A=tr[0].first,B=tr[0].second;
int pow = 1;
for (int i=1;i<sz;i++){
pow *= 3;
int c = tr[i].first,d = tr[i].second;
int opt1[] = {c*pow+A,d*pow+A,c*pow+B,d*pow+B};
pi curr = recvtrit();
vector<int> opt2;
if (curr.first == 0){
opt2.push_back(opt1[0]);
opt2.push_back(opt1[1]);
} else {
opt2.push_back(opt1[1+curr.first]);
}
opt2.push_back(opt1[1+curr.second]);
if (opt2.size()==2){
A = opt2[0];
B = opt2[0];
} else {
curr = recvtrit();
A = opt2[curr.first];
B = opt2[curr.second];
}
}
A = max(1,A);
A = min(N,A);
B = max(1,B);
B = min(N,B);
return {A,B};
}
Compilation message
communication.cpp: In function 'std::pair<int, int> sendtrit(int)':
communication.cpp:33:18: warning: 'a' may be used uninitialized in this function [-Wmaybe-uninitialized]
33 | return ans[(a<<2)+(b<<1)+c];
| ~~^~~~
communication.cpp:33:29: warning: 'c' may be used uninitialized in this function [-Wmaybe-uninitialized]
33 | return ans[(a<<2)+(b<<1)+c];
| ~~~~~~~~~~~~~^~
communication.cpp:33:25: warning: 'b' may be used uninitialized in this function [-Wmaybe-uninitialized]
33 | return ans[(a<<2)+(b<<1)+c];
| ~~^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
2940 KB |
Output is correct |
2 |
Correct |
18 ms |
2760 KB |
Output is correct |
3 |
Correct |
20 ms |
2720 KB |
Output is correct |
4 |
Correct |
8 ms |
2740 KB |
Output is correct |
5 |
Correct |
15 ms |
2736 KB |
Output is correct |
6 |
Correct |
24 ms |
2792 KB |
Output is correct |
7 |
Correct |
62 ms |
2720 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
353 ms |
352 KB |
Not correct |
2 |
Halted |
0 ms |
0 KB |
- |