結果
問題 | No.1029 JJOOII 3 |
ユーザー | chocopuu |
提出日時 | 2020-04-17 22:43:30 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 37 ms / 2,000 ms |
コード長 | 2,488 bytes |
コンパイル時間 | 2,441 ms |
コンパイル使用メモリ | 203,328 KB |
最終ジャッジ日時 | 2025-01-09 20:28:06 |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 38 |
ソースコード
#include "bits/stdc++.h" using namespace std; #define int long long #define REP(i, n) for (int i = 0; i < (int)n; ++i) #define RREP(i, n) for (int i = (int)n - 1; i >= 0; --i) #define FOR(i, s, n) for (int i = s; i < (int)n; ++i) #define RFOR(i, s, n) for (int i = (int)n - 1; i >= s; --i) #define ALL(a) a.begin(), a.end() #define IN(a, x, b) (a <= x && x < b) template<class T>inline void out(T t){cout << t << "\n";} template<class T,class... Ts>inline void out(T t,Ts... ts){cout << t << " ";out(ts...);} template<class T>inline bool CHMIN(T&a,T b){if(a > b){a = b;return true;}return false;} template<class T>inline bool CHMAX(T&a,T b){if(a < b){a = b;return true;}return false;} constexpr int INF = 1e18; signed main(){ int N,K; cin >> N >> K; vector<tuple<string,int>>v(N); int ans = INF; REP(i,N){ string s; int c; cin >> s >> c; v[i] = {s,c}; int J = 0,O = 0,I = 0; REP(j,s.size()){ if(s[j] == 'J')++J; if(J >= K && s[j] == 'O')++O; if(O >= K && s[j] == 'I')++I; } if(I >= K)CHMIN(ans,c); } vector<int>dp1(K + 100,INF); dp1[0] = 0; REP(i,N){ auto[s,c] = v[i]; int cnt = 0; REP(j,s.size()){ if(s[j] == 'J')++cnt; } REP(j,K){ CHMIN(dp1[j + cnt],dp1[j] + c); } } RREP(i,K + 99)CHMIN(dp1[i],dp1[i + 1]); vector<int>dp2(K + 100,INF); vector<int>dp3(K + 100,INF); dp2[0] = dp1[K]; REP(i,N){ int jj = 0,o = 0; auto[s,c] = v[i]; REP(j,s.size()){ if(s[j] == 'O')++o; } REP(j,s.size()){ if(s[j] == 'J')++jj; else if(s[j] == 'O')--o; if(K - jj >= 0){ int I = 0; if(o >= K){ int no = 0; FOR(k,j + 1,s.size()){ if(s[k] == 'O')++no; if(no >= K && s[k] == 'I')++I; } CHMIN(dp3[I],dp1[K - jj] + c); } CHMIN(dp2[o],dp1[K - jj] + c); } } } REP(i,N){ auto[s,c] = v[i]; int cnt = 0; REP(j,s.size()){ if(s[j] == 'O')++cnt; } REP(j,K){ CHMIN(dp2[j + cnt],dp2[j] + c); } } RREP(i,K + 99)CHMIN(dp2[i],dp2[i + 1]); dp3[0] = dp2[K]; REP(i,N){ int o = 0,ii = 0; auto[s,c] = v[i]; REP(j,s.size()){ if(s[j] == 'I')++ii; } REP(j,s.size()){ if(s[j] == 'O')++o; else if(s[j] == 'I')--ii; if(K - o >= 0)CHMIN(dp3[ii],dp2[K - o] + c); } } REP(i,N){ auto[s,c] = v[i]; int cnt = 0; REP(j,s.size()){ if(s[j] == 'I')++cnt; } REP(j,K){ CHMIN(dp3[j + cnt],dp3[j] + c); } } RREP(i,K + 99)CHMIN(dp3[i],dp3[i + 1]); if(dp3[K] == INF)out(-1); else out(min(ans,dp3[K])); //REP(i,K + 1)out(i,dp1[i],dp2[i],dp3[i]); }