Submission #705484

#TimeUsernameProblemLanguageResultExecution timeMemory
705484jakobrsMutating DNA (IOI21_dna)C++17
100 / 100
42 ms5716 KiB
#include <iostream>
#include <vector>

struct Count {
  int at, ca, tc;
  int qw;

  Count() : at(0), ca(0), tc(0), qw(0) {}

  inline Count operator+(const Count &rhs) const {
    Count res = *this;
    res += rhs;
    return res;
  }

  inline void operator+=(const Count &rhs) {
    at += rhs.at;
    ca += rhs.ca;
    tc += rhs.tc;
    qw += rhs.qw;
  }

  inline Count operator-(const Count &rhs) const {
    Count res = *this;
    res -= rhs;
    return res;
  }

  inline void operator-=(const Count &rhs) {
    at -= rhs.at;
    ca -= rhs.ca;
    tc -= rhs.tc;
    qw -= rhs.qw;
  }
};

std::string a, b;
std::vector<Count> prefixes;

void init(std::string A, std::string B) {
  a = A;
  b = B;

  prefixes.reserve(A.size() + 1);
  prefixes.push_back(Count{});
  Count count;
  for (int i = 0; i < A.size(); i++) {
    if (A[i] == 'A' && B[i] == 'T') count.at += 1;
    if (A[i] == 'T' && B[i] == 'A') count.at -= 1;
    if (A[i] == 'C' && B[i] == 'A') count.ca += 1;
    if (A[i] == 'A' && B[i] == 'C') count.ca -= 1;
    if (A[i] == 'T' && B[i] == 'C') count.tc += 1;
    if (A[i] == 'C' && B[i] == 'T') count.tc -= 1;
    if (A[i] != B[i]) count.qw += 1;
    prefixes.push_back(count);
  }
}

int get_distance(int x, int y) {
  y += 1;

  Count count = prefixes[y] - prefixes[x];

  // a1 = aa + at + ac
  // a2 = aa + ta + ca
  // a1 == a2
  // at + ac == ta + ca
  // (at - ta) + (ac - ca) == 0
  // (at - ta) == (ca - ac)

  // t1 = tt + ta + tc
  // t2 = tt + at + ct
  // t1 == t2
  // (ta - at) + (tc - ct) == 0
  // (tc - ct) == (at - ta)

  // c1 = cc + ca + ct
  // c2 = cc + ac + tc
  // c1 == c2
  // (ca - ac) + (ct - tc) == 0
  // (ca - ac) == (tc - ct)

  // (at - ta) == (ca - ac) == (tc - ct)
  // if (count.a1 != count.a2 || count.t1 != count.t2 || count.c1 != count.c2) {
  //   return -1;
  // }

  if (!(count.at == count.ca && count.ca == count.tc)) return -1;

  // std::cout << count.at << ' ' << count.ta << ' ' << count.ac << ' ' << count.ca << ' ' << count.tc << ' ' << count.ct << '\n';

  // int at_pairs = std::min(count.at, count.ta);
  // int ac_pairs = std::min(count.ac, count.ca);
  // int tc_pairs = std::min(count.tc, count.ct);

  // int total = at_pairs + ac_pairs + tc_pairs;

  // count.at -= at_pairs;
  // count.ta -= at_pairs;
  // count.ac -= ac_pairs;
  // count.ca -= ac_pairs;
  // count.tc -= tc_pairs;
  // count.ct -= tc_pairs;

  // std::cout << count.at << ' ' << count.ta << ' ' << count.ac << ' ' << count.ca << ' ' << count.tc << ' ' << count.ct << '\n';

  // return total + std::max(count.at, count.ta) * 2;

  // qw = at + ta + ac + ca + tc + ct
  // (at + ta) - abs(at - ta)

  int total =
      count.qw - std::abs(count.at) - std::abs(count.tc) - std::abs(count.ca);
  total /= 2;

  return total + std::abs(count.at) * 2;
}

Compilation message (stderr)

dna.cpp: In function 'void init(std::string, std::string)':
dna.cpp:47:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   47 |   for (int i = 0; i < A.size(); i++) {
      |                   ~~^~~~~~~~~~
#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...