結果

問題 No.1029 JJOOII 3
ユーザー risujirohrisujiroh
提出日時 2020-04-17 22:30:13
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 63 ms / 2,000 ms
コード長 1,345 bytes
コンパイル時間 2,464 ms
コンパイル使用メモリ 201,892 KB
最終ジャッジ日時 2025-01-09 20:19:25
ジャッジサーバーID
(参考情報)
judge4 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 38
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

#ifdef LOCAL
#include "debug.h"
#else
#define DEBUG(...)
#endif

template <class T> constexpr T inf = numeric_limits<T>::max() / 2.1;

auto chmin = [](auto&& a, auto b) { return b < a ? a = b, 1 : 0; };

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int n, k;
  cin >> n >> k;
  string t = string(k, 'J') + string(k, 'O') + string(k, 'I');
  int m = t.size();
  vector dp(m + 1, inf<long long>);
  dp[m] = 0;
  vector<string> s(n);
  vector<int> cost(n);
  vector<array<int, 3>> cnt(n);
  for (int i = 0; i < n; ++i) {
    cin >> s[i] >> cost[i];
    cnt[i][0] = count(begin(s[i]), end(s[i]), 'J');
    cnt[i][1] = count(begin(s[i]), end(s[i]), 'O');
    cnt[i][2] = count(begin(s[i]), end(s[i]), 'I');
  }
  for (int j = m; j--; ) {
    for (int i = 0; i < n; ++i) {
      int ptr = j;
      if (j >= 2 * k) {
        ptr = min(ptr + cnt[i][2], m);
      } else if (k <= j and j < 2 * k and j + cnt[i][1] < 2 * k) {
        ptr += cnt[i][1];
      } else if (j < k and j + cnt[i][0] < k) {
        ptr += cnt[i][0];
      } else {
        for (char c : s[i]) {
          if (ptr < m) {
            ptr += c == t[ptr];
          }
        }
      }
      chmin(dp[j], cost[i] + dp[ptr]);
    }
  }
  if (dp[0] >= inf<long long>) {
    dp[0] = -1;
  }
  cout << dp[0] << '\n';
}
0