Submission #996923

#TimeUsernameProblemLanguageResultExecution timeMemory
996923toast12Mutating DNA (IOI21_dna)C++17
100 / 100
220 ms6580 KiB
#include "dna.h"
#include <map>
#include <vector>
#include <iostream>
using namespace std;

map<string, vector<int>> ps;
int n;

void init(std::string a, std::string b) {
	n = a.size();
	map<string, int> prev;
	for (int i = 0; i < n; i++) {
		ps["AT"].push_back(prev["AT"]);
		ps["AC"].push_back(prev["AC"]);
		ps["CA"].push_back(prev["CA"]);
		ps["CT"].push_back(prev["CT"]);
		ps["TA"].push_back(prev["TA"]);
		ps["TC"].push_back(prev["TC"]);
		if (a[i] == b[i])
			continue;
		string s = (string)a.substr(i, 1);
		s += b[i];
		int temp = ps[s].back();
		ps[s].pop_back();
		ps[s].push_back(temp+1);
		prev["AT"] = ps["AT"].back();
		prev["AC"] = ps["AC"].back();
		prev["CA"] = ps["CA"].back();
		prev["CT"] = ps["CT"].back();
		prev["TA"] = ps["TA"].back();
		prev["TC"] = ps["TC"].back();
	}
}

int get_distance(int x, int y) {
	map<string, int> cnt;
	if (x > 0) {
		cnt["AT"] = ps["AT"][y]-ps["AT"][x-1];
		cnt["AC"] = ps["AC"][y]-ps["AC"][x-1];
		cnt["CA"] = ps["CA"][y]-ps["CA"][x-1];
		cnt["CT"] = ps["CT"][y]-ps["CT"][x-1];
		cnt["TA"] = ps["TA"][y]-ps["TA"][x-1];
		cnt["TC"] = ps["TC"][y]-ps["TC"][x-1];
	}
	else {
		cnt["AT"] = ps["AT"][y];
		cnt["AC"] = ps["AC"][y];
		cnt["CA"] = ps["CA"][y];
		cnt["CT"] = ps["CT"][y];
		cnt["TA"] = ps["TA"][y];
		cnt["TC"] = ps["TC"][y];
	}
	int ans = 0;
	int t = min(cnt["TA"], cnt["AT"]);
	ans += t;
	cnt["TA"] -= t, cnt["AT"] -= t;
	t = min(cnt["AC"], cnt["CA"]);
	ans += t;
	cnt["AC"] -= t, cnt["CA"] -= t;
	t = min(cnt["CT"], cnt["TC"]);
	ans += t;
	cnt["CT"] -= t, cnt["TC"] -= t;
	t = min(min(cnt["AT"], cnt["TC"]), cnt["CA"]);
	ans += 2*t;
	cnt["AT"] -= t, cnt["TC"] -= t, cnt["CA"] -= t;
	t = min(min(cnt["TA"], cnt["AC"]), cnt["CT"]);
	ans += 2*t;
	cnt["TA"] -= t, cnt["AC"] -= t, cnt["CT"] -= t;
	if (cnt["AT"] || cnt["TA"] || cnt["AC"] || cnt["CA"] || cnt["TC"] || cnt["CT"])
		ans = -1;
	return ans;
}
#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...