제출 #939875

#제출 시각아이디문제언어결과실행 시간메모리
939875vladiliusDNA 돌연변이 (IOI21_dna)C++17
100 / 100
113 ms8708 KiB
#include <bits/stdc++.h> using namespace std; using ll = long long; using pii = pair<char, char>; string a, b; int n; vector<char> x = {'A', 'C', 'T'}; vector<int> pa, pc, pt, pa1, pc1, pt1; map<pii, vector<int>> pr; void init(string as, string bs){ a = '#' + as; b = '#' + bs; n = (int) a.length(); pa.resize(n + 1); pc.resize(n + 1); pt.resize(n + 1); pa1.resize(n + 1); pc1.resize(n + 1); pt1.resize(n + 1); for (int i = 1; i <= n; i++){ pa[i] = pa[i - 1] + (a[i] == 'A'); pc[i] = pc[i - 1] + (a[i] == 'C'); pt[i] = pt[i - 1] + (a[i] == 'T'); pa1[i] = pa1[i - 1] + (b[i] == 'A'); pc1[i] = pc1[i - 1] + (b[i] == 'C'); pt1[i] = pt1[i - 1] + (b[i] == 'T'); } for (char i1: x){ for (char i2: x){ if (i1 == i2) continue; pr[{i1, i2}].resize(n + 1); for (int i = 1; i <= n; i++){ pr[{i1, i2}][i] = pr[{i1, i2}][i - 1] + (a[i] == i1 && b[i] == i2); } } } } int get_distance(int l, int r){ l++; r++; if ((pa[r] - pa[l - 1]) != (pa1[r] - pa1[l - 1])) return -1; if ((pc[r] - pc[l - 1]) != (pc1[r] - pc1[l - 1])) return -1; if ((pt[r] - pt[l - 1]) != (pt1[r] - pt1[l - 1])) return -1; map<pii, int> cnt; for (char i1: x){ for (char i2: x){ if (i1 == i2) continue; cnt[{i1, i2}] = pr[{i1, i2}][r] - pr[{i1, i2}][l - 1]; if (!cnt[{i1, i2}]) cnt.erase({i1, i2}); } } vector<pii> arr; for (auto [u, v]: cnt){ arr.push_back(u); } int out = 0; for (auto p1: arr){ pii p2 = {p1.second, p1.first}; if (cnt.find(p2) == cnt.end()){ continue; } int k1 = cnt[p1], k2 = cnt[p2]; out += min(k1, k2); if (k1 > k2){ cnt.erase(p2); cnt[p1] -= k2; } else { cnt.erase(p1); cnt[p2] -= k1; if (!cnt[p2]){ cnt.erase(p2); } } } vector<int> f; for (auto [u, v]: cnt) f.push_back(v); sort(f.begin(), f.end()); while (f.size() > 1){ out += f[0]; f.back() -= f[0]; f.erase(f.begin()); if (!f.back()) f.pop_back(); sort(f.begin(), f.end()); } if (!f.empty()) out += f[0]; return out; }
#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...