結果
問題 | No.1029 JJOOII 3 |
ユーザー | carrot46 |
提出日時 | 2020-04-17 22:33:06 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 225 ms / 2,000 ms |
コード長 | 2,222 bytes |
コンパイル時間 | 1,604 ms |
コンパイル使用メモリ | 178,124 KB |
実行使用メモリ | 258,176 KB |
最終ジャッジ日時 | 2024-10-03 14:09:47 |
合計ジャッジ時間 | 6,232 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 38 |
ソースコード
#include <bits/stdc++.h> #include <iomanip> using namespace std; #define reps(i,s,n) for(int i = s; i < n; i++) #define rep(i,n) reps(i,0,n) #define Rreps(i,n,e) for(int i = n - 1; i >= e; --i) #define Rrep(i,n) Rreps(i,n,0) #define ALL(a) a.begin(), a.end() #define fi first #define se second typedef long long ll; typedef vector<ll> vec; typedef vector<vec> mat; ll N,M,H,W,K,Q,A,B; string S; const ll MOD = 998244353; //const ll MOD = (1e+9) + 7; const ll INF = 1LL<<60; typedef pair<ll, ll> P; template<class T> bool chmin(T &a, const T &b){ if(a > b) {a = b; return true;} else return false; } int main() { cin>>N>>K; vector<string> s(N); vec cost(N); rep(i,N) cin>>s[i]>>cost[i]; mat num(N, vec(3, 0)); rep(i,N){ for(char c : s[i]){ ++num[i][c == 'J' ? 0 : (c == 'O' ? 1 : 2)]; } } vector<mat> dp(3, mat(N + 1, vec(K + 81, INF))); dp[0][0][0] = 0; string JOI = "JOI"; rep(c, 3){ rep(i,c){ reps(j, max(0LL, K - 80), K + 81) { ll temp = dp[i][N][j]; rep(k, N) { ll rest = (c - i == 2 ? K : max(0LL, K - j)); ll r2 = (c - i == 2 ? max(0LL, K - j) : 0LL); for (char si : s[k]) { if(r2 > 0){ r2 -= si == JOI[0]; }else if (rest > 0) { rest -= si == JOI[c - 1]; } else { rest -= si == JOI[c]; } } if (rest <= 0) chmin(dp[c][0][min(K + 80, -rest)], temp + cost[k]); } } } /*rep(j,K + 1){ cout<<dp[c][0][j]<<' '; } cout<<endl;*/ rep(i,N){ rep(j, K + 81){ chmin(dp[c][i+1][j], dp[c][i][j]); if(j >= num[i][c]) chmin(dp[c][i+1][j], dp[c][i+1][j - num[i][c]] + cost[i]); //if(j <= K) cout<<dp[c][i+1][j]<<' '; } //cout<<endl; } } ll ans = INF; reps(j,K,K + 81) chmin(ans, dp[2][N][j]); cout<<(ans == INF ? -1 : ans)<<endl; }