結果
問題 | No.1029 JJOOII 3 |
ユーザー |
![]() |
提出日時 | 2020-07-09 21:00:34 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 3,034 bytes |
コンパイル時間 | 2,185 ms |
コンパイル使用メモリ | 199,668 KB |
最終ジャッジ日時 | 2025-01-11 17:14:14 |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 35 WA * 3 |
ソースコード
#include<bits/stdc++.h> using namespace std; #define ALL(x) x.begin(),x.end() #define rep(i,n) for(int i=0;i<(n);i++) #define debug(v) cout<<#v<<":";for(auto x:v){cout<<x<<' ';}cout<<endl; #define mod 1000000007 using ll=long long; const int INF=1000000000; const ll LINF=1001002003004005006ll; int dx[]={1,0,-1,0}; int dy[]={0,1,0,-1}; // ll gcd(ll a,ll b){return b?gcd(b,a%b):a;} template<class T>bool chmax(T &a,const T &b){if(a<b){a=b;return true;}return false;} template<class T>bool chmin(T &a,const T &b){if(b<a){a=b;return true;}return false;} struct IOSetup{ IOSetup(){ cin.tie(0); ios::sync_with_stdio(0); cout<<fixed<<setprecision(12); } } iosetup; template<typename T1,typename T2> ostream &operator<<(ostream &os,const pair<T1,T2>&p){ os<<p.first<<" "<<p.second; return os; } template<typename T> ostream &operator<<(ostream &os,const vector<T>&v){ for(int i=0;i<(int)v.size();i++) os<<v[i]<<(i+1==v.size()?"":" "); return os; } int n,k; ll cost[100]; string s[100]; vector<ll> knapsack(char target){ int cnt[n]={}; rep(i,n)rep(j,s[i].size())cnt[i]+=(int)(s[i][j]==target); vector<ll> dp(k+1,LINF); dp[0]=0; rep(i,n){ for(int j=0;j<=k;j++){ if(j-cnt[i]>=0){ chmin(dp[j],dp[j-cnt[i]]+cost[i]); } } } ll m=LINF; for(int i=k;i>=0;i--){ chmin(m,dp[i]); dp[i]=m; } return dp; } signed main(){ cin>>n>>k; rep(i,n) cin>>s[i]>>cost[i]; auto dpj=knapsack('J'); auto dpo=knapsack('O'); auto dpi=knapsack('I'); if(dpj[k]==LINF or dpo[k]==LINF or dpi[k]==LINF){ cout<<-1<<endl; return 0; } int cj[n][100]={},co[n][100]={},ci[n][100]={}; rep(i,n){ rep(j,s[i].size()){ cj[i][j+1]=cj[i][j]; co[i][j+1]=co[i][j]; ci[i][j+1]=ci[i][j]; if(s[i][j]=='J') cj[i][j+1]++; if(s[i][j]=='O') co[i][j+1]++; if(s[i][j]=='I') ci[i][j+1]++; } } ll ans=dpj[k]+dpo[k]+dpi[k]; // JO, OI rep(i,n){ rep(j,n){ rep(di,s[i].size()+1){ rep(dj,s[j].size()+1){ int jaru=cj[i][di],oaru=co[i][s[i].size()]-co[i][di]+co[j][dj],iaru=ci[j][s[j].size()]-ci[j][dj]; int needj=max(0,k-jaru),needo=max(0,k-oaru),needi=max(0,k-iaru); chmin(ans,cost[i]+cost[j]+dpi[needi]+dpj[needj]+dpo[needo]); } } } } // JJJO...OIII... if(k<=80){ rep(i,n){ rep(l,s[i].size()){ for(int r=l+1;r<=s[i].size();r++){ if(co[i][r]-co[i][l]>=k){ int jaru=cj[i][l],iaru=ci[i][s[i].size()]-ci[i][r]; int needj=max(0,k-jaru),needi=max(0,k-iaru); chmin(ans,cost[i]+dpj[needj]+dpi[needi]); } } } } } cout<<ans<<endl; return 0; }