結果

問題 No.1029 JJOOII 3
ユーザー KoDKoD
提出日時 2020-04-17 22:33:21
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 166 ms / 2,000 ms
コード長 4,264 bytes
コンパイル時間 1,206 ms
コンパイル使用メモリ 93,124 KB
実行使用メモリ 7,848 KB
最終ジャッジ日時 2023-07-27 01:29:22
合計ジャッジ時間 4,914 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,384 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 8 ms
4,380 KB
testcase_04 AC 16 ms
6,604 KB
testcase_05 AC 81 ms
7,336 KB
testcase_06 AC 68 ms
5,992 KB
testcase_07 AC 72 ms
6,788 KB
testcase_08 AC 80 ms
7,012 KB
testcase_09 AC 70 ms
6,604 KB
testcase_10 AC 74 ms
6,868 KB
testcase_11 AC 89 ms
7,536 KB
testcase_12 AC 58 ms
5,428 KB
testcase_13 AC 84 ms
7,604 KB
testcase_14 AC 87 ms
7,528 KB
testcase_15 AC 65 ms
6,276 KB
testcase_16 AC 71 ms
6,780 KB
testcase_17 AC 78 ms
7,268 KB
testcase_18 AC 80 ms
7,784 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 19 ms
4,376 KB
testcase_21 AC 18 ms
4,380 KB
testcase_22 AC 21 ms
4,376 KB
testcase_23 AC 17 ms
4,376 KB
testcase_24 AC 19 ms
4,376 KB
testcase_25 AC 21 ms
4,376 KB
testcase_26 AC 30 ms
4,380 KB
testcase_27 AC 37 ms
4,376 KB
testcase_28 AC 32 ms
4,380 KB
testcase_29 AC 28 ms
4,376 KB
testcase_30 AC 36 ms
4,380 KB
testcase_31 AC 32 ms
4,380 KB
testcase_32 AC 165 ms
7,796 KB
testcase_33 AC 166 ms
7,848 KB
testcase_34 AC 165 ms
7,840 KB
testcase_35 AC 165 ms
7,848 KB
testcase_36 AC 166 ms
7,796 KB
testcase_37 AC 1 ms
4,380 KB
testcase_38 AC 2 ms
4,376 KB
testcase_39 AC 1 ms
4,380 KB
testcase_40 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <utility>
#include <vector>
#include <numeric>

template <class T, class U>
inline bool chmin(T &lhs, const U &rhs) {
  if (lhs > rhs) {
    lhs = rhs;
    return true;
  }
  return false;
}

template <class T, class U>
inline bool chmax(T &lhs, const U &rhs) {
  if (lhs < rhs) {
    lhs = rhs;
    return true;
  }
  return false;
}

// [l, r) from l to r
struct range {
  struct itr {
    int i;
    constexpr itr(int i_): i(i_) { }
    constexpr void operator ++ () { ++i; }
    constexpr int operator * () const { return i; }
    constexpr bool operator != (itr x) const { return i != x.i; }
  };
  const itr l, r;
  constexpr range(int l_, int r_): l(l_), r(std::max(l_, r_)) { }
  constexpr itr begin() const { return l; }
  constexpr itr end() const { return r; }
};

// [l, r) from r to l
struct revrange {
  struct itr {
    int i;
    constexpr itr(int i_): i(i_) { }
    constexpr void operator ++ () { --i; }
    constexpr int operator * () const { return i; }
    constexpr bool operator != (itr x) const { return i != x.i; }
  };
  const itr l, r;
  constexpr revrange(int l_, int r_): l(l_ - 1), r(std::max(l_, r_) - 1) { }
  constexpr itr begin() const { return r; }
  constexpr itr end() const { return l; }
};

using lint = long long;
constexpr lint inf = (1ll << 60);

int main() {
  int N, size, K;
  std::cin >> N >> size;
  K = size * 2;
  std::vector<std::string> str(N);
  std::vector<int> cost(N), J(N), O(N), I(N);
  for (int i: range(0, N)) {
    std::cin >> str[i] >> cost[i];
    J[i] = std::count(str[i].cbegin(), str[i].cend(), 'J');
    O[i] = std::count(str[i].cbegin(), str[i].cend(), 'O');
    I[i] = std::count(str[i].cbegin(), str[i].cend(), 'I');
  }
  std::vector<lint> jdp(K + 1, inf), odp(K + 1, inf), idp(K + 1, inf);
  jdp.front() = 0;
  for (int i: range(0, N)) {
    for (int k: range(0, K + 1)) {
      if (k - J[i] >= 0) {
        chmin(jdp[k], jdp[k - J[i]] + cost[i]);
      } 
    }
  }
  for (int k: revrange(0, K)) {
    chmin(jdp[k], jdp[k + 1]);
  }
  odp.front() = 0;
  for (int i: range(0, N)) {
    for (int k: range(0, K + 1)) {
      if (k - O[i] >= 0) {
        chmin(odp[k], odp[k - O[i]] + cost[i]);
      } 
    }
  }
  for (int k: revrange(0, K)) {
    chmin(odp[k], odp[k + 1]);
  }
  idp.front() = 0;
  for (int i: range(0, N)) {
    for (int k: range(0, K + 1)) {
      if (k - I[i] >= 0) {
        chmin(idp[k], idp[k - I[i]] + cost[i]);
      } 
    }
  }
  for (int k: revrange(0, K)) {
    chmin(idp[k], idp[k + 1]);
  }
  std::vector<std::vector<std::vector<int>>> count(N);
  for (int i: range(0, N)) {
    count[i].assign(3, std::vector<int>(str[i].size() + 1));
    for (int j: range(0, str[i].size())) {
      if (str[i][j] == 'J') {
        ++count[i][0][j + 1];
      }
      count[i][0][j + 1] += count[i][0][j];
    }
    for (int j: range(0, str[i].size())) {
      if (str[i][j] == 'O') {
        ++count[i][1][j + 1];
      }
      count[i][1][j + 1] += count[i][1][j];
    }
    for (int j: range(0, str[i].size())) {
      if (str[i][j] == 'I') {
        ++count[i][2][j + 1];
      }
      count[i][2][j + 1] += count[i][2][j];
    }
  }
  lint ans = inf;
  for (int i: range(0, N)) {
    for (int j: range(0, N)) {
      for (int k: range(0, str[i].size() + 1)) {
        for (int l: range(0, str[j].size() + 1)) {
          int nj = size - (count[i][0][k] - count[i][0][0]);
          int no = size - (count[i][1][str[i].size()] - count[i][1][k]) - (count[j][1][l] - count[j][1][0]);
          int ni = size - (count[j][2][str[j].size()] - count[j][2][l]);
          chmax(nj, 0);
          chmax(no, 0);
          chmax(ni, 0);
          chmin(ans, jdp[nj] + odp[no] + idp[ni] + cost[i] + cost[j]);
        }
      }
    }
  }
  for (int i: range(0, N)) {
    for (int r: range(0, str[i].size() + 1)) {
      for (int l: range(0, r)) {
        if (count[i][1][r] - count[i][1][l] < size) {
          continue;
        }
        int nj = size - (count[i][0][l] - count[i][0][0]);
        int ni = size - (count[i][2][str[i].size()] - count[i][2][r]);
        chmax(nj, 0);
        chmax(ni, 0);
        chmin(ans, jdp[nj] + idp[ni] + cost[i]);
      }
    }
  }
  std::cout << (ans == inf ? -1 : ans) << '\n';
  return 0;
}
0