2014
02-10

# Bone Collector

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 ?

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.

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.

1
5 10
1 2 3 4 5
5 4 3 2 1

14

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define N 1001
int dp[N];
int c[N],w[N];
int max(int x,int y)
{
return x>y?x:y;
}
int main()
{
int t,n,v,i,j;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&v);
for(i=0;i<n;i++)
scanf("%d",&c[i]);
for(i=0;i<n;i++)
scanf("%d",&w[i]);
memset(dp,0,sizeof(dp));
for(i=0;i<n;i++)
{
for(j=v;j>=w[i];j--)
dp[j]=max(dp[j],dp[j-w[i]]+c[i]);
}
printf("%d\n",dp[v]);
}
system("pause");
return 0;
}

//二维数组实现01背包
#include <iostream>
#include <algorithm>
using namespace std;
int dp[1001][1001],vol[1001],val[1001];
int main()
{
int t,n,i,j,v;
cin>>t;
while(t--)
{
cin>>n>>v;
for(i=1;i<=n;i++)
scanf("%d",&val[i]);
for(i=1;i<=n;i++)
scanf("%d",&vol[i]);
memset(dp,0,sizeof(dp)); //初始化
for(i=1;i<=n;i++)
for(j=0;j<=v;j++) //注意要从0开始，这个题测试数据有点变态，有的骨头有价值，但占的空间是0
{
if(vol[i]<=j && dp[i-1][j]<dp[i-1][j-vol[i]]+val[i])
dp[i][j]=dp[i-1][j-vol[i]]+val[i];
else
dp[i][j]=dp[i-1][j];
}
cout<<dp[n][v]<<endl;
}
return 0;
}

1. 换句话说，A[k/2-1]不可能大于两数组合并之后的第k小值，所以我们可以将其抛弃。
应该是，不可能小于合并后的第K小值吧

2. 嗯 分析得很到位，确实用模板编程能让面试官对你的印象更好。在设置辅助栈的时候可以这样：push时，比较要push的elem和辅助栈的栈顶，elem<=min.top()，则min.push(elem).否则只要push（elem）就好。在pop的时候，比较stack.top()与min.top(),if(stack.top()<=min.top()),则{stack.pop();min.pop();}，否则{stack.pop();}.

3. Excellent Web-site! I required to ask if I might webpages and use a component of the net web website and use a number of factors for just about any faculty process. Please notify me through email regardless of whether that would be excellent. Many thanks