zoukankan      html  css  js  c++  java
  • HDUOJ--Bone Collector

    Bone Collector

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 21463    Accepted Submission(s): 8633


    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 231).
     
    Sample Input
    1 5 10 1 2 3 4 5 5 4 3 2 1
     
    Sample Output
    14
     
    Author
    Teddy
     
    Source
     
    Recommend
    lcy

    背包问题.....一定要多练习...

    代码:----

     1 #include<stdio.h>
     2 #include<string.h>
     3 #define maxn 1005
     4 int dp[maxn],arr[maxn][2];
     5 int max(int a,int b)
     6 {
     7     return a>b?a:b;
     8 }
     9 
    10 void zeroonepack(int cost ,int value,int v)
    11 {
    12      for(int i=v;i>=cost;i--)
    13          dp[i]=max(dp[i],dp[i-cost]+value);
    14 
    15 }
    16 int main()
    17 {
    18     int t,n,v ,i;
    19     scanf("%d",&t);
    20     while(t--)
    21     {
    22         scanf("%d%d",&n,&v);
    23         memset(dp,0,sizeof dp);
    24         for(i=0;i<n;i++)
    25         {
    26             scanf("%d",arr[i]+0);
    27         }
    28         for(i=0;i<n;i++)
    29         {
    30             scanf("%d",arr[i]+1);
    31         }
    32        for(i=0;i<n;i++)
    33            zeroonepack(arr[i][1],arr[i][0],v);
    34        printf("%d
    ",dp[v]);
    35     }
    36     return 0;
    37 }
    View Code

     优化后代码:

    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    struct st
    {
        int a;
        int b;
    };
    typedef struct st sta;
    int main()
    {
        int test,n,v,i,j;
        scanf("%d",&test);
        while(test--)
        {
            scanf("%d%d",&n,&v);
         int *dp =(int *)malloc(sizeof(int)*(v+1));
         sta *stu =(sta *)malloc(sizeof(sta)*(n+1));
            for(i=0;i<n;i++)
                scanf("%d",&stu[i].a);
            for(i=0;i<n;i++)
                scanf("%d",&stu[i].b);
            for(i=0;i<=v;i++)
                dp[i]=0;
    
           for(i=0;i<n;i++)
           {
               for(j=v ; j>=stu[i].b ; j--)
               {
                if(dp[j]<dp[j-stu[i].b]+stu[i].a)
                   dp[j]=dp[j-stu[i].b]+stu[i].a;
               }
           }
           printf("%d
    ",dp[v]);
           free(dp);
           free(stu);
        }
        return 0;
    }
  • 相关阅读:
    LeetCode:149_Max Points on a line | 寻找一条直线上最多点的数量 | Hard
    LeetCode: 150_Evaluate Reverse Polish Notation | 分析逆波兰式 | Medium
    LeetCode:151_Reverse Words in a String | 字符串中单词的逆反 | Medium
    Cellular Traffic Offloading
    在word 2010中采用EndNote X7插入引用
    屏幕截图和标记
    A Nice Paper About Mobile Data Offloading
    linux Redhat 6环境上通过源码包安装DRBD 8
    hbase shell中执行list命令报错:ERROR: org.apache.hadoop.hbase.PleaseHoldException: Master is initializing
    如何通过phoenix中查看表的主键信息
  • 原文地址:https://www.cnblogs.com/gongxijun/p/3236766.html
Copyright © 2011-2022 走看看