Skip to main content
  1. Posts/

light oj 1422 - Halloween Costumes (区间dp)

·2 mins
Note: This article is available in Chinese only. 本文暂无英文版本。 View original

light oj 1422 题目链接

题意:

按顺序去参加舞会。每个舞会对衣服都有要求。可以连续穿好多件衣服。需要时候就脱下来,但是一旦脱下来,这件衣服就报废了。问最少需要几件衣服。

思路:没有思路。我连这题是dp都看不出来。。知道是dp也一点思路都没。。虽然这道题是道区间dp的入门题。。。但是不怕被鄙视。。我一点也没思路。。

参考了10多篇题解。。。终于懂了一点。。

参考博客1 参考博客2 参考博客3 参考博客4

当a[i]和a[j]相等的时候    dp[i][j]=dp[i][j-1] 嗯,就这一个转移。然后就是区间dp的固定写法了。

这句话让我恍然大悟。。。难道。。都是套路?

dp[i][j]表示的是[i,j]之间最少需要的衣服数量。

初始化dp[i][i] = 1,表示每天都换一件新衣服,显然不优。

a[i]==a[j]时,dp[i][j] = dp[i][j-1] ,表示第j天不用新换衣服,

然后枚举划分区间的点k,分成[i,k]和[k+1,j]两部分,取所有情况中最好的(这大概就是区间dp的套路?)

 1/* ***********************************************
 2Author :111qqz
 3Created Time :2016年07月25日 星期一 18时49分07秒
 4File Name :code/loj/1422.cpp
 5************************************************ */
 6
 7#include <cstdio>
 8#include <cstring>
 9#include <iostream>
10#include <algorithm>
11#include <vector>
12#include <queue>
13#include <set>
14#include <map>
15#include <string>
16#include <cmath>
17#include <cstdlib>
18#include <ctime>
19#define fst first
20#define sec second
21#define lson l,m,rt<<1
22#define rson m+1,r,rt<<1|1
23#define ms(a,x) memset(a,x,sizeof(a))
24typedef long long LL;
25#define pi pair < int ,int >
26#define MP make_pair
27
28using namespace std;
29const double eps = 1E-8;
30const int dx4[4]={1,0,0,-1};
31const int dy4[4]={0,-1,1,0};
32const int inf = 0x3f3f3f3f;
33const int N=105;
34int dp[N][N];
35int a[N];
36int n;
37int main()
38{
39	#ifndef  ONLINE_JUDGE
40	freopen("code/in.txt","r",stdin);
41  #endif
42
43	int T;
44	cin>>T;
45	int cas = 0 ;
46	while (T--)
47	{
48	    ms(dp,0x3f);
49	    scanf("%d",&n);
50	    for ( int i = 1 ; i <= n ; i++) dp[i][i] = 1;
51	    for ( int i = 1 ; i <= n ; i++) scanf("%d",&a[i]);
52
53	    for ( int l =  1 ; l <= n ; l++)
54		for ( int i = 1 ; i <= n ; i++ )
55		{
56		    int j = i+l;
57		    if (j>n) continue;
58		    if (a[i]==a[j]) dp[i][j] =  dp[i][j-1];
59
60		    for ( int k = i ; k < j ; k++)
61			dp[i][j] = min(dp[i][j],dp[i][k]+dp[k+1][j]);
62
63		}
64
65	    printf("Case %d: %d\n",++cas,dp[1][n]);
66	}
67
68  #ifndef ONLINE_JUDGE
69  fclose(stdin);
70  #endif
71    return 0;
72}

Related

poj 2955 Brackets(区间dp....括号匹配。。。人生第一道区间dp)

·2 mins
poj2955题目链接 题意:给出若干括号,问最大匹配数是多少。 思路:没有思路。我知道这是dp。。。然后其他就什么都不知道了。。。转移方程? 完全没思路。。知道了转移方程。。。。嗯,还是不会。。。边界怎么写?状态怎么推?循环顺序? 循环次序?我一点思路都没有。。。。。

hdu 1114 - Piggy-Bank (完全背包)

·2 mins
F - Piggy-Bank **Time Limit:**1000MS **Memory Limit:**32768KB 64bit IO Format:%I64d & %I64u Submit Status Description Before ACM can do anything, a budget must be prepared and the necessary financial support obtained. The main income for this action comes from Irreversibly Bound Money (IBM). The idea behind is simple. Whenever some ACM member has any small money, he takes all the coins and throws them into a piggy-bank. You know that this process is irreversible, the coins cannot be removed without breaking the pig. After a sufficiently long time, there should be enough cash in the piggy-bank to pay everything that needs to be paid.

hdu 1087 - Super Jumping! Jumping! Jumping! (最长上升子序列)

·2 mins
E - Super Jumping! Jumping! Jumping! **Time Limit:**1000MS **Memory Limit:**32768KB 64bit IO Format:%I64d & %I64u Submit Status Description Nowadays, a kind of chess game called “Super Jumping! Jumping! Jumping!” is very popular in HDU. Maybe you are a good boy, and know little about this game, so I introduce it to you now.