結果

問題 No.1029 JJOOII 3
ユーザー finefine
提出日時 2020-04-17 22:46:57
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 262 ms / 2,000 ms
コード長 1,520 bytes
コンパイル時間 2,485 ms
コンパイル使用メモリ 175,572 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-14 15:03:38
合計ジャッジ時間 6,968 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 15 ms
6,944 KB
testcase_04 AC 51 ms
6,940 KB
testcase_05 AC 234 ms
6,944 KB
testcase_06 AC 163 ms
6,940 KB
testcase_07 AC 207 ms
6,940 KB
testcase_08 AC 218 ms
6,944 KB
testcase_09 AC 186 ms
6,944 KB
testcase_10 AC 198 ms
6,944 KB
testcase_11 AC 244 ms
6,940 KB
testcase_12 AC 136 ms
6,944 KB
testcase_13 AC 250 ms
6,944 KB
testcase_14 AC 252 ms
6,944 KB
testcase_15 AC 60 ms
6,940 KB
testcase_16 AC 3 ms
6,940 KB
testcase_17 AC 149 ms
6,944 KB
testcase_18 AC 233 ms
6,940 KB
testcase_19 AC 2 ms
6,944 KB
testcase_20 AC 2 ms
6,944 KB
testcase_21 AC 2 ms
6,940 KB
testcase_22 AC 3 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 2 ms
6,940 KB
testcase_27 AC 3 ms
6,944 KB
testcase_28 AC 2 ms
6,944 KB
testcase_29 AC 2 ms
6,940 KB
testcase_30 AC 3 ms
6,940 KB
testcase_31 AC 2 ms
6,944 KB
testcase_32 AC 260 ms
6,944 KB
testcase_33 AC 259 ms
6,940 KB
testcase_34 AC 258 ms
6,944 KB
testcase_35 AC 259 ms
6,940 KB
testcase_36 AC 262 ms
6,940 KB
testcase_37 AC 2 ms
6,940 KB
testcase_38 AC 2 ms
6,940 KB
testcase_39 AC 2 ms
6,940 KB
testcase_40 AC 2 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

constexpr ll INF = 1e17;

inline char target_char(int nexi, int K) {
    if (nexi < K) return 'J';
    if (nexi < 2 * K) return 'O';
    if (nexi < 3 * K) return 'I';
    return '#';
}

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int n, K;
    cin >> n >> K;

    vector<string> s(n);
    vector<ll> c(n);
    for (int i = 0; i < n; ++i) {
        cin >> s[i] >> c[i];
    }

    vector< map<char, int> > cnt(n);
    //vector< map<char, vector<int> > > warp(n);
    for (int i = 0; i < n; ++i) {
        int sz = s[i].size();
        for (int j = 0; j < sz; ++j) {
            ++cnt[i][s[i][j]];
            //warp[i][s[i][j]].push_back(j + 1);
        }
    }

    vector<ll> dp(3 * K + 1, INF);
    dp[0] = 0;
    for (int i = 0; i < 3 * K; ++i) {
        if (dp[i] >= INF) continue;
        for (int j = 0; j < n; ++j) {
            int sz = s[j].size();

            int nexi = i;
            char tar = target_char(nexi, K);
            int lim = (nexi / K + 1) * K; 
            if (cnt[j][tar] + nexi < lim) {
                nexi += cnt[j][tar];
                dp[nexi] = min(dp[nexi], dp[i] + c[j]);
                continue;
            }

            for (char ch : s[j]) {
                tar = target_char(nexi, K);
                nexi += (tar == ch);
            }
            dp[nexi] = min(dp[nexi], dp[i] + c[j]);
        }
    }
    cout << (dp[3 * K] >= INF ? -1 : dp[3 * K]) << "\n";
    return 0;
}
0