HDU 2602(传送门)
Bone Collector
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Problem Description
Many years ago , in Teddy’s hometown there was a man who was called “Bone Collector”. This man like to collect varies of bones , such as dog’s , cow’s , also he went to the grave …
The bone collector had a big bag with a volume of V ,and along his trip of collecting there are a lot of bones , obviously , different bone has different value and different volume, now given the each bone’s value along his trip , can you calculate out the maximum of the total value the bone collector can get ?
Input
The first line contain a integer T , the number of cases.
Followed by T cases , each case three lines , the first line contain two integer N , V, (N <= 1000 , V <= 1000 )representing the number of bones and the volume of his bag. And the second line contain N integers representing the value of each bone. The third line contain N integers representing the volume of each bone.
Output
One integer per line representing the maximum of the total value (this number will be less than
).
Sample Input
1
5 10
1 2 3 4 5
5 4 3 2 1
Sample Output
14
ps: 01背包裸题
//使用二维数组
#include<iostream>
#include<algorithm>
using namespace std;
int dp[1004][1004];
int main()
{
ios::sync_with_stdio(false);
int T;
cin>>T;
while(T--)
{
int num, V;
cin>>num>>V;
int value[1004] = {0};
int w[1004] = {0};
for(int i = 1; i <= num; i++) cin>>value[i];
for(int i = 1; i <= num; i++) cin>>w[i];
for(int i = 0; i <= V; i++) dp[0][i] = 0;
for(int i = 1; i <= num; i++)
for(int j = 0; j <= V; j++)
{
if(j - w[i] >= 0)//如果当前背包装不下,就不放
dp[i][j] = max(dp[i-1][j], dp[i-1][j - w[i]] + value[i]);
else dp[i][j] = dp[i-1][j];
}
cout<<dp[num][V]<<endl;
}
return 0;
}
//使用一维数组
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
int T;
cin>>T;
while(T--)
{
int num, V;
cin>>num>>V;
int value[1004] = {0};
int w[1004] = {0};
int dp[1004] = {0};
for(int i = 1; i <= num; i++) cin>>value[i];
for(int i = 1; i <= num; i++) cin>>w[i];
for(int i = 1; i <= num; i++)
for(int j = V; j >= w[i]; j--)
{//注意j是从V开始的
dp[j] = max(dp[j], dp[j-w[i]] + value[i]);
}
cout<<dp[V]<<endl;
}
return 0;
}
HDU 1248(传送门)
寒冰王座
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Problem Description
不死族的巫妖王发工资拉,死亡骑士拿到一张N元的钞票(记住,只有一张钞票),为了防止自己在战斗中频繁的死掉,他决定给自己买一些道具,于是他来到了地精商店前.
死亡骑士:”我要买道具!”
地精商人:”我们这里有三种道具,血瓶150块一个,魔法药200块一个,无敌药水350块一个.”
死亡骑士:”好的,给我一个血瓶.”
说完他掏出那张N元的大钞递给地精商人.
地精商人:”我忘了提醒你了,我们这里没有找客人钱的习惯的,多的钱我们都当小费收了的,嘿嘿.”
死亡骑士:”……”
死亡骑士想,与其把钱当小费送个他还不如自己多买一点道具,反正以后都要买的,早点买了放在家里也好,但是要尽量少让他赚小费.
现在死亡骑士希望你能帮他计算一下,最少他要给地精商人多少小费.
Input
输入数据的第一行是一个整数T(1<=T<=100),代表测试数据的数量.然后是T行测试数据,每个测试数据只包含一个正整数N(1<=N<=10000),N代表死亡骑士手中钞票的面值.
注意:地精商店只有题中描述的三种道具.
Output
对于每组测试数据,请你输出死亡骑士最少要浪费多少钱给地精商人作为小费.
Sample Input
2
900
250
Sample Output
0
50
ps:完全背包简单应用
//dp[i][j] 表示装前i个物品,容量为j时取得的最大价值,使用二维数组
#include<iostream>
#include<algorithm>
using namespace std;
int dp[4][10004];
int main()
{
ios::sync_with_stdio(false);
const int value[4] = {0,150,200,350};
const int w[4] = {0,150,200,350};
int T;
cin>>T;
while(T--)
{
int n;
cin>>n;
for(int i = 0; i <= n; i++)dp[0][i] = 0;
for(int i = 1; i <= 3; i++)
for(int j = 0; j <= n; j++)
{
if(j - w[i] >= 0)//如果当前背包装不下,就不放
dp[i][j] = max(dp[i-1][j], dp[i][j - w[i]] + value[i]);
else dp[i][j] = dp[i-1][j];
}
cout<<n - dp[3][n]<<endl;
}
return 0;
}
#include<iostream>
#include<algorithm>
using namespace std;
int dp[10004];
int main()
{
ios::sync_with_stdio(false);
const int value[4] = {0,150,200,350};
const int w[4] = {0,150,200,350};
int T;
cin>>T;
while(T--)
{
int n;
cin>>n;
for(int i = 0; i <= n; i++)dp[i] = 0;
for(int i = 1; i <= 3; i++)
for(int j = w[i]; j <= n; j++)
{//注意J的开始大小
dp[j] = max(dp[j], dp[j - w[i]] + value[i]);
}
/*或者写成 for(int i = 1; i <= 3; i++) for(int j = 0; j <= n; j++) { if(j - w[i] >= 0) dp[j] = max(dp[j], dp[j - w[i]] + value[i]); else dp[j] = dp[j]; } */
cout<<n - dp[n]<<endl;
}
return 0;
}
HDU 1284(传送门)
Problem Description
在一个国家仅有1分,2分,3分硬币,将钱N兑换成硬币有很多种兑法。请你编程序计算出共有多少种兑法。
Input
每行只有一个正整数N,N小于32768。
Output
对应每个输入,输出兑换方法数。
Sample Input
2934
12553
Sample Output
718831
13137761
ps:和完全背包类似,区别在于每添加进一枚硬币或者是新的种币,都要加上没增加之前的数量。
//使用二维数组
#include<iostream>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
int dp[4][33000] = {0};
dp[0][0] = 1;
int value[4] = {0,1,2,3};
for(int i = 1; i <= 3; i++)
for(int j = 0; j <= 32768; j++)
{
if(j - value[i] >= 0)
dp[i][j] = dp[i-1][j] + dp[i][j - value[i]];
else
dp[i][j] = dp[i-1][j];
}
int n;
while(cin>>n)
{
cout<<dp[3][n]<<endl;
}
return 0;
}
//使用一维数组
#include<iostream>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
int dp[33000] = {0};
dp[0] = 1;
int value[4] = {0,1,2,3};
for(int i = 1; i <= 3; i++)
for(int j = i; j <= 32768; j++)
dp[j] = dp[j] + dp[j - value[i]];
int n;
while(cin>>n)
{
cout<<dp[n]<<endl;
}
return 0;
}