結果
問題 | No.1029 JJOOII 3 |
ユーザー |
![]() |
提出日時 | 2020-04-20 02:00:52 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 45 ms / 2,000 ms |
コード長 | 1,602 bytes |
コンパイル時間 | 2,804 ms |
コンパイル使用メモリ | 198,996 KB |
最終ジャッジ日時 | 2025-01-09 21:50:21 |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 38 |
ソースコード
#include <bits/stdc++.h> using namespace std; using lint = long long int; struct fast_ios { fast_ios(){ cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(20); }; } fast_ios_; #define FOR(i, begin, end) for(int i=(begin),i##_end_=(end);i<i##_end_;i++) #define REP(i, n) FOR(i,0,n) template<typename T> bool chmin(T &m, const T q) { if (m > q) {m = q; return true;} else return false; } int main() { int N, K; cin >> N >> K; vector<string> S(N); vector<lint> C(N); REP(i, N) cin >> S[i] >> C[i]; vector<int> cntj(N), cnto(N), cnti(N); REP(i, N) for (auto c : S[i]) { if (c == 'J') cntj[i]++; if (c == 'O') cnto[i]++; if (c == 'I') cnti[i]++; } vector<lint> cost(K * 3 + 1, 9e18); cost[0] = 0; REP(i, K * 3) if (cost[i] < cost[i + 1]) { REP(j, N) { if (i + 100 < K) chmin(cost[i + cntj[j]], cost[i] + C[j]); else if (i >= K and i + 100 < K * 2) chmin(cost[i + cnto[j]], cost[i] + C[j]); else if (i >= K * 2 and i + 100 < K * 3) chmin(cost[i + cnti[j]], cost[i] + C[j]); else { int now = i; for (auto c : S[j]) { if (now < K and c == 'J') now++; if (now >= K and now < K * 2 and c == 'O') now++; if (now >= K * 2 and now < K * 3 and c == 'I') now++; } cost[now] = min(cost[now], cost[i] + C[j]); } } } cout << (cost.back() == 9e18 ? -1 : cost.back()) << '\n'; }