結果
問題 | No.1029 JJOOII 3 |
ユーザー | SSRS |
提出日時 | 2020-04-17 23:01:27 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 114 ms / 2,000 ms |
コード長 | 4,256 bytes |
コンパイル時間 | 2,141 ms |
コンパイル使用メモリ | 180,968 KB |
実行使用メモリ | 6,820 KB |
最終ジャッジ日時 | 2024-10-03 14:57:20 |
合計ジャッジ時間 | 4,617 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
6,816 KB |
testcase_01 | AC | 2 ms
6,820 KB |
testcase_02 | AC | 2 ms
6,820 KB |
testcase_03 | AC | 5 ms
6,820 KB |
testcase_04 | AC | 8 ms
6,816 KB |
testcase_05 | AC | 44 ms
6,816 KB |
testcase_06 | AC | 41 ms
6,816 KB |
testcase_07 | AC | 41 ms
6,816 KB |
testcase_08 | AC | 46 ms
6,816 KB |
testcase_09 | AC | 39 ms
6,820 KB |
testcase_10 | AC | 43 ms
6,816 KB |
testcase_11 | AC | 49 ms
6,816 KB |
testcase_12 | AC | 35 ms
6,816 KB |
testcase_13 | AC | 45 ms
6,816 KB |
testcase_14 | AC | 47 ms
6,820 KB |
testcase_15 | AC | 2 ms
6,816 KB |
testcase_16 | AC | 2 ms
6,816 KB |
testcase_17 | AC | 2 ms
6,816 KB |
testcase_18 | AC | 41 ms
6,816 KB |
testcase_19 | AC | 2 ms
6,816 KB |
testcase_20 | AC | 18 ms
6,820 KB |
testcase_21 | AC | 18 ms
6,820 KB |
testcase_22 | AC | 20 ms
6,816 KB |
testcase_23 | AC | 16 ms
6,820 KB |
testcase_24 | AC | 18 ms
6,816 KB |
testcase_25 | AC | 19 ms
6,816 KB |
testcase_26 | AC | 28 ms
6,820 KB |
testcase_27 | AC | 35 ms
6,816 KB |
testcase_28 | AC | 29 ms
6,816 KB |
testcase_29 | AC | 26 ms
6,816 KB |
testcase_30 | AC | 32 ms
6,816 KB |
testcase_31 | AC | 29 ms
6,816 KB |
testcase_32 | AC | 113 ms
6,816 KB |
testcase_33 | AC | 114 ms
6,816 KB |
testcase_34 | AC | 112 ms
6,816 KB |
testcase_35 | AC | 113 ms
6,816 KB |
testcase_36 | AC | 113 ms
6,820 KB |
testcase_37 | AC | 2 ms
6,820 KB |
testcase_38 | AC | 2 ms
6,820 KB |
testcase_39 | AC | 2 ms
6,816 KB |
testcase_40 | AC | 2 ms
6,816 KB |
ソースコード
#include <bits/stdc++.h> using namespace std; long long INF = 1000000000000000; int main(){ int N, K; cin >> N >> K; vector<string> S(N); vector<long long> C(N); for (int i = 0; i < N; i++){ cin >> S[i] >> C[i]; } vector<int> J(N); vector<int> O(N); vector<int> I(N); bool Jflg = false; bool Oflg = false; bool Iflg = false; for (int i = 0; i < N; i++){ for (char c : S[i]){ if (c == 'J'){ J[i]++; Jflg = true; } if (c == 'O'){ O[i]++; Oflg = true; } if (c == 'I'){ I[i]++; Iflg = true; } } } if (!(Jflg && Oflg && Iflg)){ cout << -1 << endl; } else { vector<long long> dpJ(K + 1, INF); dpJ[0] = 0; for (int i = 0; i < N; i++){ for (int j = 0; j < min(J[i], K + 1); j++){ dpJ[j] = min(dpJ[j], C[i]); } for (int j = J[i]; j <= K; j++){ dpJ[j] = min(dpJ[j], dpJ[j - J[i]] + C[i]); } } vector<long long> dpO(K + 1, INF); dpO[0] = 0; for (int i = 0; i < N; i++){ for (int j = 0; j < min(O[i], K + 1); j++){ dpO[j] = min(dpO[j], C[i]); } for (int j = O[i]; j <= K; j++){ dpO[j] = min(dpO[j], dpO[j - O[i]] + C[i]); } } vector<long long> dpI(K + 1, INF); dpI[0] = 0; for (int i = 0; i < N; i++){ for (int j = 0; j < min(I[i], K + 1); j++){ dpI[j] = min(dpI[j], C[i]); } for (int j = I[i]; j <= K; j++){ dpI[j] = min(dpI[j], dpI[j - I[i]] + C[i]); } } vector<vector<int>> Jcnt(N); //Jで左から累積和 for (int i = 0; i < N; i++){ Jcnt[i] = vector<int>(S[i].size() + 1, 0); for (int j = 0; j < S[i].size(); j++){ if (S[i][j] == 'J'){ Jcnt[i][j + 1] = Jcnt[i][j] + 1; } else { Jcnt[i][j + 1] = Jcnt[i][j]; } } } vector<vector<int>> Ocnt1(N); //Oで左から累積和 for (int i = 0; i < N; i++){ Ocnt1[i] = vector<int>(S[i].size() + 1, 0); for (int j = 0; j < S[i].size(); j++){ if (S[i][j] == 'O'){ Ocnt1[i][j + 1] = Ocnt1[i][j] + 1; } else { Ocnt1[i][j + 1] = Ocnt1[i][j]; } } } vector<vector<int>> Ocnt2(N); //Oで右から累積和 for (int i = 0; i < N; i++){ Ocnt2[i] = vector<int>(S[i].size() + 1, 0); for (int j = S[i].size() - 1; j >= 0; j--){ if (S[i][j] == 'O'){ Ocnt2[i][j] = Ocnt2[i][j + 1] + 1; } else { Ocnt2[i][j] = Ocnt2[i][j + 1]; } } } vector<vector<int>> Icnt(N); //Iで右から累積和 for (int i = 0; i < N; i++){ Icnt[i] = vector<int>(S[i].size() + 1, 0); for (int j = S[i].size() - 1; j >= 0; j--){ if (S[i][j] == 'I'){ Icnt[i][j] = Icnt[i][j + 1] + 1; } else { Icnt[i][j] = Icnt[i][j + 1]; } } } long long ans = INF; //A: J->O->I for (int i = 0; i < N; i++){ if (O[i] >= K){ vector<int> Opos; for (int j = 0; j < S[i].size(); j++){ if (S[i][j] == 'O'){ Opos.push_back(j); } } for (int j = 0; j <= O[i] - K; j++){ int Jcurr = Jcnt[i][Opos[j]]; int Icurr = Icnt[i][Opos[j + K - 1] + 1]; long long cost = C[i]; if (Jcurr < K){ cost += dpJ[K - Jcurr]; } if (Icurr < K){ cost += dpI[K - Icurr]; } ans = min(ans, cost); } } } //B: J->O, O->I for (int i = 0; i < N; i++){ for (int j = 0; j < N; j++){ for (int k = 0; k < S[i].size() + 1; k++){ for (int l = 0; l < S[j].size() + 1; l++){ int Jcurr = Jcnt[i][k]; int Ocurr = Ocnt2[i][k] + Ocnt1[j][l]; int Icurr = Icnt[j][l]; long long cost = C[i] + C[j]; if (Jcurr < K){ cost += dpJ[K - Jcurr]; } if (Ocurr < K){ cost += dpO[K - Ocurr]; } if (Icurr < K){ cost += dpI[K - Icurr]; } ans = min(ans, cost); } } } } cout << ans << endl; } }