結果

問題 No.910 素数部分列
ユーザー rpy3cpp
提出日時 2019-10-19 18:27:22
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 5 ms / 1,000 ms
コード長 1,116 bytes
コンパイル時間 1,644 ms
コンパイル使用メモリ 166,988 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-06-27 14:37:41
合計ジャッジ時間 3,342 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 50
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N;
    cin >> N;
    string S;
    cin >> S;
    // 11, 19, 991 を何個とることができるかを考える。
    int count1 = 0;
    int count1deleted = 0;
    int ans = 0;
    for (char &s : S){
        if (s == '1'){
            ++count1;
        }else if (s == '9'){
            if (count1 > 0){
                --count1;
                ++count1deleted;
                ++ans;
                s = '0';
            }
        }else if (s == '3' or s == '5' or s == '7'){
            ++ans;
        }
    }
    for (char &s : S){
        if (s == '1' and count1deleted > 0){
            --count1deleted;
            s = '0';
            if (count1deleted == 0) break;
        }
    }
    int count9 = 0;
    for (char &s : S){
        if (s == '1'){
            if (count9 >= 2){
                count9 -= 2;
                ++ans;
                --count1;
            }
        }else if (s == '9'){
            ++count9;
        }
    }
    cout << ans + count1 / 2 << endl;
    return 0;
}
0