bjtu1024

来源:互联网 发布:淘宝开店装修模板 编辑:程序博客网 时间:2024/05/01 14:56

几个月不刷题后发现真的忘的差不多了,最近帮老师手打备课笔记,说让我顺带帮忙找找错误的,在0-1背包那份笔记中提到了北交大的一道题目,于是兴趣一来,准备上去AC一下,结果遇到了好几个问题,刷了好几遍才出题,当时真的是感慨万千的。

先上题目吧:

Going to the Movies

Time Limit: 1000 MS    Memory Limit: 65536 KbTotal Submission: 201   Accepted: 75

Description

Farmer John is taking some of his cows to the movies! While his truck has a limited capacity of C (100 <= C <= 5000) kilograms, he wants to take the cows that, in aggregate, weigh as much as possible without exceeding the limit C.

Given N (1 <= N <= 16) cows and their respective weights W_i, determine the weight of the heaviest group of cows that FJ can take to the movies.

Input

Many cases,every case input like this:

* Line 1: Two space-separated integers: C and N

* Lines 2..N+1: Line i+1 contains a single integer: W_i

Output

Every case shoule output one line:

A single integer that is the weight of the heaviest group of cows that can go to the movies

Sample Input

259 5
81
58
42
33
61

Sample Output

242

Source

USACO 2008 Open Competition


题目大意是:有一个农夫,要带这自己的n头牛去拍戏,于是准备用卡车来运输他的n头牛,但是在这里遇到了一个问题就是卡车的最大载重量是c,于是机智的卡车司机准备做多次运输,为了减少卡车的运输次数,农夫想每次都尽可能使得自己运输的牛总重量最大。

现在要求你写一个程序,求出第一次卡车运输的最大重量。

首先以为直接用二维数组的就过了的,结果爆内存,没办法了,只能用优化内存的版本了。

#include<stdio.h>#include<string.h>int main(){    unsigned int a[20],w[5005],i,j,c,n;    while(scanf("%d%d",&c,&n)!=EOF)    {        for(i=1;i<=n;i++)            scanf("%d",&a[i]);        memset(w,0,sizeof(w));        for(i=1;i<=n;i++)            for(j=c;j>=a[i];j--)            {                if(w[j]<=a[i]+w[j-a[i]])                    w[j] = a[i] + w[j-a[i]];            }        printf("%d\n",w[c]);    }    return 0;}








0 0
原创粉丝点击