HDU 5543 Pick The sticks (01 backpack)

Source: Internet
Author: User

Test instructions: Enter T, representing the T Group sample, enter N,len, indicating the number of items and the length of the container. Enter n line a,v to indicate the length and value of an item. Each item can be placed on a container only half of the time (in order to balance, if it is an object, no matter how long it can be placed on the container), can not overlap, the container can put the maximum value is how much.

Analysis: dp[i][j][k] means that the first I item in a container of length J has a maximum value of k items at the edge.

#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<queue>#include<cmath>#include<cstdlib>#include<map>using namespacestd;structstick{intA, V;} s[1005];Long Longdp[4005][3];intMain () {intT; scanf ("%d", &t); inttt =1; intN, Len;  while(t--) {scanf ("%d%d", &n, &Len); Len*=2; Long LongAns =0;  for(intI=1; i<=n; i++) {scanf ("%d%d", &AMP;S[I].A, &s[i].v); S[I].A*=2; Ans= Max (ans, (Long Long) S[I].V); } memset (DP,0,sizeof(DP));  for(intI=1; i<=n; i++)        {             for(intJ=len; j>=s[i].a/2; j--)            {                 for(intk=0; k<3; k++)                {                    if(j>=s[i].a) Dp[j][k]= Max (Dp[j][k], dp[j-s[i].a][k]+s[i].v);//General Conditionsif(k>0) Dp[j][k]= Max (Dp[j][k], dp[j-s[i].a/2][k-1]+s[i].v),///Add the item to the edge, then this item occupies the amount of A/2, the optimal solution of this state is related to having k-1 items on the edge. Ans=Max (ans, dp[j][k]); }}} printf ("Case #%d:%lld\n", tt++, ans); }    return 0;}

HDU 5543 Pick The sticks (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.