Submission #438669

#TimeUsernameProblemLanguageResultExecution timeMemory
438669dxz05Mutating DNA (IOI21_dna)C++17
100 / 100
58 ms6456 KiB
#include "dna.h"
#include <bits/stdc++.h>

using namespace std;

const int MAXN = 2e5 + 3e2;

int pref[MAXN][6];
long long cnt[MAXN][2];

vector<vector<char>> dna = {{'A', 'C'}, {'C', 'A'}, {'A', 'T'}, {'T', 'A'}, {'T', 'C'}, {'C', 'T'}};

long long f(char ch){
    if (ch == 'A') return 1ll;
    if (ch == 'C') return 200000ll;
    if (ch == 'T') return 40000000000ll;
    return -1;
}

void init(string a, string b) {
    int n = a.size();
    for (int i = 1; i <= n; i++){
        cnt[i][0] = cnt[i - 1][0] + f(a[i - 1]);
        cnt[i][1] = cnt[i - 1][1] + f(b[i - 1]);
    }

    for (int i = 1; i <= n; i++){
        for (int j = 0; j < 6; j++){
            pref[i][j] = pref[i - 1][j] + (a[i - 1] == dna[j][0] && b[i - 1] == dna[j][1]);
        }
    }

}

int get_distance(int x, int y) {
    x++, y++;
    if (cnt[y][0] - cnt[x - 1][0] != cnt[y][1] - cnt[x - 1][1]) return -1;

    int ans1 = 0, ans2 = 0;
    for (int j = 0; j < 6; j += 2){
        int a = pref[y][j] - pref[x - 1][j];
        int b = pref[y][j + 1] - pref[x - 1][j + 1];

        int c = min(a, b);
        ans1 += c;
        ans2 += a - c + b - c;
    }

    int res = ans1 + ans2 / 3 * 2;

	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...