#include "message.h"
#include <bits/stdc++.h>
#define endl "\n"
#define ll long long
using namespace std;
ll cost(vector<bool> &c)
{
ll tot = 0;
ll ptr = 0, sz = 0, first = 0;
while (c[first])
first++;
tot += 4;
sz = 1;
ptr = first + 1;
tot = 1;
while (ptr < 31 and sz < 16)
{
tot++;
ll tmp = sz;
for (ll i = 0; i < tmp and ptr < 31; i++, ptr++)
sz += c[ptr] == 0;
}
return tot;
}
void send_message(vector<bool> a, vector<bool> c)
{
vector<bool> msg(31, 0), revc = c;
reverse(revc.begin(), revc.end());
bool flg = cost(revc) < cost(c);
send_packet(vector<bool>(31, cost(revc) < cost(c)));
if (flg)
swap(revc, c);
vector<ll> fixed;
ll ptr = 0, first = 0;
while (c[first])
first++;
for (ll i = 0; i < 4; i++)
send_packet(vector<bool>(31, first >> i & 1));
fixed.push_back(flg ? 30 - first : first);
ptr = first + 1;
while (ptr < 31 and fixed.size() < 16)
{
ll sz = fixed.size();
for (ll i = 0; i < sz and ptr < 31; i++, ptr++)
{
msg[fixed[i]] = c[ptr];
if (c[ptr] == 0)
fixed.push_back(flg ? 30 - ptr : ptr);
}
send_packet(msg);
}
if (fixed.size() != 16)
exit(0);
for (ll i = 0; i < 16; i++)
msg[fixed[i]] = (a.size() >> i & 1);
send_packet(msg);
for (ll i = 0; i < a.size(); i += 16)
{
for (ll j = 0; j + i < a.size() and j < 16; j++)
msg[fixed[j]] = a[i + j];
send_packet(msg);
}
}
bool majority(vector<bool> &c)
{
return count(c.begin(), c.end(), 1) >= 16;
}
vector<bool> receive_message(vector<vector<bool>> a)
{
vector<bool> c(31, 1);
bool flg = majority(a[0]);
ll ptr = 0, cur = 1;
vector<ll> fixed;
fixed.push_back(0);
for (ll i = 0; i < 4; i++, cur++)
if (majority(a[cur]))
fixed.back() += 1 << i;
fixed.back() = flg ? 30 - fixed.back() : fixed.back();
ptr = fixed.back() + 1;
while (ptr < 31 and fixed.size() < 16)
{
ll sz = fixed.size();
for (ll i = 0; i < sz and ptr < 31; i++, ptr++)
{
c[ptr] = a[cur][fixed[i]];
if (c[ptr] == 0)
fixed.push_back(flg ? 30 - ptr : ptr);
}
cur++;
}
ll sz = 0;
for (ll i = 0; i < 16; i++)
if (a[cur][fixed[i]])
sz += 1 << i;
vector<bool> ans(sz);
for (ll i = 0; i < sz; i++)
ans[i] = a[cur + 1 + (i >> 4)][fixed[i & 15]];
return ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |