bjtu1024

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

先上题目吧:

Going to the Movies

Time Limit: 1000 MS    Memory Limit: 65536 Kb
Total 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值