結果
問題 | No.1029 JJOOII 3 |
ユーザー | kimiyuki |
提出日時 | 2020-04-17 22:23:57 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 466 ms / 2,000 ms |
コード長 | 4,097 bytes |
コンパイル時間 | 2,485 ms |
コンパイル使用メモリ | 203,668 KB |
最終ジャッジ日時 | 2025-01-09 20:16:36 |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 38 |
ソースコード
#line 1 "main.cpp" #include <bits/stdc++.h> #line 2 "/home/user/GitHub/competitive-programming-library/utils/macros.hpp" #define REP(i, n) for (int i = 0; (i) < (int)(n); ++ (i)) #define REP3(i, m, n) for (int i = (m); (i) < (int)(n); ++ (i)) #define REP_R(i, n) for (int i = (int)(n) - 1; (i) >= 0; -- (i)) #define REP3R(i, m, n) for (int i = (int)(n) - 1; (i) >= (int)(m); -- (i)) #define ALL(x) std::begin(x), std::end(x) #line 4 "/home/user/GitHub/competitive-programming-library/utils/binary_search.hpp" /** * @brief a binary search / 二分探索 * @param[in] p a monotone predicate defined on $[l, r)$ * @return $\min \lbrace x \in [l, r) \mid p(x) \rbrace$, or r if it doesn't exist */ template <typename UnaryPredicate> int64_t binsearch(int64_t l, int64_t r, UnaryPredicate p) { assert (l <= r); -- l; while (r - l > 1) { int64_t m = l + (r - l) / 2; (p(m) ? r : l) = m; } return r; } /** * @return $\max \lbrace x \in (l, r] \mid p(x) \rbrace$, or l if it doesn't exist */ template <typename UnaryPredicate> int64_t binsearch_max(int64_t l, int64_t r, UnaryPredicate p) { assert (l <= r); ++ r; while (r - l > 1) { int64_t m = l + (r - l) / 2; (p(m) ? l : r) = m; } return l; } #line 4 "main.cpp" using namespace std; template <class T, class U> inline void chmin(T & a, U const & b) { a = min<T>(a, b); } int64_t solve(int n, int k, const vector<string> & s, const vector<int64_t> & cost) { vector<vector<int> > acc_a(n); vector<vector<int> > acc_b(n); vector<vector<int> > acc_c(n); REP (i, n) { acc_a[i].resize(s[i].size() + 1); acc_b[i].resize(s[i].size() + 1); acc_c[i].resize(s[i].size() + 1); REP (j, s[i].size()) { acc_a[i][j + 1] = acc_a[i][j] + (s[i][j] == 'J'); acc_b[i][j + 1] = acc_b[i][j] + (s[i][j] == 'O'); acc_c[i][j + 1] = acc_c[i][j] + (s[i][j] == 'I'); } } vector<int64_t> dp(3 * k + 1, INT64_MAX); dp[0] = 0; REP (j, k) if (dp[j] != INT64_MAX) { REP (i, n) { int m = s[i].size(); int l = 0; int r = binsearch(l, m, [&](int r) { return j + acc_a[i][r] - acc_a[i][l] >= k; }); int nj; if (r == m) { nj = j + acc_a[i][r] - acc_a[i][l]; } else { l = r; r = binsearch(l, m, [&](int r) { return acc_b[i][r] - acc_b[i][l] >= k; }); if (r == m) { nj = k + acc_b[i][r] - acc_b[i][l]; } else { l = r; r = m; nj = 2 * k + min(k, acc_c[i][r] - acc_c[i][l]); } } chmin(dp[nj], dp[j] + cost[i]); } } REP (j, k) if (dp[k + j] != INT64_MAX) { REP (i, n) { int m = s[i].size(); int l = 0; int r = binsearch(l, m, [&](int r) { return j + acc_b[i][r] - acc_b[i][l] >= k; }); int nj; if (r == m) { nj = k + j + acc_b[i][r] - acc_b[i][l]; } else { l = r; r = m; nj = 2 * k + min(k, acc_c[i][r] - acc_c[i][l]); } chmin(dp[nj], dp[k + j] + cost[i]); } } REP (j, k) if (dp[2 * k + j] != INT64_MAX) { REP (i, n) { int m = s[i].size(); int l = 0; int r = m; int nj = 2 * k + min(k, j + acc_c[i][r] - acc_c[i][l]); chmin(dp[nj], dp[2 * k + j] + cost[i]); } } return dp.back(); } int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); constexpr char endl = '\n'; int N; int K; cin >> N; vector<string> S(N); vector<int64_t> C(N); cin >> K; REP (i, N) { cin >> S[i] >> C[i]; } auto ans = solve(N, K, S, C); cout << (ans == INT64_MAX ? -1 : ans) << endl; return 0; }