結果

問題 No.1029 JJOOII 3
ユーザー rxoxixyxdrxoxixyxd
提出日時 2020-04-18 16:24:51
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 235 ms / 2,000 ms
コード長 3,230 bytes
コンパイル時間 1,890 ms
コンパイル使用メモリ 174,956 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-14 20:59:36
合計ジャッジ時間 6,519 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 12 ms
6,940 KB
testcase_04 AC 41 ms
6,940 KB
testcase_05 AC 172 ms
6,944 KB
testcase_06 AC 124 ms
6,944 KB
testcase_07 AC 150 ms
6,944 KB
testcase_08 AC 172 ms
6,944 KB
testcase_09 AC 145 ms
6,940 KB
testcase_10 AC 153 ms
6,944 KB
testcase_11 AC 198 ms
6,944 KB
testcase_12 AC 99 ms
6,944 KB
testcase_13 AC 189 ms
6,940 KB
testcase_14 AC 189 ms
6,944 KB
testcase_15 AC 126 ms
6,940 KB
testcase_16 AC 144 ms
6,940 KB
testcase_17 AC 169 ms
6,940 KB
testcase_18 AC 172 ms
6,940 KB
testcase_19 AC 2 ms
6,940 KB
testcase_20 AC 2 ms
6,944 KB
testcase_21 AC 3 ms
6,944 KB
testcase_22 AC 2 ms
6,940 KB
testcase_23 AC 2 ms
6,944 KB
testcase_24 AC 2 ms
6,944 KB
testcase_25 AC 2 ms
6,940 KB
testcase_26 AC 2 ms
6,944 KB
testcase_27 AC 2 ms
6,944 KB
testcase_28 AC 3 ms
6,944 KB
testcase_29 AC 2 ms
6,940 KB
testcase_30 AC 2 ms
6,944 KB
testcase_31 AC 2 ms
6,944 KB
testcase_32 AC 235 ms
6,944 KB
testcase_33 AC 229 ms
6,940 KB
testcase_34 AC 230 ms
6,940 KB
testcase_35 AC 234 ms
6,944 KB
testcase_36 AC 234 ms
6,944 KB
testcase_37 AC 2 ms
6,940 KB
testcase_38 AC 2 ms
6,944 KB
testcase_39 AC 2 ms
6,944 KB
testcase_40 AC 2 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for (ll i = 0; i < n; ++i)
#define sz(x) int(x.size())
#define ALL(c) (c).begin(), (c).end()
#define SUM(x) std::accumulate(ALL(x), 0LL)
#define MIN(v) *std::min_element(v.begin(), v.end())
#define MAX(v) *std::max_element(v.begin(), v.end())
#define EXIST(v, x) (std::find(v.begin(), v.end(), x) != v.end())
#define FORV(i, v) for (auto i = v.begin(); i != v.end(); i++)
using namespace std;

using ll = int64_t;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
const int INF = 1001001001;
const long long INFL = LLONG_MAX / 2;
const double eps = (1e-9);

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  ll n, k;
  cin >> n >> k;

  vector<string> s(n);
  vector<ll> c(n);
  rep(i, n) cin >> s[i] >> c[i];

  vector<vector<ll>> wj(n, vector<ll>());  // 先頭から idx の位置までに J が何個あるか
  vector<vector<ll>> wo(n, vector<ll>());
  vector<vector<ll>> wi(n, vector<ll>());
  rep(i, n) {
    wj[i].resize(s[i].size(), 0);
    wo[i].resize(s[i].size(), 0);
    wi[i].resize(s[i].size(), 0);
    if (s[i][0] == 'J') wj[i][0] = 1;
    if (s[i][0] == 'O') wo[i][0] = 1;
    if (s[i][0] == 'I') wi[i][0] = 1;
    for (int j = 1; j < s[i].size(); j++) {
      if (s[i][j] == 'J') wj[i][j] = wj[i][j-1] + 1;
      else wj[i][j] = wj[i][j-1];
      if (s[i][j] == 'O') wo[i][j] = wo[i][j-1] + 1;
      else wo[i][j] = wo[i][j-1];
      if (s[i][j] == 'I') wi[i][j] = wi[i][j-1] + 1;
      else wi[i][j] = wi[i][j-1];
    }
  }

  vector<ll> dp(3*k+5, INFL);
  dp[0] = 0;

  for (ll x = 0; x < 3*k; x++) {
    ll r_j = max(ll(0), k - x);                        // x に居るときに残りの必要なJ
    ll r_o = x >= 2 * k ? 0 : max(ll(0), min(k, 2 * k - x));   // x に居るときに残りの必要なO
    ll r_i = max(3 * k - x, k);                      // x に居るときに残りの必要なI

    for (int y = 0; y < n; y++) {
      ll idx_dp = x;  // 今 x に居て s_y を使用したときにどこに遷移するか?
      ll idx_y = 0;   // s_y の何文字目まで使用したか

      if (r_j > 0) {
        auto itr = lower_bound(ALL(wj[y]), r_j);
        if (itr == wj[y].end()) {
          idx_y = s[y].size();
          idx_dp += wj[y].back();
        } else {
          idx_y = itr - wj[y].begin();
          idx_dp = k;
        }
      }

      if (r_o > 0 && idx_y != s[y].size()) {
        if (idx_y > 0) r_o += wo[y][idx_y];
        auto itr = lower_bound(ALL(wo[y]), r_o);
        if (itr == wo[y].end()) {
          idx_dp += wo[y].back();
          if (idx_y > 0) idx_dp -= wo[y][idx_y];
          idx_y = s[y].size();
        } else {
          idx_y = itr - wo[y].begin();
          idx_dp = 2 * k;
        }
      }

      if (idx_y != s[y].size()) {
        if (idx_y > 0) idx_dp = idx_dp + wi[y].back() - wi[y][idx_y];
        else idx_dp = idx_dp + wi[y].back();
        chmin(idx_dp, 3*k);
      }
      if (idx_dp == x) continue;

      chmin(dp[idx_dp], dp[x] + c[y]);
    }
  }

  if (dp[3*k] == INFL) puts("-1");
  else cout << dp[3 * k] << endl;

  return 0;
}
0