2639-bone Collector II (01-Pack K-optimal solution)

Source: Internet
Author: User

Topic Links:

http://acm.hdu.edu.cn/showproblem.php?pid=2639

Key codes for K-Optimal solutions:

Use two arrays to record two states (select or not), and just record the first k times. Both arrays are the best possible solutions for the first K-times. So all we have to do is compare the two arrays and go straight to K.

Title Code:

1#include <iostream>2#include <cstdio>3#include <cstring>4 using namespacestd;5 intdp[1005][ *],a[ *],b[ *],val[ the],vol[ the];6 intMain ()7 {8     intt,n,v,k,c,d,e;9      while(~SCANF ("%d",&t))Ten     { One          while(t--) A         { -scanf"%d%d%d",&n,&v,&k); -              for(intI=0; i<n;i++) thescanf"%d",&val[i]); -              for(intI=0; i<n;i++) -scanf"%d",&vol[i]); -Memset (DP,0,sizeof(DP)); +Memset (A,0,sizeof(a)); -memset (b,0,sizeof(b)); +              for(intI=0; i<n;i++) A             { at                  for(intj=v;j>=vol[i];j--) -                 { -                      for(intt=1; t<=k;t++) -                     { -a[t]=dp[j-vol[i]][t]+Val[i]; -b[t]=Dp[j][t]; in                     } -C=d=e=1; to                      while(e<=k&& (c!=k+1|| d!=k+1)) +                     { -                         if(a[c]>B[d]) theDp[j][e]=a[c++]; *                         Else $dp[j][e]=b[d++];Panax Notoginseng                         if(dp[j][e]!=dp[j][e-1]) -e++; the                     } +                 } A             } theprintf"%d\n", Dp[v][k]); +         } -     } $     return 0; $}
View Code

2639-bone Collector II (01-Pack K-optimal solution)

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.