dp专辑

dp完全背包基础
1、题目链接:https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=114&page=show_problem&problem=615
我的代码:
在这里插入图片描述
核心就是dp[j]+=dp[j-a[i]];
2、题目链接:https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=114&page=show_problem&problem=83;
我的代码:
在这里插入图片描述
核心就是dp[j]+=dp[j-a[i]];
3、
Problem Description
Battle Ships is a new game which is similar to Star Craft. In this game, the enemy builds a defense tower, which has L longevity. The player has a military factory, which can produce N kinds of battle ships. The factory takes ti seconds to produce the i-th battle ship and this battle ship can make the tower loss li longevity every second when it has been produced. If the longevity of the tower lower than or equal to 0, the player wins. Notice that at each time, the factory can choose only one kind of battle ships to produce or do nothing. And producing more than one battle ships of the same kind is acceptable.

Your job is to find out the minimum time the player should spend to win the game.

Input
There are multiple test cases.
The first line of each case contains two integers N(1 ≤ N ≤ 30) and L(1 ≤ L ≤ 330), N is the number of the kinds of Battle Ships, L is the longevity of the Defense Tower. Then the following N lines, each line contains two integers t i(1 ≤ t i ≤ 20) and li(1 ≤ li ≤ 330) indicating the produce time and the lethality of the i-th kind Battle Ships.

Output
Output one line for each test case. An integer indicating the minimum time the player should spend to win the game.

Sample Input
1 1
1 1
2 10
1 1
2 5
3 100
1 10
3 20
10 100

Sample Output
2
4
5
————————————————
版权声明:本文为CSDN博主「Alex_McAvoy」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/u011815404/article/details/88598573

我的代码:

#include<iostream>
#include<string.h>
#include<cstdio> 
using namespace std;
int dp[10001];
int main()
{
	int n,l;
	while((scanf("%d %d",&n,&l))!=EOF&&(n+l)){
		memset(dp,0,sizeof(dp));
		int t[200]={0},li[200]={0};
		for(int i=0;i<n;i++){
			cin>>t[i]>>li[i];
		}
		for(int i=0;i<n;i++){
			for(int j=t[i];j<=t[i]+l;j++){
				dp[j]=max(dp[j],dp[j-t[i]]+li[i]*(j-t[i]));
			}
		}
		int minn=0;
		for(int i=1;i<=1000;i++){
			if(dp[i]>=l){
				minn=i;
				break;
			}
		}
		cout<<minn<<endl;
	}
	return 0;
 } 

核心代码:max(dp[j],dp[j-t[i]]+li[i]*(j-t[i]));
多种背包
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1059
代码:

#include<iostream>
#include<string.h>
using namespace std;
int dp[100001];
int v[6],k[10001];
int main()
{
	int sum=0,n=0;
	for(int i=0;i<6;i++){
		cin>>k[i];
		sum+=k[i]*(i+1);
	}
	while(k[0]!=0||0!=k[1]||k[2]!=0 ||k[3] != 0 ||k[4]!=0 ||k[5]!=0){
		if(sum%2==0){
			n=0; 
			for(int i=0;i<6;i++){
				int r=1;
				while(k[i]>r){   //此处采用二分制,进行优化
					k[i]-=r;
					v[++n]= r*(i+1);
					r<<=1;
				}
				if(k[i]>0){
					v[++n]=k[i]*(i+1);
				} 
			}
			memset(dp,0,sizeof(dp));
			for(int i=1;i<n;i++){
				for(int j=sum/2;j>=v[i];j--){
					dp[j]=max(dp[j],dp[j-v[i]]+v[i]);
				}
			}
			cout<<dp[sum/2]<<endl; 
			if(dp[sum/2] == sum/2){
				cout<<"Can be divided.\n\n";
				}
        	else{ 
			 	cout<<"Can't be divided.\n\n";
			}		
		}
		else{
			cout<<"Can't be divided.\n\n";
		}
		sum=0;
		for(int i=0;i<6;i++){
			cin>>k[i];
			sum+=k[i]*(i+1);
		}
	}
	return 0;
};

题解:因为这是多种背包的题,所以,k[i]表是数量,v[i]来表示价值也表示重量,总体积就是sum。
核心代码:dp[sum/2]=sum/2;这里的sum为总价值,如果通过dp可以在一半的时候,dp[sum/2]的最大值可以等于sum的一半,可证明一个人可以分的最多为总和一半,那么就符合题意;
优化:这里我们采用的是二进制优化:列如:我们可以将13转化为(2^3 + 2 ^1+2 ^ 0);核心代码看上面;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值