BZOJ——1606: [Usaco2008 Dec]Hay For Sale 购买干草

http://www.lydsy.com/JudgeOnline/problem.php?id=1606

Time Limit: 5 Sec  Memory Limit: 64 MB
Submit: 1420  Solved: 1052
[Submit][Status][Discuss]

Description

    约翰遭受了重大的损失:蟑螂吃掉了他所有的干草,留下一群饥饿的牛.他乘着容量为C(1≤C≤50000)个单位的马车,去顿因家买一些干草.  顿因有H(1≤H≤5000)包干草,每一包都有它的体积Vi(l≤Vi≤C).约翰只能整包购买,
他最多可以运回多少体积的干草呢?

Input

    第1行输入C和H,之后H行一行输入一个Vi.

Output

 
    最多的可买干草体积.

Sample Input

7 3 //总体积为7,用3个物品来背包
2
6
5


The wagon holds 7 volumetric units; three bales are offered for sale with
volumes of 2, 6, and 5 units, respectively.

Sample Output

7 //最大可以背出来的体积

HINT

Buying the two smaller bales fills the wagon.

Source

Silver

 1 #include <cstdio>
 2 
 3 #define max(a,b) (a>b?a:b)
 4 
 5 inline void read(int &x)
 6 {
 7     x=0; register char ch=getchar();
 8     for(; ch>'9'||ch<'0'; ) ch=getchar();
 9     for(; ch>='0'&&ch<='9'; ch=getchar()) x=x*10+ch-'0';
10 }
11 int C,H,v[5026],f[50626];
12 
13 int Presist()
14 {
15     read(C),read(H);
16     for(int i=1; i<=H; ++i) read(v[i]);
17     for(int i=1; i<=H; ++i)
18       for(int j=C; j>=v[i]; j--)
19         f[j]=max(f[j],f[j-v[i]]+v[i]);
20     printf("%d
",f[C]);
21     return 0;
22 }
23 
24 int Aptal=Presist();
25 int main(int argc,char**argv){;}
——每当你想要放弃的时候,就想想是为了什么才一路坚持到现在。
原文地址:https://www.cnblogs.com/Shy-key/p/7707875.html