結果

問題 No.1029 JJOOII 3
ユーザー IKyoproIKyopro
提出日時 2020-04-17 23:55:21
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 66 ms / 2,000 ms
コード長 2,338 bytes
コンパイル時間 2,594 ms
コンパイル使用メモリ 207,700 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-14 16:14:46
合計ジャッジ時間 5,183 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 5 ms
6,944 KB
testcase_04 AC 14 ms
6,944 KB
testcase_05 AC 55 ms
6,944 KB
testcase_06 AC 38 ms
6,940 KB
testcase_07 AC 51 ms
6,944 KB
testcase_08 AC 52 ms
6,940 KB
testcase_09 AC 46 ms
6,940 KB
testcase_10 AC 49 ms
6,940 KB
testcase_11 AC 54 ms
6,940 KB
testcase_12 AC 32 ms
6,940 KB
testcase_13 AC 60 ms
6,944 KB
testcase_14 AC 63 ms
6,940 KB
testcase_15 AC 40 ms
6,944 KB
testcase_16 AC 44 ms
6,940 KB
testcase_17 AC 56 ms
6,944 KB
testcase_18 AC 59 ms
6,944 KB
testcase_19 AC 2 ms
6,944 KB
testcase_20 AC 3 ms
6,944 KB
testcase_21 AC 3 ms
6,940 KB
testcase_22 AC 2 ms
6,940 KB
testcase_23 AC 2 ms
6,940 KB
testcase_24 AC 2 ms
6,940 KB
testcase_25 AC 2 ms
6,944 KB
testcase_26 AC 3 ms
6,944 KB
testcase_27 AC 3 ms
6,944 KB
testcase_28 AC 3 ms
6,940 KB
testcase_29 AC 2 ms
6,940 KB
testcase_30 AC 3 ms
6,940 KB
testcase_31 AC 3 ms
6,944 KB
testcase_32 AC 61 ms
6,940 KB
testcase_33 AC 65 ms
6,940 KB
testcase_34 AC 65 ms
6,944 KB
testcase_35 AC 63 ms
6,940 KB
testcase_36 AC 66 ms
6,940 KB
testcase_37 AC 2 ms
6,940 KB
testcase_38 AC 2 ms
6,944 KB
testcase_39 AC 2 ms
6,940 KB
testcase_40 AC 2 ms
6,940 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
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