uva10465 (full backpack, requires full backpack)

Source: Internet
Author: User

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category= 114&problem=1406&mosmsg=submission+received+with+id+15392606

Give us the cost of two items, and the capacity of a backpack

Ask us to take as many items as possible with the least amount of wasted backpack

That is, the full knapsack problem is required, as long as the attention is initialized, the dp[0] is set to 0, the other dp[i] is set to 1, indicating illegal

Then a state must be transferred from a legitimate one, and then the maximum value is selected in the legal State transfer

DP, we enumerate the capacity in reverse order to find a valid DP state

Then the DP state is a waste of the least capacity state

1#include <stdio.h>2#include <string.h>3#include <stdlib.h>4#include <algorithm>5#include <iostream>6#include <queue>7#include <stack>8#include <vector>9#include <map>Ten#include <Set> One#include <string> A#include <math.h> - using namespacestd; - #pragmaWarning (disable:4996) thetypedefLong LongLL;  - Const intINF =1<< -; - /* -  + */ - inta[2]; + intdp[10000+Ten]; A intMain () at { -     intN, I, J; -      while(SCANF ("%d%d%d", &a[0], &a[1], &n)! =EOF) -     { -          for(i =1; I <= N; ++i) -Dp[i] =-1; indp[0] =0; -          for(i =0; I <2; ++i) to          for(j = a[i]; J <= N; + +j) +         { -             if(Dp[j-a[i]]! =-1)//Select the maximum value in a legitimate state transfer theDP[J] = max (Dp[j], Dp[j-a[i]] +1); *         } $          for(j = n; J >=1; --j)Panax Notoginseng             if(Dp[j]! =-1)//find a legal state . -              Break; the         if(J = =N) +printf"%d\n", Dp[j]); A         Else theprintf"%d%d\n", Dp[j], N-j); +     } -     return 0; $}
View Code

uva10465 (full backpack, requires full 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.