UVA 4857 Halloween Costumes 區間背包
阿新 • • 發佈:2017-07-13
const dsm style cst memset post https 模板 urn
題目鏈接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2858
題意:給你n天須要穿的衣服的樣式,每次能夠套著穿衣服,脫掉的衣服就不能再用了(能夠再穿),問至少要帶多少條衣服才幹參加全部宴會
分組背包模板題:
dp[i][j];若第j件穿,dp[i][j]=d[i][j-1]+1;
若不穿。則能夠想到。i到j-1區間內必須得有這件衣服,dp[i][j]=dp[i][k-1]+dp[k][j-1],i<=k<=j-1;a[j]==a[k];
代碼:
#include<iostream> #include<cstring> #include<cmath> using namespace std; const int maxn=110; int a[maxn]; int dp[maxn][maxn]; int main() { int t,i,j,k; while(cin>>t) { int p=1; while(t--) { int n,m; cin>>n>>m; int i,j; for(i=1;i<=n;i++) { cin>>a[i]; } memset(dp,0,sizeof(dp)); for(i=1;i<=n;i++) dp[i][i]=1; //cout<<"haha"<<endl; for(i=n-1;i>=1;i--) { for(j=i+1;j<=n;j++) { dp[i][j]=dp[i][j-1]+1; for(k=i;k<=j-1;k++) { if(a[j]==a[k]) dp[i][j]=min(dp[i][j],dp[i][k-1]+dp[k][j-1]); } } } cout<<"Case "<<p++<<": "<<dp[1][n]<<endl; } } return 0; }
UVA 4857 Halloween Costumes 區間背包