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)