結果

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

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

int solve(string& s) {
    int n = s.size();
    int cnt1 = 0;
    int cnt9 = 0;
    int ans = 0;
    for (int i = 0; i < n; i++) {
        if (s[i] == '1') {
            cnt1++;
        } else {
            if (cnt1 > 0) {
                cnt1--;
                ans++;
            } else {
                cnt9++;
            }
        }
    }
    while (cnt9 >= 2 && cnt1 > 0) {
        cnt9 -= 2;
        cnt1--;
        ans++;
    }
    ans += cnt1 / 2;
    return ans;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n;
    cin >> n;
    string s;
    cin >> s;
    int ans = 0;
    string s2 = "";
    for (int i = 0; i < n; i++) {
        if (s[i] == '3' || s[i] == '5' || s[i] == '7') {
            ans++;
        } else {
            s2 += s[i];
        }
    }
    ans += solve(s2);
    cout << ans << "\n";
    return 0;
}
0