Submission #1101256

#TimeUsernameProblemLanguageResultExecution timeMemory
1101256Zero_OPMutating DNA (IOI21_dna)C++17
0 / 100
24 ms6992 KiB
#include "bits/stdc++.h"

#ifndef LOCAL
    #include "dna.h"
#endif // LOCAL

using namespace std;

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

const int MAX = 1e5 + 5;

int n, pref[MAX][3][3];

void init(std::string a, std::string b) {
    n = (int)a.size();
    for(int i = 0; i < n; ++i){
        for(int j = 0; j < 3; ++j){
            for(int k = 0; k < 3; ++k){
                pref[i + 1][j][k] = pref[i][j][k];
            }
        }

        ++pref[i + 1][code(a[i])][code(b[i])];
    }
}

int A[3], B[3], cnt[3][3];

int get_distance(int x, int y) {
    ++x; ++y;
    for(int i = 0; i < 3; ++i) {
        A[i] = B[i] = 0;
        for(int j = 0; j < 3; ++j){
            cnt[i][j] = 0;
        }
    }

    for(int i = 0; i < 3; ++i){
        for(int j = 0; j < 3; ++j){
            A[i] += pref[y][i][j] - pref[x - 1][i][j];
            B[j] += pref[y][i][j] - pref[x - 1][i][j];
            cnt[i][j] += pref[y][i][j] - pref[x - 1][i][j];
        }
    }

    for(int i = 0; i < 3; ++i){
        if(A[i] != B[i]){
            return -1;
        }
    }

    int ans = 0;

    auto work_greedy = [&](int i, int j){
        int x = min(cnt[i][j], cnt[j][i]);
        ans += x;
        cnt[i][j] -= x;
        cnt[j][i] -= x;
    };

    for(int i = 0; i < 3; ++i){
        for(int j = 0; j < 3; ++j){
            work_greedy(i, j);
        }
    }

    int rest = 0;
    for(int i = 0; i < 3; ++i){
        for(int j = 0; j < 3; ++j){
            rest = max(rest, cnt[i][j]);
        }
    }

    ans += rest * 2;

	return ans;
}

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