#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;
while (sz < 16)
{
tot++;
for (ll i = 0; i < sz and ptr < 31 and sz < 16; i++, ptr++)
sz += c[ptr] == 0;
if ((16 - sz) * 2 > 31 - ptr)
{
if (c[ptr] == 0)
sz++;
ptr++;
}
}
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;
bool ok = false;
while (fixed.size() < 16)
{
ll i;
for (i = 0; i < fixed.size() and ptr < 31; i++, ptr++)
{
msg[fixed[i]] = c[ptr];
if (c[ptr] == 0)
fixed.push_back(flg ? 30 - ptr : ptr);
}
if (i + 3 < fixed.size())
{
for (ll j = 0; j < 4; j++)
msg[fixed[i + j]] = a.size() >> j & 1;
ok = true;
}
if ((16 - fixed.size()) * 2 > 31 - ptr)
{
for (ll i = (flg ? 0 : ptr); i <= (flg ? 30 - ptr : 30); i++)
msg[i] = c[ptr];
if (c[ptr] == 0)
fixed.push_back(flg ? 30 - ptr : 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);
if (!ok)
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, ll l, ll r)
{
return count(c.begin() + l, c.begin() + r + 1, 1) * 2 > (r - l + 1);
}
vector<bool> receive_message(vector<vector<bool>> a)
{
vector<bool> c(31, 1);
bool flg = majority(a[0], 0, 30);
ll ptr = 0, cur = 1;
vector<ll> fixed;
bool ok = false;
ll rem = 0;
while (fixed.size() < 16)
{
ll i;
for (i = 0; i < fixed.size() and ptr < 31; i++, ptr++)
{
c[ptr] = a[cur][fixed[i]];
if (c[ptr] == 0)
fixed.push_back(flg ? 30 - ptr : ptr);
}
if (i + 3 < fixed.size())
{
for (ll j = 0; j < 4; j++)
rem += a[cur][fixed[i + j]] ? (1 << j) : 0;
ok = true;
}
if ((16 - fixed.size()) * 2 > 31 - ptr)
{
c[ptr] = majority(a[cur], flg ? 0 : ptr, flg ? 30 - ptr : 30);
if (c[ptr] == 0)
fixed.push_back(flg ? 30 - ptr : ptr);
ptr++;
}
cur++;
}
if (fixed.size() != 16)
exit(0);
ll sz = 0;
if (!ok)
{
for (ll i = 0; i < 16; i++)
if (a[cur][fixed[i]])
sz += 1 << i;
}
else
sz = (a.size() - cur - 1) << 4 | rem;
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... |