题目链接

思路
d p [ i ] [ j ] i p a r t y j p a r t y 穿 dp[i][j]表示从第i场party到第j场party所需穿的最少服装 dp[i][j]ipartyjparty穿
d p 2 3 4 按照区间dp一般做法,我们通常枚举长度为2、3、4的区间找找规律 dp234
d p [ i ] [ j ] d p [ i ] [ j + 1 ] 不难猜想,如果我们知道dp[i][j],那我们如何求dp[i][j +1]的值呢? dp[i][j]dp[i][j+1]
d p [ i ] [ j + 1 ] d p [ i ] [ j ] 1 p a r t y 显然可以知道,dp[i][j+1]只是比dp[i][j]枚举的区间多了1个party dp[i][j+1]dp[i][j]1party
d p [ i ] [ j + 1 ] 1 d p [ i ] [ j + 1 ] 那对答案dp[i][j+1]的贡献最多是1,所以dp[i][j+1]的答案怎么确定呢? dp[i][j+1]1dp[i][j+1]
k 枚举区间分割点,设为k k
a [ k ] = = a [ j + 1 ] [ i , k ] k p a r t y 穿 [ k + 1 , j + 1 ] j + 1 p a r t y 如果a[k] == a[j+1],则表示[i,k]举行第k个party的时候穿的衣服与[k+1,j+1]举行第j+1个party的相同 a[k]==a[j+1][i,k]kparty穿[k+1,j+1]j+1party
d p [ i ] [ j + 1 ] = m i n ( d p [ i ] [ j + 1 ] , d p [ i ] [ k ] + d p [ k + 1 ] [ j + 1 ] 1 ) 那我们就可以确定dp[i][j + 1]=min(dp[i][j+1], dp[i][k] + dp[k+1][j+1]-1) dp[i][j+1]=min(dp[i][j+1],dp[i][k]+dp[k+1][j+1]1)
d p [ i ] [ j + 1 ] = m i n ( d p [ i ] [ j + 1 ] , d p [ i ] [ k ] + d p [ k + 1 ] [ j + 1 ] ) 否则,dp[i][j + 1]=min(dp[i][j+1], dp[i][k] + dp[k+1][j+1]) dp[i][j+1]=min(dp[i][j+1],dp[i][k]+dp[k+1][j+1])

AC代码

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
ll dp[105][105];
ll a[105];
int main(){
    int t;
    scanf("%d", &t);
    int num = 0;
    while(t--){
	num++;
	int n;
	scanf("%d", &n);
	for(int i = 1; i <= n; i++) scanf("%lld", &a[i]);
	for(int i = 1; i <= n; i++){
		for(int j = i; j <= n; j++){
			if(j == i) dp[i][j] = 1;
			else dp[i][j] = 10000;
		}
	}
	for(int len = 2; len <= n; len++){
		for(int i = 1; i <= n; i++){
			int j = len + i - 1;
			if(j > n) break;
			for(int k = i; k < j; k++){
				if(a[j] == a[k]) dp[i][j] = min(dp[i][j], dp[i][k] + dp[k + 1][j] - 1);
				else dp[i][j] = min(dp[i][j], dp[i][k] + dp[k + 1][j]);
			}
		}
	}
	printf("Case %d: %lld\n", num, dp[1][n]);
    }
    return 0;
}

思考
d p 有时候这些dp转移方程式很难想到怎么证明,但是如果我们自己类推找规律的话。 dp
直接莽一发,说不定就对了