結果

問題 No.1029 JJOOII 3
ユーザー kcvlexkcvlex
提出日時 2020-04-17 23:09:01
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 899 ms / 2,000 ms
コード長 6,478 bytes
コンパイル時間 1,677 ms
コンパイル使用メモリ 160,760 KB
実行使用メモリ 120,232 KB
最終ジャッジ日時 2024-10-03 15:07:22
合計ジャッジ時間 22,519 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 177 ms
118,148 KB
testcase_01 AC 176 ms
118,116 KB
testcase_02 AC 180 ms
117,936 KB
testcase_03 AC 271 ms
118,308 KB
testcase_04 AC 348 ms
119,508 KB
testcase_05 AC 797 ms
119,940 KB
testcase_06 AC 625 ms
119,308 KB
testcase_07 AC 725 ms
119,664 KB
testcase_08 AC 761 ms
119,704 KB
testcase_09 AC 712 ms
119,580 KB
testcase_10 AC 723 ms
119,700 KB
testcase_11 AC 844 ms
119,980 KB
testcase_12 AC 566 ms
119,068 KB
testcase_13 AC 834 ms
120,000 KB
testcase_14 AC 849 ms
120,232 KB
testcase_15 AC 524 ms
119,376 KB
testcase_16 AC 563 ms
119,564 KB
testcase_17 AC 630 ms
120,004 KB
testcase_18 AC 626 ms
120,048 KB
testcase_19 AC 221 ms
117,952 KB
testcase_20 AC 238 ms
118,180 KB
testcase_21 AC 244 ms
118,100 KB
testcase_22 AC 238 ms
117,952 KB
testcase_23 AC 233 ms
117,940 KB
testcase_24 AC 242 ms
118,116 KB
testcase_25 AC 248 ms
118,048 KB
testcase_26 AC 285 ms
117,944 KB
testcase_27 AC 278 ms
117,972 KB
testcase_28 AC 259 ms
118,000 KB
testcase_29 AC 254 ms
118,024 KB
testcase_30 AC 265 ms
117,956 KB
testcase_31 AC 264 ms
118,188 KB
testcase_32 AC 899 ms
120,216 KB
testcase_33 AC 879 ms
120,088 KB
testcase_34 AC 879 ms
120,216 KB
testcase_35 AC 891 ms
120,212 KB
testcase_36 AC 887 ms
120,192 KB
testcase_37 AC 176 ms
117,976 KB
testcase_38 AC 230 ms
117,976 KB
testcase_39 AC 192 ms
118,076 KB
testcase_40 AC 178 ms
117,956 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <limits>
#include <initializer_list>
#include <utility>
#include <bitset>
#include <tuple>
#include <type_traits>
#include <functional>
#include <string>
#include <array>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <iterator>
#include <algorithm>
#include <complex>
#include <random>
#include <numeric>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <regex>
#include <cassert>
#include <cstddef>
#define endl codeforces
#define ALL(v) std::begin(v), std::end(v)
#define ALLR(v) std::rbegin(v), std::rend(v)
using ll = std::int64_t;
using ull = std::uint64_t;
using pii = std::pair<int, int>;
using tii = std::tuple<int, int, int>;
using pll = std::pair<ll, ll>;
using tll = std::tuple<ll, ll, ll>;
template <typename T> using vec = std::vector<T>;
template <typename T> using vvec = vec<vec<T>>;
template <typename T> const T& var_min(const T &t) { return t; }
template <typename T> const T& var_max(const T &t) { return t; }
template <typename T, typename... Tail> const T& var_min(const T &t, const Tail&... tail) { return std::min(t, var_min(tail...)); }
template <typename T, typename... Tail> const T& var_max(const T &t, const Tail&... tail) { return std::max(t, var_max(tail...)); }
template <typename T, typename... Tail> void chmin(T &t, const Tail&... tail) { t = var_min(t, tail...); }
template <typename T, typename... Tail> void chmax(T &t, const Tail&... tail) { t = var_max(t, tail...); }
template <typename T> const T& clamp(const T &t, const T &low, const T &high) { return std::max(low, std::min(high, t)); }
template <typename T> void chclamp(T &t, const T &low, const T &high) { return t = clamp(t, low, high); }
template <typename T> T make_v(T init) { return init; }
template <typename T, typename... Tail> auto make_v(T init, std::size_t s, Tail... tail) { auto v = std::move(make_v(init, tail...)); return vec<decltype(v)>(s, v); }
template <typename T, std::size_t Head, std::size_t ...Tail> struct multi_dem_array { using type = std::array<typename multi_dem_array<T, Tail...>::type, Head>; };
template <typename T, std::size_t Head> struct multi_dem_array<T, Head> { using type = std::array<T, Head>; };
template <typename T, std::size_t ...Args> using mdarray = typename multi_dem_array<T, Args...>::type;
namespace init__ { struct InitIO { InitIO() { std::cin.tie(nullptr); std::ios_base::sync_with_stdio(false); std::cout << std::fixed << std::setprecision(30); } } init_io; }
    
constexpr std::size_t SIZE = 81;
constexpr ll inf = 1e16;
mdarray<std::string, 2, SIZE, SIZE> substr;
mdarray<std::string, SIZE, SIZE, SIZE> substr2;

struct Solver {
    ll n, k;
    vec<std::pair<std::string, ll>> sv;
    vvec<ll> cnts, lcost;
    mdarray<ll, 2, SIZE, SIZE> minc;

    Solver(ll n, ll k) : n(n), k(k), sv(n), cnts(make_v<ll>(0, 3, n)), lcost(make_v<ll>(inf, 3, k + 1)) {
        for (ll i = 0; i < 2; i++) for (auto &&v : minc[i]) std::fill(ALL(v), inf);
        minc[0][0][0] = minc[1][0][0] = 0;
        for (ll i = 0; i < SIZE; i++) for (ll j = 0; j < SIZE; j++) {
            substr[0][i][j] = std::string(i, 'J') + std::string(j, 'O');
            substr[1][i][j] = std::string(i, 'O') + std::string(j, 'I');
            for (ll l = 0; l < SIZE; l++) substr2[i][j][l] = std::string(i, 'J') + std::string(j, 'O') + std::string(l, 'I');
        }

        ll idx = 0;
        char cs[] = { 'J', 'O', 'I', };
        for (auto &&e : sv) {
            std::string s;
            ll c;
            std::cin >> s >> c;
            e = std::make_pair(s, c);
            for (ll i = 1; i < SIZE; i++) for (ll j = 1; j < SIZE; j++) {
                if (in_s(s, substr[0][i][j])) chmin(minc[0][i][j], c);
                if (in_s(s, substr[1][i][j])) chmin(minc[1][i][j], c);
            }
            for (ll i = 0; i < 3; i++) {
                ll cnt = 0;
                for (char c : s) if (c == cs[i]) cnt++;
                cnts[i][idx] = cnt;
            }
            idx++;
        }
    }

    bool in_s(const std::string &a, const std::string &b) {
        ll idx = 0;
        for (char c : a) {
            if (c == b[idx]) idx++;
            if (idx == b.size()) break;
        }
        return idx == b.size();
    }

    void calc_lc(ll cidx) {
        lcost[cidx][0] = 0;
        for (ll i = 0; i < n; i++) {
            ll cnt = cnts[cidx][i];
            ll cost = sv[i].second;
            for (ll j = 1; j <= std::min(k, cnt); j++) chmin(lcost[cidx][j], cost);
        }
        for (ll i = 0; i < n; i++) {
            ll cnt = cnts[cidx][i];
            ll cost = sv[i].second;
            if (cnt == 0) continue;
            while (true) {
                for (ll j = 0; j <= k; j++) {
                    ll pre = std::max<ll>(j - cnt, 0);
                    chmin(lcost[cidx][std::min<ll>(k, j + cnt)], lcost[cidx][j] + cost);
                }
                if (k <= cnt) break;
                cnt *= 2;
                cost *= 2;
            }
        }
        for (ll j = k; 1 <= j; j--) chmin(lcost[cidx][j - 1], lcost[cidx][j]);
    }

    ll solve() {
        for (ll i = 0; i < 3; i++) calc_lc(i);
        ll ans = inf;
        ll ml = std::min<ll>(SIZE - 1, k);
        if (k <= ml) {
            for (ll i = 0; i < n; i++) for (ll j = 0; j <= ml; j++) {
                if (in_s(sv[i].first, substr2[j][k][k])) chmin(ans, sv[i].second + (0 <= k - j ? lcost[0][k - j] : 0));
                if (in_s(sv[i].first, substr2[k][k][j])) chmin(ans, sv[i].second + (0 <= k - j ? lcost[2][k - j] : 0));
                for (ll l = 0; l <= ml; l++) if (in_s(sv[i].first, substr2[j][k][l])) chmin(ans, sv[i].second + (0 <= k - l ? lcost[0][k - l] : 0) + (0 <= k - j ? lcost[2][k - j] : 0));
            }
        }
        for (ll i = 0; i <= ml; i++) for (ll j = 0; j <= ml; j++) for (ll l = 0; l <= ml; l++) for (ll m = 0; m <= ml; m++) {
            ll tmp = 0;
            tmp += minc[0][i][j];
            tmp += minc[1][l][m];
            ll rest_j = k - i;
            ll rest_o = std::max<ll>(k - (j + l), 0);
            ll rest_i = k - m;
            tmp += lcost[0][rest_j] + lcost[1][rest_o] + lcost[2][rest_i];
            chmin(ans, tmp);
        }
        return ans == inf ? -1 : ans;
    }
};

int main() {
    ll n, k;
    std::cin >> n >> k;
    Solver solver(n, k);
    auto ans = solver.solve();
    std::cout << ans << '\n';
    return 0;
}
0