結果

問題 No.910 素数部分列
ユーザー merom686
提出日時 2019-10-18 22:59:52
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 1,438 bytes
コンパイル時間 738 ms
コンパイル使用メモリ 74,632 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-06-25 18:35:06
合計ジャッジ時間 2,394 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 20 WA * 30
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cstring>
#include <cstdlib>
#include <cmath>
using namespace std;
using ll = long long;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);

    int n;
    string s;
    cin >> n >> s;

    int r = 0, m = 0;
    for (int i = 0; i < n; i++) {
        if (s[i] == '3' || s[i] == '5' || s[i] == '7') {
            r++;
        } else {
            s[m++] = s[i];
        }
    }

    int c1 = 0, c9 = 0, j = 0;
    for (int i = 0; i < m; i++) {
        if (s[i] == '1') {
            if (c9 >= 2) {
                c9 -= 2;
                r++;
            } else {
                c1++;
                for (j = max(j, i + 1); j < n; j++) {
                    if (s[j] == '9') {
                        c1--;
                        s[j] = 0;
                        r++;
                        j++;
                        break;
                    }
                }
            }
        } else if (s[i] == '9') {
            c9++;
        }
    }
    r += c1 / 2;

    cout << r << endl;

    //9が余ったらカウントしておく 1が余ったら(余った9二つ、右の9の順)使う それでも余った1はカウントしておく
    //それで余った9が右に余らせたために損することあるか そこより左の1は全部有効活用してるから

    //3 5 7
    //11 19
    //991

    return 0;
}
0