結果

問題 No.1029 JJOOII 3
ユーザー KoDKoD
提出日時 2020-04-17 22:21:05
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 4,262 bytes
コンパイル時間 1,277 ms
コンパイル使用メモリ 95,160 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-14 14:37:52
合計ジャッジ時間 4,193 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 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,944 KB
testcase_04 AC 11 ms
6,944 KB
testcase_05 AC 55 ms
6,940 KB
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 AC 50 ms
6,944 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 AC 42 ms
6,940 KB
testcase_13 AC 55 ms
6,944 KB
testcase_14 WA -
testcase_15 AC 46 ms
6,944 KB
testcase_16 AC 49 ms
6,940 KB
testcase_17 AC 53 ms
6,940 KB
testcase_18 AC 53 ms
6,944 KB
testcase_19 AC 2 ms
6,940 KB
testcase_20 WA -
testcase_21 AC 18 ms
6,944 KB
testcase_22 AC 22 ms
6,940 KB
testcase_23 AC 17 ms
6,944 KB
testcase_24 AC 20 ms
6,940 KB
testcase_25 AC 21 ms
6,944 KB
testcase_26 AC 30 ms
6,940 KB
testcase_27 AC 38 ms
6,944 KB
testcase_28 AC 33 ms
6,940 KB
testcase_29 AC 29 ms
6,944 KB
testcase_30 WA -
testcase_31 AC 32 ms
6,944 KB
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 AC 134 ms
6,944 KB
testcase_36 WA -
testcase_37 AC 2 ms
6,940 KB
testcase_38 WA -
testcase_39 AC 1 ms
6,944 KB
testcase_40 AC 2 ms
6,940 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 + 100;
  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: range(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: range(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: range(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];
    }
  }
  long long 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