Halloween Costumes LightOJ - 1422 (the interval dp)

topic

Small gray take part in some of the Christmas party, and the corresponding need to wear party clothes, so he needs to change clothes several times, for convenience, he can choose to take off some clothes or put on new clothes if he first wore clothes A, and B wear clothes, wear a C clothes, if he wants the outermost clothes is a, he may choose to wear a straight a, C or put off, and then take off B).

Input
of the first input line of a T, represents the number of test cases T <= 200 N and a [i] <= 100
Next, an input line N, the number of the party,
the next row number n, denotes the i th party he would wear a [i] clothes to attend this party (the party before and after the order can not be replaced)

Output
For each test case, output "Case i:" plus the required minimum number of clothing.
Input Case
2
. 4
. 1 2. 1 2
. 7
. 1 2. 1. 1. 1. 3 2

Output Case
Case. 1:. 3
Case 2:. 4

Explanation

dp [I] [j] i ~ j representative of the interval a minimum number of dress,
for wear j-selectable new clothes and do not wear (undressed to expose the j same clothes clothes), wear is dp [i] [j] = dp [i] [j-1] +1;
not wear dp [i] [j] = min (dp [i] [k] + dp [k + 1] [j-1] ); for the conditions k i <= k <= j- 1 && a [k] == a [j]
minimum number of clothes i to k plus a minimum quantity of clothing garment behind k add up to not wear minimum number of clothes.

#include <cstdio>
#include <algorithm>
int const maxn = 102;
using namespace std;
int dp[maxn][maxn];
int a[maxn];
int main(){
	int t;
	int n, i, j, k;
	int cont = 1;
	scanf("%d", &t);
	while(t--){
		scanf("%d", &n);
		for( i = 1; i <= n; i++){
			scanf("%d", a+i);
		}
		for(i = n; i >= 1; i--){
			dp[i][i] = 1;
			for(j = i+1; j <= n; j++){
				dp[i][j] = dp[i][j-1]+1;
				for(k = i; k < j; k++){
					if(a[k] == a[j]){
						dp[i][j] = min(dp[i][j], dp[i][k] + dp[k+1][j-1]);
					}
				}
			}
		}
		printf("Case %d: %d\n", cont++,dp[1][n]);
	}
	return 0;
}
发布了52 篇原创文章 · 获赞 2 · 访问量 873

Guess you like

Origin blog.csdn.net/qq_44714572/article/details/103037895