ACM 之 M - 基礎(chǔ)DP

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 2 ^31).

Sample Input

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

Sample Output

14

理解:

第一道DP題,也就是01背包問題,我的做法就是多看多寫多理解。
另外,個(gè)人感覺用一位數(shù)組更簡潔明了。

代碼部分

#include <cstdio>
#include <cstring>
using namespace std;
int va[101],vo[101],dp[100002],t,n,v;
inline int Max(int x,int y)
{return x>y?x:y;}
int main()
{
    while(scanf("%d",&t)!=EOF)
    {
        getchar();
        for(int i=0;i<t;i++)
        {
            scanf("%d%d",&va[i],&vo[i]);
        }getchar();
        scanf("%d",&v);getchar();
        memset(dp,0,sizeof(dp));
        for(int i=0;i<t;i++)
            for(int j=vo[i];j<=v;j++)
                dp[j]=Max(dp[j],dp[j-vo[i]]+va[i]);
        printf("%d\n",dp[v]);
    }
    return 0;
}

意見反饋 || 任何建議

聯(lián)系我(新浪)
郵箱:qianlizhihao@gmail.com

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 最近這些日子,現(xiàn)實(shí)成功地打消了本科畢業(yè)時(shí)對(duì)未來的種種幻想。 到了找工作的階段,對(duì)自己的感覺在所不能和一無是處之間不...
    馮昱霖閱讀 1,405評(píng)論 10 12
  • 我的文字可能不是很美,語言組織可能也不是很嚴(yán)謹(jǐn),內(nèi)容可能也不夠精彩,可是我想慢慢寫點(diǎn)遠(yuǎn)離朋友圈遠(yuǎn)離生活圈的事情。 ...
    開心逗不開心閱讀 290評(píng)論 2 2
  • 第一課,真誠的課題 分享心得:真誠,一直以來都被教育說要真誠待人,真誠待人—這一說,是要真誠的對(duì)待別人,真誠對(duì)待顧...
    郭曉萱閱讀 1,006評(píng)論 0 2
  • 偶然看到淘寶上在賣一種名為“昨天”的酒。其實(shí)也不過是普通的梅子酒,只是包裝好看,名字特色而已。 “昨天”...
    李青茴閱讀 373評(píng)論 0 1

友情鏈接更多精彩內(nèi)容