結果

問題 No.1029 JJOOII 3
ユーザー rxoxixyxdrxoxixyxd
提出日時 2020-04-17 23:17:30
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,004 bytes
コンパイル時間 2,438 ms
コンパイル使用メモリ 171,880 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-04-14 15:32:39
合計ジャッジ時間 4,008 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 6 ms
6,940 KB
testcase_04 AC 16 ms
6,940 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 AC 3 ms
6,944 KB
testcase_16 AC 2 ms
6,944 KB
testcase_17 AC 2 ms
6,944 KB
testcase_18 WA -
testcase_19 AC 2 ms
6,944 KB
testcase_20 AC 2 ms
6,940 KB
testcase_21 AC 2 ms
6,940 KB
testcase_22 AC 3 ms
6,944 KB
testcase_23 AC 3 ms
6,940 KB
testcase_24 AC 2 ms
6,940 KB
testcase_25 AC 3 ms
6,944 KB
testcase_26 WA -
testcase_27 AC 3 ms
6,940 KB
testcase_28 AC 3 ms
6,940 KB
testcase_29 AC 2 ms
6,940 KB
testcase_30 AC 3 ms
6,940 KB
testcase_31 AC 3 ms
6,940 KB
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 AC 2 ms
6,940 KB
testcase_39 AC 1 ms
6,944 KB
testcase_40 WA -
権限があれば一括ダウンロードができます

ソースコード

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 = (1LL<<60);
const double eps = (1e-9);

// num_j 個の J の後ろに O が何個あるか?
pair<int, int> count_num_o(string& s, int num_j, int num_o) {
  int ans = 0;
  int ans_o = 0, ans_i = 0;
  for (char& c : s) {
    if (c == 'J') num_j--;
    if (num_j <= 0 && c == 'O') {
      ans_o++;
      num_j--;
    }
    if (num_o <= 0 && c == 'I') ans_i++;
  }
  return make_pair(ans_o, ans_i);
}

int count_num_i(string& s, int num_o) {
  int ans = 0;
  for (char& c : s) {
    if (c == 'O') num_o--;
    else if (num_o <= 0 && c == 'I') ans++;
  }
  return ans;
}

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

  int n, k;
  cin >> n >> k;

  vector<string> s(n);
  vector<int> c(n);
  rep(i, n) cin >> s[i] >> c[i];
  vector<int> num_j(n, 0), num_o(n, 0), num_i(n, 0);
  rep(i, n) {
    for (char& c : s[i]) {
      if (c == 'J') num_j[i]++;
      else if (c == 'O') num_o[i]++;
      else if (c == 'I') num_i[i]++;
    }
  }

  if (SUM(num_j) == 0 || SUM(num_o) == 0 || SUM(num_i) == 0) {
    puts("-1");
    return 0;
  }

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

  for (int x = 0; x < 3*k; x++) {
    int r_j = k - x;                            // x に居るときに残りの必要なJ
    int r_o = x >= k ? max(0, 2 * k - x) : k;   // x に居るときに残りの必要なO
    int r_i = x >= 2 * k ? 3*k - x : k;         // x に居るときに残りの必要なI
    for (int y = 0; y < n; y++) {
      // 今 x に居て s_y を使用したときにどこに遷移するか?
      if (r_j > 0) {
        if (r_j - num_j[y] > 0) {
          chmin(dp[x + num_j[y]], dp[x] + c[y]);
        } else {
          auto p = count_num_o(s[y], r_j, r_o);
          if (p.second > 0) chmin(dp[min(2*k + p.second, 3*k)], dp[x] + c[y]);
          else chmin(dp[k + p.first], dp[x] + c[y]);
        }
      } else if (r_o > 0) {
        if (r_o - num_o[y] > 0) chmin(dp[x + num_o[y]], dp[x] + c[y]);
        else {
          // r_o 個の O の後ろに I が何個あるか?
          int num = count_num_i(s[y], r_o);
          chmin(dp[min(2*k + num, 3*k)], dp[x] + c[y]);
        }
      } else {
        chmin(dp[min(3*k, x + num_i[y])], dp[x] + c[y]);
      }
    }
  }

//  rep(i, 3*k+1) cerr << dp[i] << " "; cerr << endl;

  cout << dp[3 * k] << endl;

  return 0;
}
0