結果

問題 No.2276 I Want AC
ユーザー umezoumezo
提出日時 2023-04-21 22:37:54
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 63 ms / 2,000 ms
コード長 1,382 bytes
コンパイル時間 1,885 ms
コンパイル使用メモリ 199,832 KB
実行使用メモリ 9,984 KB
最終ジャッジ日時 2024-04-24 08:44:43
合計ジャッジ時間 4,774 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 10 ms
5,376 KB
testcase_12 AC 34 ms
9,088 KB
testcase_13 AC 10 ms
5,376 KB
testcase_14 AC 5 ms
5,376 KB
testcase_15 AC 16 ms
6,400 KB
testcase_16 AC 14 ms
6,144 KB
testcase_17 AC 35 ms
8,832 KB
testcase_18 AC 22 ms
7,296 KB
testcase_19 AC 2 ms
5,376 KB
testcase_20 AC 47 ms
9,600 KB
testcase_21 AC 13 ms
5,888 KB
testcase_22 AC 6 ms
5,376 KB
testcase_23 AC 40 ms
9,856 KB
testcase_24 AC 41 ms
9,856 KB
testcase_25 AC 39 ms
9,984 KB
testcase_26 AC 46 ms
9,984 KB
testcase_27 AC 40 ms
9,856 KB
testcase_28 AC 30 ms
9,856 KB
testcase_29 AC 28 ms
9,856 KB
testcase_30 AC 37 ms
9,856 KB
testcase_31 AC 21 ms
9,856 KB
testcase_32 AC 39 ms
9,856 KB
testcase_33 AC 37 ms
9,856 KB
testcase_34 AC 51 ms
9,856 KB
testcase_35 AC 37 ms
9,856 KB
testcase_36 AC 19 ms
9,856 KB
testcase_37 AC 62 ms
9,856 KB
testcase_38 AC 38 ms
9,984 KB
testcase_39 AC 37 ms
9,984 KB
testcase_40 AC 18 ms
9,856 KB
testcase_41 AC 18 ms
9,984 KB
testcase_42 AC 63 ms
9,856 KB
testcase_43 AC 61 ms
9,856 KB
testcase_44 AC 28 ms
9,856 KB
testcase_45 AC 40 ms
9,856 KB
testcase_46 AC 49 ms
9,856 KB
testcase_47 AC 28 ms
9,856 KB
testcase_48 AC 28 ms
9,856 KB
testcase_49 AC 40 ms
9,856 KB
testcase_50 AC 40 ms
9,856 KB
testcase_51 AC 48 ms
9,856 KB
testcase_52 AC 49 ms
9,856 KB
testcase_53 AC 41 ms
9,856 KB
testcase_54 AC 34 ms
9,984 KB
testcase_55 AC 33 ms
9,856 KB
testcase_56 AC 30 ms
9,856 KB
testcase_57 AC 36 ms
9,856 KB
testcase_58 AC 24 ms
9,984 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define ALL(v) v.begin(),v.end()
typedef long long ll;
 
#include<bits/stdc++.h>
using namespace std;

template <typename T>
struct BIT{
  int n;
  vector<T> bit[2];
  BIT(int n_){init(n_);}
  void init(int n_){
    n=n_+1;
    for(int p=0;p < 2;p++) bit[p].assign(n,0);
  }
  
  void add_sub(int p,int i,T x){
    for(int idx=i;idx<n;idx+=(idx & -idx)){
      bit[p][idx] += x;
    }
  }
  
  void add(int l,int r,T x){ //[l,r]に加算
    add_sub(0,l,-x*(l-1));
    add_sub(0,r+1,x*r);
    add_sub(1,l,x);
    add_sub(1,r+1,-x);
  }
  
  T sum_sub(int p,int i){
    T s(0);
    for(int idx=i;idx>0;idx-=(idx & -idx)){
      s+=bit[p][idx];
    }
    return s;
  }
  
  T sum(int i){return sum_sub(0,i)+sum_sub(1,i)*i;}
  
  T sum(int i,int j){  //[i,j]の要素の総和
    return sum(j)-sum(i-1);
  }  
};

int main(){
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  
  int n;
  string s;
  cin>>n>>s;
  
  BIT<ll> bit(n),bit1(n);
  
  ll ans=0;
  ll cnt=0;
  for(int i=n-1;i>=0;i--){
    if(s[i]=='C'){
      cnt++;
    }
    else{
      ans+=cnt;
      bit.add(i+1,i+1,1);
    }
    bit1.add(i+1,i+1,cnt);
  }
  
  ll now=ans;
  for(int i=n-1;i>=0;i--){
    if(s[i]=='?'){
      now-=bit1.sum(i+1,i+1);
      bit1.add(1,i,1);
      now+=bit.sum(1,i);
    }
    ans=max(ans,now);
  }
  cout<<ans<<endl;
    
  return 0;
}
0