Submission #818129

#TimeUsernameProblemLanguageResultExecution timeMemory
818129becaidoMutating DNA (IOI21_dna)C++17
100 / 100
36 ms8424 KiB
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx,popcnt,sse4,abm")
#include <bits/stdc++.h>
using namespace std;

#ifndef WAIMAI
#include "dna.h"
#endif

#ifdef WAIMAI
#define debug(HEHE...) cout << "[" << #HEHE << "] : ", dout(HEHE)
void dout() {cout << '\n';}
template<typename T, typename...U>
void dout(T t, U...u) {cout << t << (sizeof...(u) ? ", " : ""), dout(u...);}
#else
#define debug(...) 7122
#endif

#define ll long long
#define Waimai ios::sync_with_stdio(false), cin.tie(0)
#define FOR(x,a,b) for (int x = a, I = b; x <= I; x++)
#define pb emplace_back
#define F first
#define S second

const int SIZE = 1e5 + 5;

int n;
int prea[SIZE][3];
int preb[SIZE][3];
int pre2[SIZE][3][3];

void init(string a, string b) {
    n = a.size();
    a = " " + a;
    b = " " + b;
    FOR (i, 1, n) {
        FOR (j, 0, 2) {
            prea[i][j] = prea[i - 1][j];
            preb[i][j] = preb[i - 1][j];
            FOR (k, 0, 2) pre2[i][j][k] = pre2[i - 1][j][k];
        }
        int x = (a[i] == 'A' ? 0 : a[i] == 'T' ? 1 : 2);
        int y = (b[i] == 'A' ? 0 : b[i] == 'T' ? 1 : 2);
        prea[i][x]++;
        preb[i][y]++;
        pre2[i][x][y]++;
    }
}

int cnt[3][3];
int get_distance(int l, int r) {
    l++, r++;
    FOR (i, 0, 2) if (prea[r][i] - prea[l - 1][i] != preb[r][i] - preb[l - 1][i]) return -1;
    memset(cnt, 0, sizeof(cnt));
    FOR (i, 0, 2) {
        FOR (j, 0, 2) cnt[i][j] = pre2[r][i][j] - pre2[l - 1][i][j];
        cnt[i][i] = 0;
    }
    int ans = 0, mx = 0;
    FOR (i, 0, 2) FOR (j, i + 1, 2) {
        int &x = cnt[i][j], &y = cnt[j][i];
        int delta = min(x, y);
        ans += delta;
        x -= delta, y -= delta;
    }
    FOR (i, 0, 2) FOR (j, 0, 2) mx = max(mx, cnt[i][j]);
    ans += 2 * mx;
    return ans;
}

/*
in1
6 3
ATACAT
ACTATA
1 3
4 5
3 5
out1
2
1
-1
*/

#ifdef WAIMAI
int main() {
    int n, q;
    assert(scanf("%d %d", &n, &q) == 2);
    char A[n+1], B[n+1];
    assert(scanf("%s", A) == 1);
    assert(scanf("%s", B) == 1);
    string a = string(A);
    string b = string(B);
    vector<int> x(q), y(q);
    for (int i = 0; i < q; i++) {
        assert(scanf("%d %d", &x[i], &y[i]) == 2);
    }
    fclose(stdin);
    vector<int> results(q);
    init(a, b);
    for (int i = 0; i < q; i++) {
        results[i] = get_distance(x[i], y[i]);
    }
    for (int i = 0; i < q; i++) {
        printf("%d\n", results[i]);
    }
    fclose(stdout);
    return 0;
}
#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...