动态规划,poj几道基础经典题型,重要!

本文介绍动态规划在不同场景的应用案例,包括三角DP求最大路径和、完全背包求方案总数、01背包求最优解、最长回文子串及最长递增子序列等问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.经典应用之一:三角DP

POJ3176

Input

Line 1: A single integer, N

Lines 2..N+1: Line i+1 contains i space-separated integers that represent row i of the triangle.

Output

Line 1: The largest sum achievable using the traversal rules

Sample Input

5
7
3 8
8 1 0
2 7 4 4
4 5 2 6 5

Sample Output

30

输入一个三角形,均为自然数,求从上到下只能向下或者向右下,求一路能得到最大的和

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
int a[355][355];
int ans[355][355];
int main(){
	int n;
	while(cin>>n){
		memset(a,0,sizeof(a));
		memset(ans,0,sizeof(ans));
		for (int i=1;i<=n;i++){
			for (int j=1;j<=i;j++){
				cin>>a[i][j];
			}
		}
		
		for (int i=1;i<=n;i++){
			for (int j=1;j<=i;j++){
				ans[i][j]=max(ans[i-1][j-1]+a[i][j],ans[i-1][j]+a[i][j]);//最简单的。。。
			}
		}
		int x=0;
		for (int i=1;i<=n;i++){
			x=max(x,ans[n][i]);
		}
		cout<<x<<endl;
	}
}

2.经典应用之二:完全背包,求方案总数(简单版)

poj2229
Sumsets
Time Limit: 2000MS Memory Limit: 200000K
Total Submissions: 15480 Accepted: 6158

Description

Farmer John commanded his cows to search for different sets of numbers that sum to a given number. The cows use only numbers that are an integer power of 2. Here are the possible sets of numbers that sum to 7:

1) 1+1+1+1+1+1+1
2) 1+1+1+1+1+2
3) 1+1+1+2+2
4) 1+1+1+4
5) 1+2+2+2
6) 1+2+4

Help FJ count all possible representations for a given integer N (1 <= N <= 1,000,000).

Input

A single line with a single integer, N.

Output

The number of ways to represent N as the indicated sum. Due to the potential huge size of this number, print only last 9 digits (in base 10 representation).

Sample Input

7

Sample Output

6

如果将基数换为某些面值不同的硬币,求某个金额的搭配数目,也是一样的解法:

#include<iostream>
#include<cstring>
using namespace std;
int ans[1000005];
int m=1000000000;
int main(){
	int n;
	while(cin>>n){
		memset(ans,0,sizeof(ans));
		ans[0]=1;
		for (int i=1;i<=n;i*=2){
			for (int j=1;j<=n;j++){//注意是由小到大x循环,因为每一种pow(2,n)为无限多个
				if (j>=i) ans[j] = (ans[j]+ans[j-i])%m;//每都一种面额就累加
			}
		}
		cout<<ans[n]<<endl;
	}
}

3.经典应用之三:01背包,求最优解:

poj3616

Milking Time
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 7067 Accepted: 2945

Description

Bessie is such a hard-working cow. In fact, she is so focused on maximizing her productivity that she decides to schedule her next N (1 ≤ N ≤ 1,000,000) hours (conveniently labeled 0..N-1) so that she produces as much milk as possible.

Farmer John has a list of M (1 ≤ M ≤ 1,000) possibly overlapping intervals in which he is available for milking. Each interval i has a starting hour (0 ≤ starting_houriN), an ending hour (starting_houri < ending_houriN), and a corresponding efficiency (1 ≤ efficiencyi ≤ 1,000,000) which indicates how many gallons of milk that he can get out of Bessie in that interval. Farmer John starts and stops milking at the beginning of the starting hour and ending hour, respectively. When being milked, Bessie must be milked through an entire interval.

Even Bessie has her limitations, though. After being milked during any interval, she must rest R (1 ≤ RN) hours before she can start milking again. Given Farmer Johns list of intervals, determine the maximum amount of milk that Bessie can produce in the N hours.

Input

* Line 1: Three space-separated integers: N, M, and R
* Lines 2..M+1: Line i+1 describes FJ's ith milking interval withthree space-separated integers: starting_houri , ending_houri , and efficiencyi

Output

* Line 1: The maximum number of gallons of milk that Bessie can product in the N hours

Sample Input

12 4 2
1 2 8
10 12 19
3 6 24
7 10 31

Sample Output

43


将每段按结束时间排序,然后ans[i]表示从开始到i能得到的最大值,ans[i] = max (ans[i],ans[j]+a[j].val) for j =0...i,且a[j].end<a[i].start;复杂度为O(n2);

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
struct node{
	int sta;
	int end;
	int val;
	node (){
		sta=end=val=0;
	}
	bool operator < (const node &b) const{
		if (end!=b.end){
			return end<b.end;
		}
		else return sta<b.sta; 
	}
};
int main(){
	int n,m,r;
	while(cin>>n>>m>>r){
		node a[m];
		int ans[1001]={0};
		for (int i=0;i<m;i++){
			cin>>a[i].sta>>a[i].end>>a[i].val;
			a[i].end+=r;
		}
		sort(a,a+m);
		for (int i=0;i<m;i++){
			ans[i]=max(ans[i],a[i].val);
			for (int j=0;j<i;j++){
				if (a[j].end<=a[i].sta){
					ans[i]=max(ans[i],ans[j]+a[i].val);
				}
			}
		}
		int t=0;
		for (int i=0;i<m;i++){
			t=max(ans[i],t);
		}
		cout<<t<<endl;
	}
} 

4.经典应用之三:最长回文子串:

给出一个字符串,判断最长回文子串:

int lpsDp(char * str,int n){
	int dp[n][n], tmp;
	memset(dp,0,sizeof(dp));
	for(int i=0; i<n; i++) dp[i][i] = 1;
	// i 表示 当前长度为 i+1的 子序列
	for(int i=1; i<n; i++){
		tmp = 0;
		//考虑所有连续的长度为i+1的子串. 该串为 str[j, j+i]
		for(int j=0; j+i<n; j++){
			//如果首尾相同
			if(str[j] == str[j+i]){
				tmp = dp[j+1][j+i-1] + 2;
			}else{
				tmp = max(dp[j+1][j+i],dp[j][j+i-1]);
			}
			dp[j][j+i] = tmp;
		}
	}
	//返回串 str[0][n-1] 的结果
	return dp[0][n-1];
}


5.最长递增子序列,前面已经写过优化的nlogn的算法,所以不再叙述


未完待续。。。。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值