Rice Card Time limit:5000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 14463 Accepted Submission (s): 5023
Problem Description UESTC part of the canteen meal card has a very strange design, that is, before purchasing to determine the balance. If the remaining amount on the card is greater than or equal to 5 yuan before the purchase of an item, the purchase must be successful (even if the balance is negative on the card after purchase), it cannot be purchased (even if the amount is sufficient). So we all want to try to keep the balance on the card to the minimum.
One day, the canteen has n vegetables for sale, each vegetable can be purchased once. If you know the price of each vegetable and the balance on the card, ask at least how much of the balance on the card.
Input multiple sets of data. For each group of data:
The first action is an integer n, which indicates the number of dishes. n<=1000.
The second line consists of n positive integers representing the price per vegetable. The price does not exceed 50.
The third line includes a positive integer m, which represents the balance on the card. m<=1000.
N=0 indicates the end of the data.
Output for each set of inputs, outputs a line that contains an integer that represents the smallest possible balance on the card.
Sample Input
1 50 5 10 1 2 3 2 1 1 2 3 2 1 50 0
Sample Output
-45 32
<span style= "FONT-SIZE:14PX;" >for ( i = 1; I <=n; i++)
for ( j = m; J >= Price[i]; j--)
dp[j] = __max (dp[j), dp[j-price[i]]+pric E[i]);</span>
In fact, the two-dimensional DP equation should be Dp[i][j]=max (Dp[i-1][j],dp[i-1][j-price[i]]+price[i]) for a one-dimensional, in the calculation of dp[j], because the value of dp[j] has not been updated, so at this time the right side of the equal sign Dp[j] Represented in fact is dp[i-1][j], and at this time we also need a dp[i-1][j-price[i]], obviously, according to just described, in fact, we need is a not modified by the first cycle, Dp[j-price[i]], to it in the current calculation of the dp[j] is not modified, that is to say, from for (M to 1), because if for (1:M), will recalculate Dp[j], has been calculated dp[j-price[i], that is, the meaning of the DP "I" [j-price[i]], and no longer is the DP "I-1" [ J-price[i]]
#include <cstdio> #include <cmath> #include <cstring> #include <string> #include <algorithm&
Gt
#include <iostream> using namespace std;
#define INF 0x3f3f3f3f int n,m;
int price[1005];
int dp[1005]; /* tat~~ This part is a recursive notation. Recursion and recursion complexity seem to be the same. But because the problem of recursion itself is relatively inefficient, it is better understood.
The subject recursion 300+, recursion 15MS//Of course to get an array of dp[maxn][maxn] int res;
if (i==1) {if (J>=price[i]) res=price[i]; else res=0;
} else if (J>=price[i]) Res=__max (REC (I-1,J), rec (i-1,j-price[i]) +price[i]);
else Res=rec (I-1,J);
return dp[i][j]=res; } */int main () {while (scanf ("%d", &n)!=eof) {int i,j; if (!n) break; for (i=1;i<=n;i++) {scanf ("%d", &am
P;price[i]);
} sort (price+1,price+n+1);
scanf ("%d", &m);
if (n==1) {printf ("%d\n", m-price[1]); continue;}
n--;
if (m<5) {printf ("%d\n", m); continue;} m-=5;
Memset (Dp,0,sizeof (DP)); for (i = 1; I <=n; i++) for (j = m; J >= Price[i]; j--) d P[J] = __max (Dp[j], dp[j-price[i]]+price[i]);
J Why should I see the above annotated printf ("%d\n", m-dp[m]+5-price[n+1]) in reverse order;
} return 0;
}