1606: [Usaco2008 dec]hay for Sale buy hay
Time Limit:5 Sec Memory limit:64 MB
submit:978 solved:735
[Submit] [Status] [Discuss]
Description
约翰遭受了重大的损失:蟑螂吃掉了他所有的干草,留下一群饥饿的牛.他乘着容量为C(1≤C≤50000)个单位的马车,去顿因家买一些干草. 顿因有H(1≤H≤5000)包干草,每一包都有它的体积Vi(l≤Vi≤C).约翰只能整包购买,
How many volumes of hay can he transport back?
Input
第1行输入C和H,之后H行一行输入一个Vi.
Output
最多的可买干草体积.
Sample Input
7 3//Total volume of 7, with 3 items to backpack
2
6
5
The wagon holds 7 volumetric units; Three bales is offered for sale with
Volumes of 2, 6, and 5 units, respectively.
Sample Output
7//maximum volume that can be backed up
idea : 01 knapsack judgment feasibility;
Code :
#include <stdio.h>#include <iostream>using namespace STD;intf[50005];intv[5005];intMain () {intC,h;scanf("%d%d", &c,&h); for(intI=1; i<=h;i++)scanf("%d", &v[i]); f[0]=1; for(intI=1; i<=h;i++) { for(intj=c;j>=v[i];j--)if(f[j-v[i]]==1) f[j]=1; } for(inti=c;i>=0; i--) {if(F[i]) {cout<<i<<endl;return 0; } }}
"Bzoj 1606" [Usaco2008 dec]hay for Sale buy hay 01 backpack