bzoj 1606 购买干草

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 //最大可以背出来的体积
 
思路 : 01背包
 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define R register int
 4 #define rep(i,a,b) for(R i=a;i<=b;i++)
 5 #define Rep(i,a,b) for(R i=a;i>=b;i--)
 6 #define ms(i,a)    memset(a,i,sizeof(a))
 7 #define gc()       getchar()
 8 template<class T>void read(T &x){
 9   x=0 ; char c=0;
10   while (!isdigit(c))  c=gc();
11   while (isdigit(c)) x=x*10+(c^48),c=getchar();
12 }
13 int const N=50003;
14 int c,n,a[N],f[N];
15 int main(){
16   read(c); read(n);
17   rep(i,1,n) read(a[i]);
18   rep(i,1,n) Rep(j,c,a[i]) f[j]=max(f[j],f[j-a[i]]+a[i]);
19   printf("%d\n",f[c]);
20   return 0;
21 }
View Code

猜你喜欢

转载自www.cnblogs.com/ZJXXCN/p/10219430.html