제출 #438111

#제출 시각아이디문제언어결과실행 시간메모리
438111mango_lassiDNA 돌연변이 (IOI21_dna)C++17
100 / 100
62 ms6072 KiB
#include "dna.h"
#include <bits/stdc++.h>
using namespace std;

vector<array<int, 9>> cou;

int getInd(char c) {
	if (c == 'A') return 0;
	if (c == 'C') return 1;
	if (c == 'T') return 2;
	assert(false);
	return -1;
}

void init(string a, string b) {
	int n = a.size();
	cou.resize(n + 1);
	for (int x = 0; x < 9; ++x) cou[0][x] = 0;
	for (int j = 1; j <= n; ++j) {
		for (int x = 0; x < 9; ++x) cou[j][x] = cou[j-1][x];
		int va = getInd(a[j-1]);
		int vb = getInd(b[j-1]);
		++cou[j][va + 3*vb];
	}
}

int get_distance(int x, int y) {
	array<int, 9> counts = cou[y + 1];
	for (int j = 0; j < 9; ++j) counts[j] -= cou[x][j];

	int ab = counts[1];
	int ac = counts[2];
	int ba = counts[3];
	int bc = counts[5];
	int ca = counts[6];
	int cb = counts[7];

	int diff_a = ab + ac - ba - ca;
	int diff_b = ba + bc - ab - cb;
	int diff_c = ca + cb - ac - bc;
	if (diff_a != 0 || diff_b != 0 || diff_c != 0) return -1;

	int abba = min(ab, ba);
	int acca = min(ac, ca);
	int bccb = min(bc, cb);
	int res = abba + acca + bccb;
	
	ab -= abba;
	ba -= abba;
	ac -= acca;
	ca -= acca;
	bc -= bccb;
	cb -= bccb;

	int abc = min(ab, min(bc, ca));
	int acb = min(ac, min(cb, ba));
	res += 2 * (abc + acb);
	ab -= abc;
	bc -= abc;
	ca -= abc;
	ac -= acb;
	cb -= acb;
	ba -= acb;
	assert(ab == 0 && bc == 0 && ca == 0 && ac == 0 && cb == 0 && ba == 0);

	return res;
}
#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...