結果

問題 No.910 素数部分列
ユーザー IKyopro
提出日時 2019-10-18 22:55:40
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 1,042 bytes
コンパイル時間 838 ms
コンパイル使用メモリ 69,160 KB
最終ジャッジ日時 2025-01-07 23:31:17
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 22 WA * 28
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
using namespace std;

int main(){
    int N;
    string S,T = "";
    cin >> N >> S;
    int ans = 0;
    for(int i=0;i<N;i++){
        if(S[i]!='1' && S[i]!='9') ans++;
        else T += S[i];       
    }
    int one = 0,nine = 0;
    int ans2 = 0;
    int n = T.size();
    for(int i=n-1;i>=0;i--){
        if(T[i]=='9') nine++;
        else{
            if(nine){
                nine--;
                ans2++;
            }else one++;
        }
    }
    ans2 += one/2;
    one = 0,nine = 0;
    int ans3 = 0;
    for(int i=0;i<n;i++){
        if(T[i]=='9') nine++;
        else{
            if(nine>=2){
                nine -= 2;
                ans3++;
            }else one++;
        }
    }
    ans3 += one/2;
    one = 0,nine = 0;
    int ans4 = 0;
    for(int i=n-1;i>=0;i--){
        if(T[i]=='9') nine++;
        else{
            if(nine>=2){
                nine -= 2;
                ans4++;
            }else one++;
        }
    }
    ans4 += one/2;
    cout << ans+max(ans2,max(ans3,ans4)) << endl;
}
0