#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define vec vector
string to_string(vec<bool> numbers) {
string text = "{";
for (auto i = numbers.begin(); i != numbers.end(); i++) {
text += to_string(*i);
if (next(i) != numbers.end()) {text += ", ";}
} text += '}';
return text;
}
string to_string(vec<vec<bool>> numbers) {
int j = 0;
string text = "{";
for (auto i = numbers.begin(); i != numbers.end(); i++) {
text += to_string(j) + ": " + to_string(*i);
if (next(i) != numbers.end()) {text += "\n";}
j++;
} text += '}';
return text;
}
vec<bool> receive_message(vec<vec<bool>> R) {
int leftbound = 0, rightbound = 30;
while (leftbound < rightbound) {
vec<bool> packet = R[0];
R.erase(R.begin());
int numofzeros = 0, numofones = 0;
for (int i = 0; i < 31; i++) {
if (packet[i] == 0) {numofzeros++;}
else {numofones++;}
}
int temp = (leftbound + rightbound) / 2;
if (numofzeros >= 16) {rightbound = temp;}
else {leftbound = temp + 1;}
} int index = leftbound;
vec<bool> C(31, 1);
if (index == 30) {C = {0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0};}
else {
C[index] = C[index + 1] = 0;
int indexofC = 0;
for (int i = 0; i < 15; i++) {
if (indexofC == index) {indexofC += 2;}
if (indexofC == index + 1) {indexofC++;}
if (indexofC < 31) {
C[indexofC] = R[i][index];
indexofC++;
} if (indexofC < 31) {
C[indexofC] = R[i][index + 1];
indexofC++;
}
}
}
vec<bool> packet = R[R.size() - 1];
R.erase(R.begin() + (R.size() - 1));
vec<bool> binary;
int i = 0;
for (int j = 0; j < 31; j++) {
if (C[j] == 0) {
binary.push_back(packet[j]);
i++;
} if (i == 11) {break;}
}
int poweroftwo = 1024;
int n = 0;
for (int j = 0; j < 11; j++) {
n += binary[j] * poweroftwo;
poweroftwo /= 2;
}
vec<bool> M;
int decoded = 0, ii = 0, jj = 0;
while (decoded < n) {
if (C[jj] == 0) {
if (ii <= 14 && index != 30) {
if (jj != index && jj != index + 1) {
M.push_back(R[ii][jj]);
decoded++;
}
} else {
M.push_back(R[ii][jj]);
decoded++;
}
} jj++;
if (jj == 31) {jj = 0; ii++;}
}
return M;
}
vec<bool> send_packet(vec<bool> A);
void send_message(vec<bool> M, vec<bool> C) {
int n = M.size(), index = 30;
for (int i = 0; i < 30; i++) {if (C[i] == 0 && C[i + 1] == 0) {index = i; break;}}
vec<vec<bool>> packets, newpackets;
int leftbound = 0, rightbound = 30;
while (leftbound < rightbound) {
int temp = (leftbound + rightbound) / 2, value;
if (index > temp) {
leftbound = temp + 1;
value = 1;
} else {
rightbound = temp;
value = 0;
} vec<bool> packet(31, value);
packets.push_back(packet);
}
for (int i = 0; i < 15; i++) {newpackets.push_back(vec<bool>(31, 0));}
if (index != 30) {
int indexofC = 0;
for (int i = 0; i < 15; i++) {
if (indexofC == index) {indexofC += 2;}
if (indexofC < 31) {
newpackets[i][index] = C[indexofC];
indexofC++;
} if (indexofC < 31) {
newpackets[i][index + 1] = C[indexofC];
indexofC++;
}
}
}
int encoded = 0, i = 0, j = 0;
while (encoded < n) {
if (C[j] == 0) {
if (i <= 14 && index != 30) {
if (j != index && j != index + 1) {
newpackets[i][j] = M[encoded];
encoded++;
}
} else {
newpackets[i][j] = M[encoded];
encoded++;
}
} j++;
if (j == 31) {
j = 0;
i++;
if (i >= 15) {newpackets.push_back(vec<bool>(31, 0));}
}
}
packets.insert(packets.end(), newpackets.begin(), newpackets.end());
vec<bool> binary;
int poweroftwo = 1024;
while (poweroftwo != 0) {
if (n >= poweroftwo) {
n -= poweroftwo;
binary.push_back(1);
} else {
binary.push_back(0);
} poweroftwo /= 2;
}
vec<bool> packet(31, 0);
i = 0;
for (int j = 0; j < 31; j++) {
if (C[j] == 0) {
packet[j] = binary[i];
i++;
} if (i == 11) {break;}
} packets.push_back(packet);
for (int k = 0; k < packets.size(); k++) {
send_packet(packets[k]);
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |