結果

問題 No.1029 JJOOII 3
ユーザー TAISA_TAISA_
提出日時 2020-04-17 22:13:54
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,783 bytes
コンパイル時間 2,514 ms
コンパイル使用メモリ 211,252 KB
実行使用メモリ 68,788 KB
最終ジャッジ日時 2023-07-27 01:05:58
合計ジャッジ時間 8,624 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 14 ms
6,500 KB
testcase_04 AC 39 ms
16,256 KB
testcase_05 AC 201 ms
60,312 KB
testcase_06 AC 158 ms
43,320 KB
testcase_07 AC 176 ms
53,664 KB
testcase_08 AC 193 ms
56,640 KB
testcase_09 AC 170 ms
49,772 KB
testcase_10 AC 178 ms
52,108 KB
testcase_11 AC 218 ms
64,476 KB
testcase_12 AC 131 ms
36,352 KB
testcase_13 AC 214 ms
64,928 KB
testcase_14 AC 220 ms
65,800 KB
testcase_15 AC 144 ms
44,948 KB
testcase_16 AC 148 ms
51,788 KB
testcase_17 AC 178 ms
60,452 KB
testcase_18 AC 194 ms
61,236 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 27 ms
4,380 KB
testcase_21 AC 25 ms
4,376 KB
testcase_22 AC 30 ms
4,376 KB
testcase_23 AC 24 ms
4,376 KB
testcase_24 AC 27 ms
4,380 KB
testcase_25 AC 30 ms
4,376 KB
testcase_26 AC 42 ms
4,376 KB
testcase_27 AC 54 ms
4,376 KB
testcase_28 AC 45 ms
4,376 KB
testcase_29 AC 40 ms
4,376 KB
testcase_30 AC 51 ms
4,380 KB
testcase_31 AC 45 ms
4,376 KB
testcase_32 AC 332 ms
68,788 KB
testcase_33 AC 329 ms
68,772 KB
testcase_34 AC 332 ms
68,680 KB
testcase_35 AC 329 ms
68,760 KB
testcase_36 AC 330 ms
68,688 KB
testcase_37 AC 2 ms
4,376 KB
testcase_38 WA -
testcase_39 AC 2 ms
4,376 KB
testcase_40 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define all(vec) vec.begin(), vec.end()
#define pb push_back
#define eb emplace_back
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
template <class T>
using V = vector<T>;
constexpr ll INF = (1LL << 60) - 1LL;
constexpr ll MOD = 998244353LL;
constexpr int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
template <class T>
void chmin(T &a, T b) { a = min(a, b); }
template <class T>
void chmax(T &a, T b) { a = max(a, b); }
void debug() { cerr << "ok" << endl; }
template <class T>
void vout(const vector<T> &v) {
    for (int i = 0; i < v.size(); i++) {
        cout << v[i] << (i + 1 == v.size() ? '\n' : ' ');
    }
}
int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    int n, K;
    cin >> n >> K;
    V<string> s(n);
    V<ll> c(n);
    for (int i = 0; i < n; i++) {
        cin >> s[i] >> c[i];
    }
    auto calc = [&](char cc) {
        V<V<ll>> dp(n + 1, V<ll>(K + 200, INF));
        dp[0][0] = 0;
        for (int i = 0; i < n; i++) {
            int co = 0;
            for (auto &e : s[i]) {
                if (e == cc) {
                    co++;
                }
            }
            for (int j = 0; j <= K + 100; j++) {
                chmin(dp[i + 1][j], dp[i][j]);
                if (j >= co) {
                    chmin(dp[i + 1][j], dp[i + 1][j - co] + c[i]);
                }
            }
        }
        return dp[n];
    };
    V<ll> dpj = calc('J'), dpo = calc('O'), dpi = calc('I');
    for (int i = K; i >= 0; i--) {
        chmin(dpj[i], dpj[i + 1]);
        chmin(dpo[i], dpo[i + 1]);
        chmin(dpi[i], dpi[i + 1]);
    }
    V<V<int>> cco(n), cci(n), ccj(n);
    ll res = INF;
    for (int i = 0; i < n; i++) {
        cco[i].resize(s[i].size() + 1);
        cci[i].resize(s[i].size() + 1);
        ccj[i].resize(s[i].size() + 1);
        for (int j = 0; j < s[i].size(); j++) {
            ccj[i][j + 1] = ccj[i][j];
            cco[i][j + 1] = cco[i][j];
            cci[i][j + 1] = cci[i][j];
            if (s[i][j] == 'J') {
                ccj[i][j + 1]++;
            } else if (s[i][j] == 'O') {
                cco[i][j + 1]++;
            } else {
                cci[i][j + 1]++;
            }
        }
    }
    for (int i = 0; i < n; i++) {
        int cj = 0;
        for (int j = 0; j < s[i].size(); j++) {
            int co = 0;
            for (int k = j; k < s[i].size(); k++) {
                if (s[i][k] == 'O') co++;
                if (co >= K) {
                    int toj = K - cj, toi = K - (cci[i][s[i].size()] - cci[i][k]);
                    chmax(toj, 0);
                    chmax(toi, 0);
                    if (dpj[toj] != INF && dpi[toi] != INF) {
                        chmin(res, dpj[toj] + dpi[toi] + c[i]);
                    }
                }
            }
            if (s[i][j] == 'J') {
                cj++;
            }
        }
        cj = 0;
        for (int j = 0; j < s[i].size(); j++) {
            int co = cco[i][s[i].size()] - cco[i][j];
            for (int k = 0; k < n; k++) {
                int co2 = 0;
                for (int l = 0; l < s[k].size(); l++) {
                    if (s[k][l] == 'O') {
                        co2++;
                    }
                    int toj = K - cj, toi = K - (cci[k][s[k].size()] - cci[k][l]), too = K - co - co2;
                    chmax(toj, 0);
                    chmax(toi, 0);
                    chmax(too, 0);
                    if (dpj[toj] != INF && dpi[toi] != INF && dpo[too] != INF) {
                        chmin(res, c[i] + c[k] + dpj[toj] + dpi[toi] + dpo[too]);
                    }
                }
            }
            if (s[i][j] == 'J') {
                cj++;
            }
        }
    }
    cout << (res == INF ? -1LL : res) << '\n';
}
0