Submission #1210036

#TimeUsernameProblemLanguageResultExecution timeMemory
1210036trimkusMessage (IOI24_message)C++20
0 / 100
0 ms740 KiB
#include "message.h"
#include <bits/stdc++.h>
using namespace std;
const int LIMIT = 66;
void send_message(std::vector<bool> M, std::vector<bool> C) {
  std::vector<bool> A(31, 0);
  int N = (int)M.size();
  vector<int> dist(31, -1);
  for (int i = 0; i < 31; ++i) {
    if (C[i] == 0) {
      int d = 1;
      for (int j = (i + 1) % 31; C[j] == 1; j = (j + 1) % 31) {
        d++;
      }
      dist[i] = d;
    }
  }
  // cout << "DIST:" << endl;
  // for (int i = 0; i < 31; ++i) {
  //   cout << dist[i] << " ";
  // }
  // cout << endl;
  vector<bool> send[31];
  int ptr = (int)M.size() - 1024;
  for (int i = 0; i < 31; ++i) {
    // cout << "BEFORE = " << ptr;
    if (C[i] == 0) {
      while (dist[i] > 0) {
        send[i].push_back(1);
        dist[i]--;
      }
      if (ptr < -1) {
        ptr++; 
      }
      send[i].push_back(0);
      while (ptr < -1 && (int)send[i].size() < LIMIT) {
        send[i].push_back(0);
        ptr++;
      }
      while ((int)send[i].size() < LIMIT && ptr == -1) {
        send[i].push_back(1);
        ptr++;
        cerr << "STARTING AT " << i << " " << send[i].size() - 1 << "\n";
      }
      while (ptr < N && (int)send[i].size() < LIMIT) {
        send[i].push_back(M[ptr++]);
        cout << "NOW AT " << i << " " << send[i].size() - 1 << "\n";
      }
    }
    while ((int)send[i].size() < LIMIT) {
      // assert(C[i] != 0);
      send[i].insert(send[i].begin(), 0);
    }
    // cout << "AFTER = " << ptr << "\n";
  }
  cout << "PTR = " << ptr << ", wanted = " << N << "\n";
  // for (int i = 0; i < 31; ++i) {
  //   if (C[i] == 0 || C[i] == 1) {
  //     cout << setw(4) << left << i << " = ";
  //     for (auto u : send[i]) {
  //       cout << u << " ";
  //     }
  //     cout << endl;
  //   }
  // }
  // cout << "HERE!" << endl;
  for (int i = 0; i < 31; ++i) {
    // cout << i << endl;
    assert((int)send[i].size() == LIMIT);
    // cout << i << endl;
  }
  for (int i = 0; i < LIMIT; ++i) {
    for (int j = 0; j < 31; ++j) {
      A[j] = send[j][i];
    }
    send_packet(A);
  }
  // cout << "DONE SENDING..." << endl;
}

std::vector<bool> receive_message(std::vector<std::vector<bool>> R) {
  vector<bool> res;
  vector<int> nxt(31, -1);
  vector<bool> C[31];
  int N = (int)R.size();
  for (int i = 0; i < N; ++i) {
    for (int j = 0; j < 31; ++j) {
      // cout << R[i][j] << " ";
      C[j].push_back(R[i][j]);
    }
    // cout << endl;
  }
  // cout << endl;
  for (int i = 0; i < 31; ++i) {
    auto& v = C[i];
    // for (auto u : v) {
    //   cout << u << " ";
    // }
    // cout << endl;
    while (v.size() && v[0] == 0) {
      v.erase(v.begin());
    }
    if (!v.size()) continue;
    int dist = 0;
    while (v.size() && v[0] == 1) {
      dist++;
      v.erase(v.begin());
    }
    // cout << i << " = " << dist << "\n";
    if (v.size() == 0) continue;
    nxt[i] = (i + dist) % 31;
  }
  int start = -1;
  // for (int i = 0; i < 31; ++i) {
  //   cout << nxt[i] << " ";
  // }
  // cout << endl;
  for (int i = 0; start == -1 && i < 31; ++i) {
    if (nxt[i] == -1) continue;
    int j = i;
    int d = 0;
    while (d < 16 && j >= i) {
      d++;
      j = nxt[j];
      if (j == i) break;
    }
    if (d == 16 && j == i) {
      start = i;
    }
    // cout << i << " -> " << j << endl;
  }
  assert(start != -1);
  bool good[31];
  for (int i = 0; i < 31; ++i) good[i] = false;
  {
    int ni = start;
    good[ni] = true;
    ni = nxt[ni];
    while (ni != start) {
      good[ni] = true;
      ni = nxt[ni];
    }
  }
  // for (int i = 0; i < 31; ++i) {
  //   cout << good[i] << " ";
  // }
  // cout << endl;
  bool can = false;
  for (int i = 0; i < 31; ++i) {
    if (good[i]) {
      auto& v = C[i];
      int j = (nxt[i] - i + 31) % 31;
      bool popped = false;
      while (!can && (int)v.size() > 0) {
          if (v[0] == 1) {
            can = true;
            popped = true;
            cerr << "STARTING AT " << i << " " << j << "\n";
          }
          j++;
          v.erase(v.begin());
      }
      if (!popped) {
        v.erase(v.begin());
      }
      while ((int)v.size() > 0) {
        assert(can);
        cerr << "NOW AT " << i << " " << j << "\n";
        res.push_back(v[0]);
        v.erase(v.begin());
        j++;
      }
    }
  }
  return res;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...