Q

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 …<br>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 ?<br><center><img src=../../../data/images/C154-1003-1.jpg> </center><br>
 

Input
The first line contain a integer T , the number of cases.<br>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 2<sup>31</sup>).
 

Sample Input
   
   
   
   
1 5 10 1 2 3 4 5 5 4 3 2 1
 

Sample Output
   
   
   
   
14
题目大意1:
有个包体积一定房价值最大的东西
思路:一开始用贪心做的可是一直是runtime error 后来想了像用dp做的就过了贪心算法应该没啥问题,,,应该。。。毕竟测试数据就一组
贪心:按性价比排序
dp: F[i;v] 表示前i 件物品 恰放入一个容量为v的背包可以获得的最大价值。则其状态转移方程便是:F[i;v] = maxF[i-1;v];F[i-1;v-Ci] + Wig

用二维数组存,只考虑第i 件物品的策略(放或不放),那么就可以转化为一个只和前i-1件物品相关的问题。如果不放第i 件物品,那么问题就转化为“前i-1件物品放入容量为v的

背包中”,价值为F[i-1; v];如果放第i 件物品,那么问题就转化为“前i-1件物品放入剩下的容量为v-Ci 的背包中”,此时能获得的最大价值就是F[i-1;v-Ci] 再加上通过放入第i 件物品获得的价值Wi。

代码:dp

#include<iostream> #include<string.h> using namespace std; int dp[1000][1000]; int max(int x,int y) {     return x>y?x:y; } int main() {     int t,n,v,i,j;     int va[1000],vo[1000];     cin>>t;     while(t--)     {         cin>>n>>v;         for(i=1;i<=n;i++)             cin>>va[i];         for(i=1;i<=n;i++)             cin>>vo[i];         memset(dp,0,sizeof(dp));          for(i=1;i<=n;i++)         {             for(j=0;j<=v;j++)             {                 if(vo[i]<=j)                     dp[i][j]=max(dp[i-1][j],dp[i-1][j-vo[i]]+va[i]);                 else                     dp[i][j]=dp[i-1][j];             }         }         cout<<dp[n][v]<<endl;     }     return 0; }

贪心(应该对但是a不了的代码):

#include<iostream> #include<algorithm> using namespace std; struct bag {     int ve;     int v; }  b[20000]; bool cmp(bag a,bag b) {     return a.ve/a.v>=b.ve/b.v; } int main() {     int n;     cin>>n;     while(n--)     {         int bnum;         int bv;         cin>>bnum>>bv;         for(int i=0;i<bnum;i++)         cin>>b[i].ve;        for(int i=0;i<bnum;i++)         cin>>b[i].v;         sort(b,b+bnum,cmp);         int ans=0;          int tv=0;          for(int i=0;i<bnum;i++)             if(tv<bv)             {                 ans+=b[i].ve;                 tv+=b[i].v;             }             else                 break;             cout<<ans<<endl;     }     return 0; }

你可能感兴趣的:(Q)