1. 程式人生 > >bzo1606: [Usaco2008 Dec]Hay For Sale 購買幹草

bzo1606: [Usaco2008 Dec]Hay For Sale 購買幹草

lds key function medium usaco bmi com ex18 math

1606: [Usaco2008 Dec]Hay For Sale 購買幹草

Time Limit: 5 Sec Memory Limit: 64 MB
Submit: 1338 Solved: 991
[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

題解

超級裸的01背包dp 打板子就過了(

/************************************************************** Problem: 1606 User: a799091501 Language: C++ Result: Accepted Time:292 ms Memory:1608 kb ****************************************************************/ #include<iostream> #include<cstdio> #include<cstring>
#include<algorithm> #include<cmath> #include<queue> #include<stack> #define N 100001 using namespace std; inline int read() { int f=1,x=0;char ch=getchar(); while(ch>‘9‘|ch<‘0‘) { if(ch==‘-‘) f=-1; ch=getchar(); } while(ch<=‘9‘&&ch>=‘0‘) { x=(x<<3)+(x<<1)+ch-‘0‘; ch=getchar(); } return f*x; } int main() { int c=read(),h=read(),j,i,b[100001],v[100001]; b[0]=1; for(i=1;i<=h;i++) { v[i]=read(); for(j=c-v[i];j>=0;j--) if(b[j]) b[j+v[i]]=1; } int ans; for(i=0;i<=c;i++) if(b[i])ans=i; cout<<ans; }

bzo1606: [Usaco2008 Dec]Hay For Sale 購買幹草