結果

問題 No.1951 消えたAGCT(2)
ユーザー srjywrdnprkt
提出日時 2023-06-29 18:39:40
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 208 ms / 3,000 ms
コード長 1,397 bytes
コンパイル時間 1,887 ms
コンパイル使用メモリ 199,452 KB
最終ジャッジ日時 2025-02-15 03:08:05
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 28
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

//RSQ (0-indexed)
template<class T> struct BIT {
    vector<T> bit;
    int N;

    BIT (int n){
        N = n;
        bit.resize(N);
    }

    void add(int loc, T val){
        loc++;
        while(loc <= N){
            bit[loc-1] += val;
            loc += loc & -loc;
        }
    }

    T sum(int l, int r){
        T res = _sum(r) - _sum(l-1);
        return res;
    }

    T _sum(int r){
        r++;
        T res = 0;
        while(r > 0){
            res += bit[r-1];
            r -= r & -r;
        }
        return res;
    }
};

int swp;
vector<int> cnt(26);
vector<int> v = {0, 19, 6, 2};

int atgc(){
    int res = 0;
    for (int i=0; i<4; i++) res += cnt[(26-swp+v[i]) % 26];
    return res;
}

int main(){
 
    int N, sm;
    int ch;
    string S;
    cin >> N >> S;
    BIT<int> tree(N);
    for (int i=0; i<N; i++){
        tree.add(i, 1);
        cnt[S[i]-'A']++;
    }

    while(1){
        sm = atgc();
        if (sm == 0) break;
        int l=0, r=N, c;
        while(r-l>1){
            c = (l+r)/2;
            if (tree.sum(0, c-1) < sm) l=c;
            else r=c;
        }
        tree.add(l, -1);
        ch = ((S[l]-'A')+swp) % 26;
        cnt[(26-swp+ch) % 26] -= 1;
        swp += cnt[(26-swp+ch) % 26];
        swp %= 26;
    }

    cout << N-tree.sum(0, N-1) << endl;

    return 0;
}
0