区间DP LightOJ 1422 Halloween Costumes

时间:2021-07-23 21:43:56

http://lightoj.com/volume_showproblem.php?problem=1422

做的第一道区间DP的题目,试水。

参考解题报告:

http://www.cnblogs.com/ziyi--caolu/p/3236035.html

http://blog.csdn.net/hcbbt/article/details/15478095

dp[i][j]为第i天到第j天要穿的最少衣服,考虑第i天,如果后面的[i+1, j]天的衣服不要管,那么dp[i][j] = dp[i + 1][j] + 1。

然后在区间[i +1, j]里面找到和第i天衣服一样的日子,尝试直到那天都不把i脱掉,

那么就变成dp[i][j] = dp[i + 1][k - 1] + dp[k][j],你可能会发现第i天不见了,其实只要把+两边换一下就好说了,就是第k天的衣服一直穿着

贴我的代码:

 #include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <cmath>
#include <cstdlib> #define rep(i,a,n) for(int i = a;i <= n;i++)
#define per(i,n,a) for(int i = n;i>=a;i--)
#define pb push_back
#define VI vector<int>
#define QI queue<int>
#define logM(N) log10(N)/log10(M)
#define eps 1e-8 typedef long long ll; using namespace std; int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
#endif
int n,_;
scanf("%d",&n);
_ = ;
int a[ + ] = {};
int dp[ + ][ + ] = {};
int t;
while(n--){
scanf("%d",&t);
rep(i,,t+){
rep(j,,t+){
dp[i][j] = ;
}
}
rep(i,,t){
scanf("%d",&a[i]);
}
rep(i,,t){
rep(j,i,t){
dp[i][j] = ;
}
}
per(i,t,){
rep(j,i,t){
dp[i][j] = dp[i+][j] +;
rep(k,i+,j){
if(a[i] == a[k]){
dp[i][j] = min(dp[i][j],dp[i+][k-]+dp[k][j]);
}
}
}
}
printf("Case %d: %d\n",++_,dp[][t]);
}
return ;
}