結果

問題 No.1951 消えたAGCT(2)
ユーザー KKT89
提出日時 2022-05-20 21:47:40
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 81 ms / 3,000 ms
コード長 2,085 bytes
コンパイル時間 1,759 ms
コンパイル使用メモリ 136,664 KB
最終ジャッジ日時 2025-01-29 10:25:58
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 28
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize("Ofast")
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <queue>
#include <cstdio>
#include <ctime>
#include <assert.h>
#include <chrono>
#include <random>
#include <numeric>
#include <set>
#include <deque>
#include <stack>
#include <sstream>
#include <utility>
#include <cstring>
#include <unordered_map>
#include <unordered_set>
#include <tuple>
#include <array>
#include <bitset>
using namespace std;
typedef long long int ll;
typedef unsigned long long ull;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll myRand(ll B) {
    return (ull)rng() % B;
}
inline double time() {
    return static_cast<long double>(chrono::duration_cast<chrono::nanoseconds>(chrono::steady_clock::now().time_since_epoch()).count()) * 1e-9;
}

// 1-indexed
template<typename T>
struct BIT{
    int n;
    vector<T> bit;
    BIT(int n_=0):n(n_),bit(n+1){}
    T sum(int i){
        T res=0;
        for(;i>0;i-=(i&-i))res+=bit[i];
        return res;
    }
    void add(int i,T a){
        if(i==0)return;
        for(;i<=n;i+=(i&-i)){bit[i]+=a;}
    }
    int lower_bound(T k){ // k<=sum(res)
        if(k<=0)return 0;
        int res=0,i=1;
        while((i<<1)<=n)i<<=1;
        for(;i;i>>=1){
            if(res+i<=n&&bit[res+i]<k)k-=bit[res+=i];
        }
        return res+1;
    }
};

ll cnt[26];

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int n; cin >> n;
    string s; cin >> s;
    int res = 0;
    for(char c:s){
        cnt[c-'A']++;
    }
    BIT<int> bit(n);
    for(int i=0;i<n;i++){
        bit.add(i+1,1);
    }
    ll u = 0;
    auto gt2=[&](ll u,char c)->int{
        ll r = (c-'A')-u;
        r %= 26;
        if(r < 0) r += 26;
        return r;
    };
    while(1){
        ll sum = cnt[gt2(u,'A')] + cnt[gt2(u,'G')] + cnt[gt2(u,'C')] + cnt[gt2(u,'T')];
        if(sum == 0)break;
        int idx = bit.lower_bound(sum);
        bit.add(idx,-1);
        char c = s[idx-1];
        cnt[c-'A']--;
        u += cnt[c-'A'];
        res++;
    }
    cout << res << endl;
}


0