Submission #1040402

#TimeUsernameProblemLanguageResultExecution timeMemory
1040402yanbMutating DNA (IOI21_dna)C++17
100 / 100
69 ms16804 KiB
#include <bits/stdc++.h>
    
using namespace std;
    
#define int long long
#define pii pair<long long, long long>

string a, b;
vector<vector<vector<int>>> pref;
vector<vector<int>> apref, bpref;

void init(string a_, string b_) {
    vector<int> mp(256, -1);
    mp['A'] = 0, mp['C'] = 1, mp['T'] = 2;

    a = a_, b = b_;
    int n = a.size();
    pref.resize(3, vector<vector<int>>(3, vector<int>(n + 1)));
    apref.resize(3, vector<int>(n + 1));
    bpref.resize(3, vector<int>(n + 1));

    for (int i = 0; i < n; i++) {
        for (int c1 = 0; c1 < 3; c1++) {
            for (int c2 = 0; c2 < 3; c2++) {
                pref[c1][c2][i + 1] = pref[c1][c2][i] + (c1 == mp[a[i]] && c2 == mp[b[i]]);
            }
        }
        for (int c = 0; c < 3; c++) {
            apref[c][i + 1] = apref[c][i] + (c == mp[a[i]]);
            bpref[c][i + 1] = bpref[c][i] + (c == mp[b[i]]);
        }
    }

    /*for (auto [str, vec] : apref) {
        cout << str << ": ";
        for (int x : vec) cout << x << " ";
        cout << "\n";
    }*/
}

int get_distance(signed l, signed r) {
    vector<vector<int>> cnt(3, vector<int>(3));
    vector<int> acnt(3), bcnt(3);
    for (int c1 = 0; c1 < 3; c1++) {
        for (int c2 = 0; c2 < 3; c2++) {
            cnt[c1][c2] = pref[c1][c2][r + 1] - pref[c1][c2][l];
        }
        acnt[c1] = apref[c1][r + 1] - apref[c1][l];
        bcnt[c1] = bpref[c1][r + 1] - bpref[c1][l];
    }

    for (int chr = 0; chr < 3; chr++) {
        if (acnt[chr] != bcnt[chr]) return -1;
    }

    int atflip = min(cnt[2][0], cnt[0][2]);
    int acflip = min(cnt[1][0], cnt[0][1]);
    int ctflip = min(cnt[2][1], cnt[1][2]);
    int ans = atflip + acflip + ctflip;
    cnt[2][0] -= atflip;
    cnt[0][2] -= atflip;
    cnt[2][1] -= ctflip;
    cnt[1][2] -= ctflip;
    cnt[1][0] -= acflip;
    cnt[0][1] -= acflip;
    ans += (cnt[2][0] + cnt[0][2] + cnt[2][1] + cnt[1][2] + cnt[1][0] + cnt[0][1]) / 3 * 2;

    return ans;
}

#ifdef ONLINE_JUDGE
signed main() {
    init("ATACAT", "ACTATA");
    cout << get_distance(1, 3) << "\n";
    cout << get_distance(4, 5) << "\n";
    cout << get_distance(3, 5) << "\n";
}
#endif
#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...