Submission #880106

#TimeUsernameProblemLanguageResultExecution timeMemory
880106AkibAzmainMutating DNA (IOI21_dna)C++17
100 / 100
80 ms6348 KiB
#include <bits/stdc++.h>
using namespace std;
#include "dna.h"

int n;
vector < array < int, 6 > > bit;

int
toi (char x, char y)
{
  if (y > x) --y;
  return (x - 'A') * 2 + y - 'A';
}

auto
bs (int i, int x)
{
  int ans = 0;
  while (x >= 0)
    {
      ans += bit[x][i];
      x -= (x + 1) & -(x + 1);
    }
  return ans;
}

auto
brs (int i, int l, int r)
{
  int ans = bs (i, r);
  if (l) ans -= bs (i, l - 1);
  return ans;
}

void init(std::string a, std::string b) {
  n = a.size ();
  bit.resize (n);
  for (int i = 0; i < n; ++i)
    {
      if (a[i] == 'T') a[i] = 'B';
      if (b[i] == 'T') b[i] = 'B';
      if (i)
        for (int j = 0; j < 6; ++j)
          bit[i][j] += brs (j, i - ((i + 1) & -(i + 1)) + 1, i - 1);
      if (a[i] != b[i])
        bit[i][toi (a[i], b[i])]++;
    }
}

int get_distance(int x, int y) {
  array < int, 6 > a = { brs (0, x, y), brs (1, x, y), brs (2, x, y),
                         brs (3, x, y), brs (4, x, y), brs (5, x, y) };
  int c = 0;
  auto fn = [&] ()
  {
    for (char x = 'A'; x <= 'C'; ++x)
      for (char y = 'A'; y <= 'C'; ++y)
        if (x != y)
          {
            int m = min (a[toi (x, y)], a[toi (y, x)]);
            c += m;
            a[toi (x, y)] -= m;
            a[toi (y, x)] -= m;
          }
  };
  fn ();
  for (char x = 'A'; x <= 'C'; ++x)
    for (char y = 'A'; y <= 'C'; ++y)
      if (x != y)
        {
          char z = 'A' + 'B' + 'C' - x - y;
          int m = min (a[toi (x, y)], a[toi (z, x)]);
          c += m;
          a[toi (x, y)] -= m;
          a[toi (z, x)] -= m;
          a[toi (z, y)] += m;
          fn ();
        }
  if (a[0] || a[1] || a[2] || a[3] || a[4] || a[5]) return -1;
  return c;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...