結果

問題 No.1951 消えたAGCT(2)
ユーザー auauaauaua
提出日時 2022-05-20 23:54:56
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 247 ms / 3,000 ms
コード長 3,897 bytes
コンパイル時間 1,808 ms
コンパイル使用メモリ 174,600 KB
実行使用メモリ 12,052 KB
最終ジャッジ日時 2023-10-20 14:51:36
合計ジャッジ時間 5,341 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 47 ms
12,028 KB
testcase_09 AC 47 ms
12,028 KB
testcase_10 AC 47 ms
12,028 KB
testcase_11 AC 47 ms
12,028 KB
testcase_12 AC 31 ms
12,052 KB
testcase_13 AC 7 ms
4,348 KB
testcase_14 AC 225 ms
12,052 KB
testcase_15 AC 182 ms
12,052 KB
testcase_16 AC 194 ms
12,052 KB
testcase_17 AC 245 ms
12,028 KB
testcase_18 AC 243 ms
12,028 KB
testcase_19 AC 244 ms
12,028 KB
testcase_20 AC 243 ms
12,028 KB
testcase_21 AC 47 ms
12,028 KB
testcase_22 AC 47 ms
12,028 KB
testcase_23 AC 46 ms
12,028 KB
testcase_24 AC 46 ms
12,028 KB
testcase_25 AC 46 ms
12,028 KB
testcase_26 AC 241 ms
12,028 KB
testcase_27 AC 247 ms
12,028 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#include <unordered_set>
#include <random>
//#define int long long
#define REP(i,m,n) for(int i=(m);i<(n);i++)
#define rep(i,n) REP(i,0,n)
#define pb push_back
#define all(a) a.begin(),a.end()
#define rall(c) (c).rbegin(),(c).rend()
#define mp make_pair
#define endl '\n'
//#define vec vector<ll>
//#define mat vector<vector<ll> >
#define fi first
#define se second
#define double long double
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,ll> pll;
//typedef long double ld;
typedef complex<double> Complex;
const ll INF=1e9+7;
const ll MOD=998244353;
const ll inf=INF*INF;
const ll mod=MOD;
const ll MAX=200010;
const double PI=acos(-1.0);
typedef vector<vector<ll> > mat;
typedef vector<ll> vec;

ll dx[]={0,1,0,-1};
ll dy[]={1,0,-1,0};

//SegmentTree
template< typename Monoid >
struct SegmentTree
{
  using F = function< Monoid(Monoid, Monoid) >;
 
  int sz;
  vector< Monoid > seg;
 
  const F f;
  const Monoid M1;
  int N;
  SegmentTree(int n, const F f, const Monoid &M1) : f(f), M1(M1)
  {
    N = n;
    sz = 1;
    while(sz < n) sz <<= 1;
    seg.assign(2 * sz, M1);
  }
 
  void set(int k, const Monoid &x)
  {
    seg[k + sz] = x;
  }
 
  void build()
  {
    for(int k = sz - 1; k > 0; k--) {
      seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]);
    }
  }
 
  void update(int k, const Monoid &x)
  {
    k += sz;
    seg[k] = x;
    while(k >>= 1) {
      seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]);
    }
  }
 
  Monoid query(int a, int b)
  {
    Monoid L = M1, R = M1;
    for(a += sz, b += sz; a < b; a >>= 1, b >>= 1) {
      if(a & 1) L = f(L, seg[a++]);
      if(b & 1) R = f(seg[--b], R);
    }
    return f(L, R);
  }
  // a[l] + ... + a[r-1] <= x となる最大のr
  int max_right(int l, Monoid x, F g)
  {
      int r = l + sz;
      Monoid now = M1;
      while(1){
          if(g(f(now, seg[r]), x)) {
              int tmp = r + 1;
              if((tmp & (-tmp)) == tmp)return N;
              if(r & 1) {
                  now = f(now, seg[r]);
                  r++;
              }
              r >>= 1;
          }else{
              while(r - sz < 0) {
                  r <<= 1;
                  if(g(f(now, seg[r]), x)) {
                      now = f(now, seg[r]);
                      r++;
                  }
              }
              return r - sz;
          }
      }
  }
  //a[l] + ... + a[r-1] <= x となる最小のl
  int min_left(int r, Monoid x, F g)
  {
      int l = r - 1 + sz;
      int now = M1;
      while(1){
          if(g(f(now, seg[l]), x)){
              if((l&(-l))==l)return 0;
              if((l&1)==0){
                  now = f(now, seg[l]);
                  l--;
              }
              l >>= 1;
          }else{
              while(l - sz < 0) {
                  l <<= 1;
                  l++;
                  if(g(f(now, seg[l]), x)){
                      now = f(now, seg[l]);
                      l--;
                  }
              }
              return l + 1 - sz;
          }
      }
      return l - sz;
  }

  Monoid operator[](const int &k) const
  {
    return seg[k + sz];
  }
};


void solve(){
    ll n;cin>>n;
    string s;cin>>s;
    string mm="ATGC";
    vector<ll>m(4);
    rep(i,4)m[i]=mm[i]-'A';
    vector<ll>cnt(26);
    SegmentTree<ll>seg(n,[](ll a,ll b){return a+b;},0);
    rep(i,n)seg.update(i,1);
    rep(i,n){
        cnt[s[i]-'A']++;
    }
    ll now=0;
    ll ans=0;
    rep(i,n){
        ll c1=0;
        rep(k,4){
            c1+=cnt[(m[k]+26-now)%26];
        }
        if(c1==0)break;
        ans++;
        c1--;
        ll id=seg.max_right(0,c1,[](ll a,ll b){return a<=b;});
        cnt[s[id]-'A']--;
        seg.update(id,0);
        ll c2=cnt[s[id]-'A'];
        now+=c2;
        now%=26;
    }
    cout<<ans<<endl;
}

signed main(){
    //cin.tie(0);
    //ios::sync_with_stdio(false);
    solve();
}
0