結果

問題 No.1029 JJOOII 3
ユーザー rxoxixyxdrxoxixyxd
提出日時 2020-04-18 14:17:19
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 4,770 bytes
コンパイル時間 1,893 ms
コンパイル使用メモリ 178,848 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-04-14 20:49:22
合計ジャッジ時間 15,160 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 44 ms
6,940 KB
testcase_04 AC 164 ms
6,944 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 2 ms
6,944 KB
testcase_16 AC 2 ms
6,940 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,944 KB
testcase_21 AC 2 ms
6,944 KB
testcase_22 AC 2 ms
6,940 KB
testcase_23 AC 2 ms
6,940 KB
testcase_24 AC 2 ms
6,944 KB
testcase_25 AC 2 ms
6,944 KB
testcase_26 AC 2 ms
6,940 KB
testcase_27 AC 2 ms
6,940 KB
testcase_28 AC 2 ms
6,940 KB
testcase_29 AC 2 ms
6,944 KB
testcase_30 AC 2 ms
6,944 KB
testcase_31 AC 2 ms
6,940 KB
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 AC 1 ms
6,940 KB
testcase_38 AC 2 ms
6,940 KB
testcase_39 AC 2 ms
6,940 KB
testcase_40 AC 2 ms
6,944 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 = (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]++;
    }
  }

  vector<vector<int>> wj(n, vector<int>());  // 先頭から idx の位置までに J が何個あるか
  vector<vector<int>> wo(n, vector<int>());
  vector<vector<int>> wi(n, vector<int>());
  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];
    }
  }

  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 を使用したときにどこに遷移するか?
      int idx_y = 0;   // s_y の何文字目まで使用したか
      int idx_dp = x;
      string sy = s[y];
      if (r_j > 0) {
        auto itr = lower_bound(ALL(wj[y]), r_j);
        if (itr == wj[y].end()) {
          idx_y = sy.size();
          idx_dp += wj[y][sy.size()-1];
        } else {
          idx_y = itr - wj[y].begin();
          idx_dp = k;
        }
//        cerr << "J: " << idx_dp << endl;
      }

//      if (x == 0 && y == 0) cerr << "idx_y: " << idx_y << "  idx_dp: " << idx_dp << endl;

      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);
//        cerr << "r_o: " << r_o << "  *itr: " << *itr << itr - wo[y].begin() << endl;
        if (itr == wo[y].end()) {
          idx_dp += wo[y].back() - wo[y][idx_y];
          idx_y = sy.size();
//          cerr << "a: idx_dp: " << idx_dp << " " << idx_y << " " << *itr << endl;
        } else {
//          cerr << "b" << endl;
          idx_y = itr - wo[y].begin();
          idx_dp = 2 * k;
        }
//        cerr << "O: " << idx_dp << endl;
      }
//      if (x == 0 && y == 0) cerr << "idx_y: " << idx_y << "  idx_dp: " << idx_dp << endl;
      if (idx_y != s[y].size()) {
        if (idx_y > 0) idx_dp = min(3*k, idx_dp + wi[y].back() - wi[y][idx_y]);
        else idx_dp = min(3*k, idx_dp + wi[y].back());
        // cerr << "I: " << idx_dp << " x: " << x << endl;
      }
//      if (x == 0 && y == 0) cerr << "idx_y: " << idx_y << "  idx_dp: " << idx_dp << endl;
//      if (x == 0 && y == 0) cerr << "idx: " << idx_dp << " c: " << c[y] << endl;
      chmin(dp[idx_dp], dp[x] + c[y]);
      // cerr << "ans: " << dp[3 * k] << endl;
    }
  }

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

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

  return 0;
}
0