↓ Skip to main content
  1. Posts/

light oj 1422 - Halloween Costumes (区间dp)

·669 words·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)

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

hdu 1160 FatMouse's Speed (最长上升子序列)

·769 words·2 mins
FatMouse’s Speed # Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 10172 Accepted Submission(s): 4521 Special Judge Problem Description FatMouse believes that the fatter a mouse is, the faster it runs. To disprove this, you want to take the data on a collection of mice and put as large a subset of this data as possible into a sequence so that the weights are increasing, but the speeds are decreasing.

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

·774 words·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! (最长上升子序列)

·536 words·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. The game can be played by two or more than two players. It consists of a chessboard(棋盘)and some chessmen(棋子), and all chessmen are marked by a positive integer or “start” or “end”. The player starts from start-point and must jumps into end-point finally. In the course of jumping, the player will visit the chessmen in the path, but everyone must jumps from one chessman to another absolutely bigger (you can assume start-point is a minimum and end-point is a maximum.). And all players cannot go backwards. One jumping can go from a chessman to next, also can go across many chessmen, and even you can straightly get to end-point from start-point. Of course you get zero point in this situation. A player is a winner if and only if he can get a bigger score according to his jumping solution. Note that your score comes from the sum of value on the chessmen in you jumping path. Your task is to output the maximum value according to the given chessmen list.

poj 1141 Brackets Sequence (区间dp,括号匹配,记录路径)

·835 words·2 mins
poj 1141题目链接 题意:给出一个括号序列,要求添加最少的括号,使得这个序列变成合法的括号匹配,输出最后的序列。 思路:区间dp。。。有了那么一点思路。。。我们可以用dp[i][j]表示区间[i,j]的序列最少需要添加几个符号使得匹配。。转移的话。。。和之前差不多。。dp[i][j] = dp[i+1][j-1] (s[i]与s[j])匹配。。。不匹配的话也是找中间某个点。。。初始化的话。。要变成最大值。。。比较没思路的是输出括号序列这部分。。。