結果

問題 No.2276 I Want AC
ユーザー FromBooskaFromBooska
提出日時 2023-06-06 20:19:14
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 325 ms / 2,000 ms
コード長 1,808 bytes
コンパイル時間 277 ms
コンパイル使用メモリ 86,972 KB
実行使用メモリ 76,576 KB
最終ジャッジ日時 2023-08-28 10:48:02
合計ジャッジ時間 13,508 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 68 ms
71,260 KB
testcase_01 AC 70 ms
71,260 KB
testcase_02 AC 68 ms
71,260 KB
testcase_03 AC 72 ms
71,472 KB
testcase_04 AC 69 ms
71,368 KB
testcase_05 AC 69 ms
71,384 KB
testcase_06 AC 71 ms
71,132 KB
testcase_07 AC 71 ms
71,184 KB
testcase_08 AC 71 ms
71,240 KB
testcase_09 AC 72 ms
71,292 KB
testcase_10 AC 71 ms
71,020 KB
testcase_11 AC 140 ms
75,788 KB
testcase_12 AC 281 ms
76,216 KB
testcase_13 AC 143 ms
75,912 KB
testcase_14 AC 106 ms
75,760 KB
testcase_15 AC 173 ms
76,128 KB
testcase_16 AC 164 ms
75,976 KB
testcase_17 AC 282 ms
76,380 KB
testcase_18 AC 219 ms
76,044 KB
testcase_19 AC 83 ms
75,460 KB
testcase_20 AC 308 ms
76,368 KB
testcase_21 AC 156 ms
76,148 KB
testcase_22 AC 109 ms
76,060 KB
testcase_23 AC 303 ms
76,432 KB
testcase_24 AC 316 ms
76,576 KB
testcase_25 AC 325 ms
76,452 KB
testcase_26 AC 312 ms
76,376 KB
testcase_27 AC 303 ms
76,252 KB
testcase_28 AC 247 ms
76,272 KB
testcase_29 AC 218 ms
76,552 KB
testcase_30 AC 290 ms
76,424 KB
testcase_31 AC 191 ms
76,428 KB
testcase_32 AC 308 ms
76,504 KB
testcase_33 AC 289 ms
76,268 KB
testcase_34 AC 297 ms
76,388 KB
testcase_35 AC 76 ms
75,600 KB
testcase_36 AC 78 ms
75,552 KB
testcase_37 AC 260 ms
76,132 KB
testcase_38 AC 96 ms
76,012 KB
testcase_39 AC 91 ms
76,496 KB
testcase_40 AC 103 ms
76,156 KB
testcase_41 AC 110 ms
76,008 KB
testcase_42 AC 263 ms
76,244 KB
testcase_43 AC 265 ms
76,168 KB
testcase_44 AC 79 ms
75,736 KB
testcase_45 AC 305 ms
76,208 KB
testcase_46 AC 284 ms
76,456 KB
testcase_47 AC 110 ms
75,992 KB
testcase_48 AC 119 ms
76,136 KB
testcase_49 AC 309 ms
76,272 KB
testcase_50 AC 297 ms
76,468 KB
testcase_51 AC 291 ms
76,480 KB
testcase_52 AC 318 ms
76,420 KB
testcase_53 AC 313 ms
76,264 KB
testcase_54 AC 220 ms
76,280 KB
testcase_55 AC 203 ms
76,244 KB
testcase_56 AC 188 ms
76,340 KB
testcase_57 AC 142 ms
76,248 KB
testcase_58 AC 234 ms
76,260 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

# ?の前半は当然Aがいい、後半はCがいい
# それではどこまでAであるべきなのか、を探索する
# 入力例1からわかるが単調増加、単調減少ではないので三分探索
# 使っている三分探索が最低値探索なのでスコアをマイナスにして探索した

N = int(input())
S = input()
Q_count = S.count('?')

# 使っている三分探索が最低値探索用なのでマイナスのスコアとした
def check(X):
    q_count = 0
    a_count = 0
    score = 0
    for s in S:
        if s == 'A':
            a_count += 1
        elif s == 'C':
            score += a_count
        elif s == '?':
            if q_count < X:
                # deem as A
                a_count += 1
                q_count += 1
            else:
                # deem as C
                score += a_count
                q_count += 1
    return -score

#for x in range(Q_count+1):
#    print(x, check(x))


if Q_count == 0:
    ans = -check(0)
    print(ans)
    exit()



# 谷、つまり1極値を求める三分探索での解き方
# https://roiti46.hatenablog.com/entry/2015/04/29/yukicoder_No.198_%E3%82%AD%E3%83%A3%E3%83%B3%E3%83%87%E3%82%A3%E3%83%BC%E3%83%BB%E3%83%9C%E3%83%83%E3%82%AF%E3%82%B9%EF%BC%92
# https://qiita.com/ganyariya/items/1553ff2bf8d6d7789127

# 範囲に注意、ギリギリありえない値とする
left, right = -1, Q_count+1
while right-left > 2:
    left_mid, right_mid = (2*left+right)//3, (left+2*right)//3
    if check(left_mid) <= check(right_mid):
        # 右midの方が大きいから右を右midに変える
        right = right_mid
    else:
        left = left_mid

#print(left_mid, right_mid)

ans = min(check(left_mid), check(right_mid))
# leftとrightの間も入れる必要あるのか? ない
print(-ans)

0