結果

問題 No.1029 JJOOII 3
ユーザー Mayimg
提出日時 2020-07-06 16:48:29
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 91 ms / 2,000 ms
コード長 1,377 bytes
コンパイル時間 2,233 ms
コンパイル使用メモリ 210,800 KB
最終ジャッジ日時 2025-01-11 16:18:05
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 38
権限があれば一括ダウンロードができます

ソースコード

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