Hdu--2639--bone Collector ii--01 Backpack

Source: Internet
Author: User

Bone Collector IITime limit:5000/2000 MS (java/others) Memory limit:32768/32768 K (java/others)Total Submission (s): 2464 Accepted Submission (s): 1295Problem DescriptionThe Title of this problem be familiar,isn ' t it?yeah,if you had took part in the ' Rookie Cup ' competitio N,you must has seem this title. If you haven ' t seen it before,it doesn ' t matter,i would give you a link:here is the Link:http://acm.hdu.edu.cn/showproblem. Php?pid=2602today We is not desiring the maximum value of bones,but the k-th maximum value of the bones. NOTICE That,we considerate and ways that get the same value of bones is the same. That means,it'll be a strictly decreasing sequence from the 1st maximum, 2nd maximum. To the k-th maximum. If the total number of different values are less than k,just ouput 0. inputthe first line contain a integer T, the Nu Mber of cases. Followed by T cases, each case three lines, the first line contain both integer n, v, K (n <=, v <=, K & lt;=) representing the number of bones and the volume of his bag and the K we need. And the second line contain N integers representing the value of each bone. The THIRD line contain N integers representing the volume of each Bone. outputone integer per line representing the k-th ma Ximum of the total value (this number would be is less than 231).  sample Input
35 10 21 2 3 4 55 4 3 2 15 10 121 2 3 4 55 4 3 2 15 10 161 2 3 4 55 4 3 2 1

Sample Output
1220


Test instructions: Give you the information of n Bones (value and volume), give you a backpack of volume V, ask you to find this back packing bone to get all the value of the K-large

Think about a half-day do not know Baidu, Baidu after or do not understand the results to see the replacement report, see a half-day look is not know what bloggers want to say is what, so the control of the code against the data things, and then just almost ignorant

Analysis: The conventional 01 backpack is the V volume of the container loaded items to get the maximum value, such as the requirements of the K-big is a little too much, so to add material, backpack Nine said inside the formula is

F[X]=MAX[F[X-V]+W,F[X]), require K large value must be recorded more data, so to the array plus one dimension into f[[], the second dimension is used to record K data, respectively, the largest descending to K, and then directly with 01 backpack method to seek is, originally f[x][1~ K] only k data, for 1~k all do the above formula to deal with, and then get the data is 2*k, understand? It's f[x][k] and f[x-v][k]+w, and then this data takes the largest K records down the line.

In fact, I still do not understand, the verification of a half-day is not to give me the conclusion of peace of conscience, why this can ensure that the results can be obtained all values, some of the episodes will not say, look at the code bar!


#include <iostream> #include <cstdio> #include <cstring>using namespaceStd;intMain (void){intN,V,M,I,J,K,L,T;__int64Dp[1111][ -],A[2][ -],Vol.[111],Val[111];scanf("%d",&T); while(T--&&~scanf("%d%d%d",&N,&V,&M))    { for(I=0;I<N;I++) {scanf("%i64d",&Val[I]); } for(I=0;I<N;I++) {scanf("%i64d",&Vol.[I]); }Memset(Dp,0,sizeof(Dp)); for(I=0;I<N;I++) { for(J=V;J>=Vol.[I];J--) { for(K=0;K<M;K++) Each  data in the second dimension is recorded with an inherited value and a preferred value {A[0][K]=Dp[J][K]; /  /Do not choose to load the first itemA[1][K]=Dp[J-Vol.[I]][K]+Val[I]; /  /load Item i}intP,Q,W;P=Q=W=0;A[0][M]=A[1][M]=-1; /  /Consider the case where one of the arrays is gone, so the end-1 has magical while(W<M&& (P<M||Q<M))                {if(A[0][P]<A[1][Q])                    {Dp[J][W]=A[1][Q];Q++; }Else                    {Dp[J][W]=A[0][P];P++; }if(W==0||Dp[J][W]!=Dp[J][W-1])//with duplicate valuesW++; }            }        }Printf("%i64d\n",Dp[V][M-1]); }return0;}

Summary: This is a new way of writing, at least for me, I have not really understood this practice, just a simple note down, it is useless to write down, to understand the line, the two days to see it, until the understanding!


Hdu--2639--bone Collector ii--01 Backpack

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.