Skip to main content
  1. Posts/

hdu 2018 母牛的故事 (基础dp,记忆化搜索)

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

hdu2018题目链接

题意:第1年有1头奶牛,每年生一头奶牛,新生的奶牛从生下来的第四年(包括生下来那年)也开始每年一头奶牛。 问第n年有多少头奶牛。 思路:最容易想到的。。递推一下。。。dp[i] = dp[i-1] + dp[i-3] (注意初始化不是一个dp[1]=1,而是dp[1..4]=1..4)

递推代码:

 1/* ***********************************************
 2Author :111qqz
 3Created Time :2016年07月27日 星期三 13时19分17秒
 4File Name :code/hdu/2018.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
20a#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=60;
34int n;
35LL dp[N];
36int main()
37{
38	#ifndef  ONLINE_JUDGE
39	freopen("code/in.txt","r",stdin);
40  #endif
41
42	dp[1] = 1;
43	dp[2] = 2;
44	dp[3] = 3;
45	dp[4] = 4;
46	for ( int i = 5 ; i < N ; i++)
47	{
48	    dp[i] = dp[i-1] + dp[i-3];
49	}
50
51
52//	for ( int i = 0 ; i < N;  i++)
53//	    cout<<"i:"<<i<<" dp[i]:"<<dp[i]<<endl;
54
55
56	while (scanf("%d",&n)!=EOF)
57	{
58	    if (n==0) break;
59	    printf("%lld\n",dp[n]);
60	}
61
62
63
64  #ifndef ONLINE_JUDGE
65  fclose(stdin);
66  #endif
67    return 0;
68}

也可以考虑记忆化嘛

理论上应该是所有的dp都可以写成记忆化。。。?

记忆化的好处是不用考虑什么边界的问题。。。坏处是。。可能爆栈。。。。。

记忆化代码:

 1/* ***********************************************
 2Author :111qqz
 3Created Time :2016年07月27日 星期三 13时19分17秒
 4File Name :code/hdu/2018.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=60;
34int n;
35LL dp[N];
36LL cal( int n)
37{
38    if (dp[n]!=-1) return dp[n];
39    return dp[n] = cal(n-1) + cal(n-3);
40}
41int main()
42{
43	#ifndef  ONLINE_JUDGE
44	freopen("code/in.txt","r",stdin);
45  #endif
46
47	ms(dp,-1);
48	dp[1] = 1;
49	dp[2] = 2;
50	dp[3] = 3;
51	dp[4] = 4;
52	while (scanf("%d",&n)!=EOF)
53	{
54	    if (n==0) break;
55	    printf("%lld\n",cal(n));
56	}
57
58
59
60  #ifndef ONLINE_JUDGE
61  fclose(stdin);
62  #endif
63    return 0;
64}

Related

hdu 2084 数塔 (基础dp)

·1 min
hdu2084题目链接 题意:dp入门题。。。数字三角形。。 思路: 昨天看mit公开课。。。讲到dp的精髓是sub-problem+ reuse…

hdu 4283 You Are the One (区间dp)

·2 mins
hdu 4283题目链接 题意:有N个人按顺序排成一排上台表演,每个都有一个num[]值,若在他是第k个上场的人,则会有num[]*(k-1)的unhappiness。台下有一个黑屋(stack),对每一个人,可以选择让他先进屋子或者直接上台。现在让你找到一个最优方案使得所有人的unhappiness之和最小。

poj 3661 Running (区间dp)

·3 mins
poj 3661题目链接 题意:锻炼,一共n分钟,每分钟可以选择跑步或者休息,第i分钟跑步可以跑d[i]米,并增加一点疲劳度,如果选择休息,那么每分钟减少1点疲劳值。一旦开始休息,必须休息到疲劳值为0才能再次开始跑步。疲劳值不能超过m.第n分钟的时候疲劳值必须为0,否则之后会感觉身体被掏空。问n分钟最远多多远。