結果

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

ソースコード

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;
        }
    }*/

    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;
        }
    }

    cerr << ans << endl;

    /*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;
        }
    }

    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;
        }
    }

    cerr << st1.size() << endl;
    cerr << st2.size() << endl;
    cerr << st3.size() << endl;

    cout << ans << endl;

    return 0;
}
0