結果

問題 No.1029 JJOOII 3
ユーザー tsutajtsutaj
提出日時 2020-04-17 22:32:50
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 91 ms / 2,000 ms
コード長 3,380 bytes
コンパイル時間 1,232 ms
コンパイル使用メモリ 114,340 KB
実行使用メモリ 6,080 KB
最終ジャッジ日時 2023-07-27 01:28:13
合計ジャッジ時間 3,956 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 4 ms
4,380 KB
testcase_04 AC 13 ms
5,136 KB
testcase_05 AC 91 ms
5,572 KB
testcase_06 AC 54 ms
5,096 KB
testcase_07 AC 52 ms
5,296 KB
testcase_08 AC 55 ms
5,468 KB
testcase_09 AC 46 ms
5,160 KB
testcase_10 AC 64 ms
5,484 KB
testcase_11 AC 59 ms
5,804 KB
testcase_12 AC 41 ms
4,748 KB
testcase_13 AC 64 ms
5,824 KB
testcase_14 AC 74 ms
5,700 KB
testcase_15 AC 25 ms
5,156 KB
testcase_16 AC 4 ms
5,340 KB
testcase_17 AC 36 ms
5,648 KB
testcase_18 AC 58 ms
5,680 KB
testcase_19 AC 2 ms
4,376 KB
testcase_20 AC 6 ms
4,376 KB
testcase_21 AC 5 ms
4,376 KB
testcase_22 AC 6 ms
4,380 KB
testcase_23 AC 5 ms
4,380 KB
testcase_24 AC 6 ms
4,376 KB
testcase_25 AC 5 ms
4,376 KB
testcase_26 AC 6 ms
4,376 KB
testcase_27 AC 9 ms
4,380 KB
testcase_28 AC 7 ms
4,376 KB
testcase_29 AC 6 ms
4,376 KB
testcase_30 AC 9 ms
4,380 KB
testcase_31 AC 8 ms
4,376 KB
testcase_32 AC 59 ms
5,824 KB
testcase_33 AC 59 ms
5,892 KB
testcase_34 AC 59 ms
6,080 KB
testcase_35 AC 59 ms
5,884 KB
testcase_36 AC 60 ms
5,820 KB
testcase_37 AC 2 ms
4,376 KB
testcase_38 AC 1 ms
4,380 KB
testcase_39 AC 2 ms
4,376 KB
testcase_40 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// #define _GLIBCXX_DEBUG // for STL debug (optional)
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <fstream>
#include <functional>
#include <bitset>
using namespace std;
using ll = long long int;
using int64 = long long int;
 
template<typename T> void chmax(T &a, T b) {a = max(a, b);}
template<typename T> void chmin(T &a, T b) {a = min(a, b);}
template<typename T> void chadd(T &a, T b) {a = a + b;}
 
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
const int INF = 1LL << 29;
const ll LONGINF = 1LL << 60;
const ll MOD = 1000000007LL;

ll dp1[100010][3], dp2[250];
int inc[85][3], nxt[85][3], pre[85][85][3], suf[85][85][3];

int main() {
    int N, K; scanf("%d%d", &N, &K);
    vector<string> S(N);
    vector<ll> C(N);
    for(int i=0; i<N; i++) cin >> S[i], scanf("%lld", &C[i]);

    const string pat = "JOI";
    vector< vector< vector<int> > > pos(N, vector< vector<int> >(3));
    for(int i=0; i<N; i++) {
        int L = S[i].size();
        for(int j=0; j<L; j++) {
            pos[i][ pat.find(S[i][j]) ].emplace_back(j);
        }
    }
    
    ll ans = LONGINF;
    if(K <= 80) {
        fill(dp2, dp2 + 3*K + 1, LONGINF);
        dp2[0] = 0;
        for(int p=0; p<3*K; p++) {
            for(int i=0; i<N; i++) {
                int L = S[i].size(), ptr = p;
                for(int j=0; j<L and ptr < 3*K; j++) {
                    int t = ptr / K;
                    ptr += (S[i][j] == pat[t]);
                }
                chmin(dp2[ptr], dp2[p] + C[i]);
            }
        }
        ans = dp2[3*K];
    }
    else {
        fill(dp1[0], dp1[K+1], LONGINF);
        dp1[0][0] = 0;

        for(int x=0; x<3; x++) {
            for(int p=0; p<K; p++) {
                if(dp1[p][x] == LONGINF) continue;
                for(int i=0; i<N; i++) {
                    int rem = K - p, ava = pos[i][x].size();
                    if(rem > ava) {
                        int np = p + ava;
                        chmin(dp1[np][x], dp1[p][x] + C[i]);
                    }
                    else {
                        ava = min(ava, rem);
                        int cur = pos[i][x][ava - 1];
                        
                        if(x == 2) {
                            chmin(dp1[K][x], dp1[p][x] + C[i]);
                        }
                        else {
                            int nx = x + 1;
                            int c = upper_bound(pos[i][nx].begin(),
                                                pos[i][nx].end(),
                                                cur) - pos[i][nx].begin();
                            int cnt = (int)pos[i][nx].size() - c;
                            // fprintf(stderr, "i = %d: p = %d, x = %d, cnt = %d\n",i, p, x, cnt);
                            chmin(dp1[cnt][nx], dp1[p][x] + C[i]);
                        }
                    }
                }
            }
        }
        
        ans = dp1[K][2];
    }
    
    if(ans == LONGINF) cout << -1 << endl;
    else cout << ans << endl;
    return 0;
}
0