結果

問題 No.910 素数部分列
ユーザー rtia_iidxrtia_iidx
提出日時 2019-10-18 23:13:03
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 96 ms / 1,000 ms
コード長 5,579 bytes
コンパイル時間 1,003 ms
コンパイル使用メモリ 109,772 KB
実行使用メモリ 22,412 KB
最終ジャッジ日時 2023-09-08 04:37:20
合計ジャッジ時間 5,063 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 1 ms
4,380 KB
testcase_17 AC 1 ms
4,380 KB
testcase_18 AC 1 ms
4,376 KB
testcase_19 AC 1 ms
4,380 KB
testcase_20 AC 1 ms
4,380 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 2 ms
4,376 KB
testcase_23 AC 33 ms
10,524 KB
testcase_24 AC 33 ms
10,616 KB
testcase_25 AC 33 ms
10,592 KB
testcase_26 AC 34 ms
10,608 KB
testcase_27 AC 33 ms
10,532 KB
testcase_28 AC 35 ms
10,928 KB
testcase_29 AC 88 ms
21,704 KB
testcase_30 AC 88 ms
22,224 KB
testcase_31 AC 85 ms
21,316 KB
testcase_32 AC 91 ms
22,168 KB
testcase_33 AC 87 ms
22,128 KB
testcase_34 AC 86 ms
22,212 KB
testcase_35 AC 86 ms
22,264 KB
testcase_36 AC 85 ms
22,220 KB
testcase_37 AC 89 ms
22,120 KB
testcase_38 AC 84 ms
22,088 KB
testcase_39 AC 91 ms
22,264 KB
testcase_40 AC 86 ms
22,092 KB
testcase_41 AC 90 ms
22,412 KB
testcase_42 AC 83 ms
22,232 KB
testcase_43 AC 83 ms
22,140 KB
testcase_44 AC 85 ms
22,100 KB
testcase_45 AC 86 ms
22,140 KB
testcase_46 AC 85 ms
22,124 KB
testcase_47 AC 88 ms
22,316 KB
testcase_48 AC 81 ms
22,168 KB
testcase_49 AC 96 ms
22,268 KB
testcase_50 AC 96 ms
22,100 KB
testcase_51 AC 3 ms
4,376 KB
testcase_52 AC 91 ms
22,172 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<vector>
#include<algorithm>
#include<functional>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<unordered_map>
#include<climits>
#include<cstdlib>
#include<cmath>
#include<string>
#include<iomanip>
#include<bitset>
#include<list>

using namespace std;

#define ll long long int

ll const MOD = 1000000007;
ll const INF = (long long int)1 << 61;

ll mypow(ll x,ll n,ll mod = MOD){
    ll ret = 1;
    while(n > 0){
        if(n&1){
            ret = (ret*x)%mod;
        }
        x = (x*x)%mod;
        n >>= 1;
    }
    return ret;
}

ll mygcd(ll a,ll b){
    if(b == 0)return a;
    return mygcd(b,a%b);
}

ll twoPow(ll shiftNum){
    return (1LL << (shiftNum - 1));
}

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

    string str;

    cin >> str;

    set<ll> st1,st2,st3;

    ll ans = 0;

    for(int i = 0; i < n; i++){
        if(str[i] == '3' || str[i] == '7' || str[i] == '5'){
            ans++;
        }else if(str[i] == '1'){
            st1.insert(i);
        }else{
            st3.insert(i);
        }
    }

    /*while(true){
        auto itr = st2.begin();
        if(itr == st2.end())break;
        auto r = st3.upper_bound(*itr);
        if(r != st3.end()){
            ans++;
            st2.erase(itr);
            st3.erase(r);
        }else{
            break;
        }
    }*/

    auto st1c = st1;
    auto st3c = st3;

    auto tmpans = ans;

    while(true){
        auto itr = st3.begin();
        if(itr == st3.end()){
            break;
        }
        bool check = true;
        auto m = st3.upper_bound(*itr);
        if(m != st3.end()){
            auto r = st1.upper_bound(*m);
            if(r != st1.end()){
                ans++;
                st3.erase(itr);
                st1.erase(r);
                st3.erase(m);
            }else{
                check = false;
            }
        }else{
            break;
        }
        if(!check){
            break;
        }
    }

    while(true){
        auto itr = st1.begin();
        if(itr == st1.end()){
            break;
        }
        auto r = st3.upper_bound(*itr);
        if(r != st3.end()){
            ans++;
            st1.erase(itr);
            st3.erase(r);
        }else{
            break;
        }
    }

    while(true){
        auto itr = st1.begin();
        if(itr == st1.end()){
            break;
        }
        auto r = st1.upper_bound(*itr);
        if(r != st1.end()){
            ans++;
            st1.erase(itr);
            st1.erase(r);
        }else{
            break;
        }
    }

    ll t = ans;
    ans = tmpans;
    st1 = st1c;
    st3 = st3c;

    while(true){
        auto itr = st1.begin();
        if(itr == st1.end()){
            break;
        }
        auto r = st3.upper_bound(*itr);
        if(r != st3.end()){
            ans++;
            st1.erase(itr);
            st3.erase(r);
        }else{
            break;
        }
    }

    while(true){
        auto itr = st3.begin();
        if(itr == st3.end()){
            break;
        }
        bool check = true;
        auto m = st3.upper_bound(*itr);
        if(m != st3.end()){
            auto r = st1.upper_bound(*m);
            if(r != st1.end()){
                ans++;
                st3.erase(itr);
                st1.erase(r);
                st3.erase(m);
            }else{
                check = false;
            }
        }else{
            break;
        }
        if(!check){
            break;
        }
    }

    

    while(true){
        auto itr = st1.begin();
        if(itr == st1.end()){
            break;
        }
        auto r = st1.upper_bound(*itr);
        if(r != st1.end()){
            ans++;
            st1.erase(itr);
            st1.erase(r);
        }else{
            break;
        }
    }

    /*while(true){
        auto itr = st1.begin();
        if(itr == st1.end()){
            break;
        }
        bool check = true;
        auto m = st2.upper_bound(*itr);
        if(m != st2.end()){
            auto r = st1.upper_bound(*m);
            if(r != st1.end()){
                ans++;
                st1.erase(itr);
                st1.erase(r);
                st2.erase(m);
            }else{
                check = false;
            }
        }else{
            break;
        }
        if(!check){
            break;
        }
    }*/

    /*while(true){
        auto itr = st3.begin();
        if(itr == st3.end()){
            break;
        }
        bool check = true;
        auto m = st1.upper_bound(*itr);
        if(m != st1.end()){
            auto r = st1.upper_bound(*m);
            if(r != st1.end()){
                ans++;
                st3.erase(itr);
                st1.erase(r);
                st1.erase(m);
            }else{
                check = false;
            }
        }else{
            break;
        }
        if(!check){
            break;
        }
    }*/

    /*while(true){
        auto itr = st3.begin();
        if(itr == st3.end()){
            break;
        }
        bool check = true;
        auto m = st1.upper_bound(*itr);
        if(m != st1.end()){
            auto r = st3.upper_bound(*m);
            if(r != st3.end()){
                ans++;
                st3.erase(itr);
                st3.erase(r);
                st1.erase(m);
            }else{
                check = false;
            }
        }else{
            break;
        }
        if(!check){
            break;
        }
    }*/

    cout << max(ans,t) << endl;

    return 0;
}
0