3407: [Usaco2009 oct]bessie ' s Weight problem Bessie's weight problem

Source: Internet
Author: User

3407: [Usaco2009 oct]bessie ' s Weight problem Bessie's weight problem time limit:3 Sec Memory limit:128 MB
submit:48 solved:46
[Submit] [Status] [Discuss] Description Bessie, like many of her sisters, grew too much fat by eating too much delicious grass from John's lawn. So John puts her in a strict diet plan. She can't eat more than H (5≤ day ≤45000) kg of hay every day. Bessie could only eat a whole bundle of hay, and when she began to eat a bundle of hay, she could no longer stop. She had a complete N (1≤n≤500) bundle that could give her a list of hay for supper. She naturally wants to eat as much hay as possible.    Naturally, each bundle of hay can only be eaten once (even though the same weight may occur 2 times in the list, this represents two bales of hay, where each bundle of hay can only be eaten once). Given a list of the weights of each bundle of hay Si (1≤si≤h), ask Bessie to eat as much hay without exceeding the limits of dieting (note that once she starts eating a bundle of hay, she will eat all that bundle of hay).    Input line 1th: Two integer days and n separated by a space. 2nd to N+1: Line i+l is a single integer representing the weight of the bales of hay si. Output A single integer indicating how many kilograms of hay can be eaten by Bessie within the limits. Sample Input56 4
15
19
20
21Sample Output56hint

There are four bales of grass, the weight of which is 15,19,20 and 21. Betsy can eat as much as she wants to eat within a 56-kilogram limit.


Bessie could eat 3 bales of hay (15,20,21, respectively). Just to reach her limit of 56 kilograms.


Source

Gold

Puzzle: Very classic 01 backpack, do not understand why all can be on the gold group = =

Method One: The most basic two-dimensional array method, which uses a scrolling array, common to only need to store the last few results of the DP problem

1/**************************************************************2Problem:34073 User:hansbug4 language:pascal5 result:accepted6Time:1120Ms7Memory:1396KB8****************************************************************/9  Ten var One I,j,k,l,m,n:longint; AA:Array[0..1,0..100000] ofLongint; -B:Array[0..100000] ofLongint; - functionMax (x,y:longint): Longint; the          begin -               ifX>y ThenMax:=xElsemax:=y; -          End; - begin + readln (m,n); -       fori:=1  toN Doreadln (B[i]); +       fori:=1  toN Do A           forj:=0  toM Do at              ifJ<b[i] ThenA[iMoD 2, j]:=a[(i+1)MoD 2, j]ElseA[iMoD 2, J]:=max (a[(i+1)MoD 2, j],a[(i+1)MoD 2, j-b[i]]+b[i]); -Writeln (a[nMoD 2, M]); - Readln; - End.

Method Two: Directly according to the "Backpack Nine" above algorithm, with a one-dimensional array (in fact, the core part of the loop as long as the loop direction upside down can be directly into a complete backpack, think why ^_^)

1/**************************************************************2Problem:34073 User:hansbug4 language:pascal5 result:accepted6Time:488Ms7Memory:656KB8****************************************************************/9  Ten var One I,j,k,l,m,n:longint; AA:Array[0..100000] ofLongint; -B:Array[0..10000] ofLongint; - functionMax (x,y:longint): Longint; the          begin -               ifX>y ThenMax:=xElsemax:=y; -          End; - begin + readln (m,n); -       fori:=1  toN Doreadln (B[i]); +       fori:=1  toN Do A           forJ:=mDownto 0  Do at              ifJ>=b[i] ThenA[j]:=max (a[j],a[j-b[i]]+b[i]); - Writeln (a[m]); - Readln; - End.

In summary, it turns out that the same time complex two kinds of writing speed actually worse than one times Qaq, said has been frightened urine

3407: [Usaco2009 oct]bessie ' s Weight problem Bessie's weight problem

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.