結果
問題 | No.1029 JJOOII 3 |
ユーザー |
|
提出日時 | 2020-04-17 22:16:19 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 55 ms / 2,000 ms |
コード長 | 2,141 bytes |
コンパイル時間 | 2,915 ms |
コンパイル使用メモリ | 201,440 KB |
最終ジャッジ日時 | 2025-01-09 20:11:55 |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 38 |
ソースコード
#include <bits/stdc++.h> // clang-format offusing Int = long long;#define REP_(i, a_, b_, a, b, ...) for (Int i = (a), lim##i = (b); i < lim##i; i++)#define REP(i, ...) REP_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__)struct SetupIO { SetupIO() { std::cin.tie(nullptr), std::ios::sync_with_stdio(false), std::cout << std::fixed << std::setprecision(13); } } setup_io;#ifndef _MY_DEBUG#define dump(...)#endif // clang-format on/*** author: knshnb* created: Fri Apr 17 21:50:59 JST 2020**/template <class T, class S> T make_vec(S x) { return x; }template <class T, class... Ts> auto make_vec(size_t n, Ts... ts) {return std::vector<decltype(make_vec<T>(ts...))>(n, make_vec<T>(ts...));}template <class T> inline bool chmin(T& a, const T& b) {if (a <= b) return false;a = b;return true;}template <class T> inline bool chmax(T& a, const T& b) {if (a >= b) return false;a = b;return true;}const Int INF = 2e18;signed main() {Int n, K;std::cin >> n >> K;std::string T(3 * K + 100, '$');REP(i, K) T[i] = 'J';REP(i, K, 2 * K) T[i] = 'O';REP(i, 2 * K, 3 * K) T[i] = 'I';std::vector<std::pair<std::string, Int>> ps(n);REP(i, n) std::cin >> ps[i].first >> ps[i].second;auto cnt = make_vec<Int>(n, 3, 0);REP(i, n) {for (char c : ps[i].first) {cnt[i][0] += c == 'J';cnt[i][1] += c == 'O';cnt[i][2] += c == 'I';}}std::vector<Int> dp(3 * K + 100, INF);dp[0] = 0;REP(i, 3 * K) {REP(k, n) {auto& p = ps[k];Int j = i, idx = 0;std::string& s = p.first;if (i + cnt[k][i / K] < K * (i / K + 1)) {j = i + cnt[k][i / K];} else {while (1) {idx = std::find(s.begin() + idx, s.end(), T[j]) - s.begin() + 1;if (idx > s.size()) break;j++;}}chmin(dp[j], dp[i] + p.second);}}std::cout << (dp[3 * K] == INF ? -1 : dp[3 * K]) << std::endl;}