Question: give you the price for n dishes and ask how much m yuan is left at least. One condition is that you can click it once if it is greater than or equal to 5 yuan. The amount on the card must be greater than 5.
First:
Whether the first n-1-1 items can constitute j RMB for dp [j]
Deformation 01 backpack. M-sum if the value and value of the entire dish are smaller than m;
Otherwise, in ascending order, the last dish purchased with e-certificate 5 must be the last one. First n-1-1 backpack into, to see how much data can be placed, and then determine what can appear after the M-5, the use of rules to replace n items
# Include <iostream> # include <cmath> # include <cstdio> # include <cstring> # include <algorithm>Using namespaceStd;# Define max (a, B) (a)> (B )? (A): (B) # define min (a, B) (a) <(B )? (A): (B) # define maxn 1100IntA[Maxn]; IntDp[Maxn]; Int main () {intN; IntI,J,M; While (~Scanf("% D",&N)&&N) {(I=1;I<=N;I++)Scanf("% D",A+I);Scanf("% D",&M); If (M<5){Printf("% D \ n",M); Continue ;}Sort(A+1,A+1+N);Memset(Dp,0, Sizeof (Dp));Dp[0] =1; IntAns=0; (I=1;I<N;I++) (J=M+50;J> =A[I];J--) If (Dp[J-A[I])Dp[J] =1,Ans=Max(Ans,J); If (Ans+A[N] <=M)Printf("% D \ n",M-A[N]-Ans); Else {(I=M;I> =0;I--) If (Dp[I]) {If (I<=M-5){Ans=Min(Ans,M-I-A[N]); Break;} elseAns=Min(Ans,I);}Printf("% D \ n",Ans) ;}} Return 0;}
The second type: use M-5 fully loaded food, and finally use 5 loaded n, you can
For the n-1-1 items in dp [j], you can buy a maximum of j yuan for food.
# Define max (a, B) (a)> (B )? (A): (B) # define min (a, B) (a) <(B )? (A): (B) # include <stdio. h> # include <string. h >#include <iostream >#include <algorithm> using namespace std; int main () {int I, j, k; int t, n, m; int maxn; int dp [2014] = {0}, price [2014] = {0}; while (scanf ("% d", & n )! = EOF, n) {memset (price, 0, sizeof (price); memset (dp, 0, sizeof (dp); for (I = 1; I <= n; I ++) {cin> price [I];} cin> m; if (m <5) {printf ("% d \ n ", m); continue;} sort (price + 1, price + n + 1); maxn = price [n]; m = M-5; for (I = 1; I <n; I ++) {for (j = m; j> = price [I]; j --) {dp [j] = max (dp [j], dp [j-price [I] + price [I]);} printf ("% d \ n", m + 5-dp [m]-maxn );} return 0;}/* 1505101 2 3 2 1 2 2 2 150610 30 45 50 15111610 45 50 25 154 */
HDU 2546 meal card (deformation 01 backpack) two ideas