結果

問題 No.1029 JJOOII 3
ユーザー MayimgMayimg
提出日時 2020-07-06 16:48:29
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 90 ms / 2,000 ms
コード長 1,377 bytes
コンパイル時間 2,956 ms
コンパイル使用メモリ 218,884 KB
実行使用メモリ 6,924 KB
最終ジャッジ日時 2023-10-24 22:06:22
合計ジャッジ時間 6,152 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 1 ms
4,348 KB
testcase_03 AC 6 ms
4,348 KB
testcase_04 AC 18 ms
5,884 KB
testcase_05 AC 83 ms
6,396 KB
testcase_06 AC 59 ms
5,620 KB
testcase_07 AC 72 ms
6,148 KB
testcase_08 AC 76 ms
6,412 KB
testcase_09 AC 63 ms
5,884 KB
testcase_10 AC 70 ms
6,148 KB
testcase_11 AC 83 ms
6,660 KB
testcase_12 AC 49 ms
5,356 KB
testcase_13 AC 88 ms
6,660 KB
testcase_14 AC 90 ms
6,660 KB
testcase_15 AC 60 ms
5,620 KB
testcase_16 AC 68 ms
6,148 KB
testcase_17 AC 78 ms
6,660 KB
testcase_18 AC 80 ms
6,660 KB
testcase_19 AC 2 ms
4,348 KB
testcase_20 AC 3 ms
4,348 KB
testcase_21 AC 3 ms
4,348 KB
testcase_22 AC 2 ms
4,348 KB
testcase_23 AC 2 ms
4,348 KB
testcase_24 AC 2 ms
4,348 KB
testcase_25 AC 2 ms
4,348 KB
testcase_26 AC 3 ms
4,348 KB
testcase_27 AC 3 ms
4,348 KB
testcase_28 AC 2 ms
4,348 KB
testcase_29 AC 2 ms
4,348 KB
testcase_30 AC 3 ms
4,348 KB
testcase_31 AC 3 ms
4,348 KB
testcase_32 AC 87 ms
6,924 KB
testcase_33 AC 87 ms
6,924 KB
testcase_34 AC 88 ms
6,924 KB
testcase_35 AC 87 ms
6,924 KB
testcase_36 AC 88 ms
6,924 KB
testcase_37 AC 1 ms
4,348 KB
testcase_38 AC 1 ms
4,348 KB
testcase_39 AC 2 ms
4,348 KB
testcase_40 AC 2 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
map<char, int> conv = {{'J', 0}, {'O', 1}, {'I', 2}}; 
signed main() { 
  ios::sync_with_stdio(false); cin.tie(0);
  int n, k;
  cin >> n >> k;
  vector<int> goal = {-1};
  for (int i = 0; i < 3; i++) for (int j = 0; j < k; j++) goal.push_back(i);
  vector<vector<int>> s;
  vector<int> cost;
  vector<vector<int>> cnt;
  for (int i = 0; i < n; i++) {
    string t;
    cin >> t;
    int c;
    cin >> c;
    vector<int> tmp;
    for (auto& ch : t) if (conv.count(ch)) tmp.push_back(conv[ch]);
    if (tmp.empty()) continue;
    vector<int> cc(3);
    for (auto& i : tmp) cc[i]++;
    s.push_back(tmp);
    cost.push_back(c);
    cnt.push_back(cc);
  }
  n = s.size();
  vector<long long> dp(1 + 3 * k, 1LL << 60);
  dp[0] = 0;
  for (int cur = 1; cur <= 3 * k; cur++) {
    for (int i = 0; i < n; i++) {
      int to = min(3 * k, cur + (int) s[i].size() - 1);
      if (goal[cur] == goal[to]) {
        int x = goal[cur];
        to = min(3 * k, cur + cnt[i][x] - 1);
        dp[to] = min(dp[to], dp[cur - 1] + cost[i]);
      } else {
        to = cur;
        for (auto& j : s[i]) {
          if (to <= 3 * k && goal[to] == j) to++;
        }
        to--;
        dp[to] = min(dp[to], dp[cur - 1] + cost[i]);
      }
    }
  }
  cout << (dp[3 * k] == 1LL << 60 ? -1 : dp[3 * k]) << endl;
  return 0;
}
0