結果

問題 No.1029 JJOOII 3
ユーザー IKyopro
提出日時 2020-04-17 23:55:21
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 73 ms / 2,000 ms
コード長 2,338 bytes
コンパイル時間 2,737 ms
コンパイル使用メモリ 204,892 KB
最終ジャッジ日時 2025-01-09 20:52:54
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 38
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In lambda function:
main.cpp:26:5: warning: control reaches end of non-void function [-Wreturn-type]
   26 |     };
      |     ^

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <class T, class U> using Pa = pair<T, U>;
template <class T> using vec = vector<T>;
template <class T> using vvec = vector<vec<T>>;
template <class T> using vvvec = vector<vvec<T>>;
template <class T> using vvvvec = vector<vvvec<T>>;

void chmin(ll &a,ll b){if(a>b) a = b;}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N,K;
    cin >> N >> K;
    vec<string> S(N);
    vec<int> C(N);
    vvec<int> cnt(N,vec<int>(3));
    vvvec<int> pos(N,vvec<int>(81,vec<int>(3)));

    auto id = [](char c){
        if(c=='J') return 0;
        if(c=='O') return 1;
        if(c=='I') return 2;
    };

    for(int i=0;i<N;i++){
        cin >> S[i] >> C[i];
        for(auto& c:S[i]){
            cnt[i][id(c)]++;
        }
        int n = S[i].size();
        for(int j=0;j<n;j++){
            int ord = 1;
            for(int k=j-1;k>=0;k--) if(S[i][j]==S[i][k]) ord++;
            pos[i][ord][id(S[i][j])] = j;
        }
    }
    ll inf = 1e18;
    vec<ll> dp(3*K+1,inf);
    dp[0] = 0;
    for(int i=0;i<3*K;i++) for(int j=0;j<N;j++){
        if(i<K){
            if(i+cnt[j][0]<K){
                chmin(dp[i+cnt[j][0]],dp[i]+C[j]);
            }else{
                int n = pos[j][K-i][0];
                int now = n;
                int c = 0;
                for(int k=n+1;k<S[j].size();k++){
                    c += (S[j][k]=='O');
                    now = k;
                    if(c==K){
                        break;
                    }
                }
                if(c<K){
                    chmin(dp[K+c],dp[i]+C[j]);   
                }else{
                    c = 0;
                    for(int k=now+1;k<S[j].size();k++) c += (S[j][k]=='I');
                    chmin(dp[min(3*K,2*K+c)],dp[i]+C[j]);
                }
            }
        }else if(i<2*K){
            if(i+cnt[j][1]<2*K){
                chmin(dp[i+cnt[j][1]],dp[i]+C[j]);             
            }else{
                int n = pos[j][2*K-i][1];
                int c = 0;
                for(int k=n+1;k<S[j].size();k++) c += (S[j][k]=='I');
                chmin(dp[min(3*K,2*K+c)],dp[i]+C[j]);
            }
        }else{
            chmin(dp[min(3*K,i+cnt[j][2])],dp[i]+C[j]);
        }
    }
    cout << (dp[3*K]!=inf? dp[3*K]:-1) << "\n";
}
0