#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 (ptr < 31 and sz < 16)
{
ll bal = 31 - ptr - (16 - sz);
if (bal < (16 - sz))
{
if (c[ptr] == 0)
sz++;
ptr++;
}
tot++;
ll tmp = sz;
for (ll i = 0; i < tmp and ptr < 31 and sz < 16; 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;
while (ptr < 31 and fixed.size() < 16)
{
ll bal = 31 - ptr - (16 - fixed.size());
if (bal < 16 - fixed.size())
{
msg = vector<bool>(31, c[ptr]);
if (c[ptr] == 0)
fixed.push_back(ptr);
ptr++;
}
ll sz = fixed.size();
for (ll i = 0; i < sz and ptr < 31 and fixed.size() < 16; 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, ll ind)
{
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], 0);
ll ptr = 0, cur = 1;
vector<ll> fixed;
while (ptr < 31 and fixed.size() < 16)
{
ll bal = 31 - ptr - (16 - fixed.size());
if (bal < 16 - fixed.size())
{
if (majority(a[cur], ptr) == 0)
fixed.push_back(ptr);
ptr++;
}
ll sz = fixed.size();
for (ll i = 0; i < sz and ptr < 31 and fixed.size() < 16; 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... |