Link: http://acm.hdu.edu.cn/showproblem.php? PID = 2546 http://acm.hust.edu.cn/vjudge/contest/view.action? Cid = 29096 # Problem/C meal card
Time Limit: 5000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 7371 accepted submission (s): 2514
Problem description the dining card of the E-Science and Technology Department's canteen has a very strange design, that is, to determine the balance before purchasing. If the remaining amount on the card is greater than or equal to 5 yuan before purchasing a product, the purchase will be successful (even if the balance on the card is negative after purchase); otherwise, the purchase will fail (even if the amount is sufficient ). Therefore, we all hope to minimize the balance on the card.
One day, N kinds of vegetables are sold in the canteen, and each type of food can be purchased once. If you know the price of each dish and the balance on the card, ask the minimum amount of balance on the card.
Input Multiple groups of data. For each group of data:
The first positive integer, N, indicates the number of dishes. N <= 1000.
The second line contains n positive integers, indicating the price of each dish. The price cannot exceed 50.
The third row contains a positive integer m, indicating the balance on the card. M <= 1000.
N = 0 indicates that the data has ended.
For each group of inputs, output a line containing an integer, indicating the possible minimum balance on the card.
Sample Input
1505101 2 3 2 1 1 2 3 2 1500
Sample output
-4532
Sourceuestc 6th Programming Contest online
Recommendlcy
Algorithm: greedy idea + 01 backpack idea: a very bare 01 backpack, a slight conversion will be wa many times. I think they are talking about their ideas. This is a very simple question, basic skills are not solid
If the starting m is smaller than 5, you cannot buy anything and output M directly.
Otherwise, buy the most expensive dish for 5 yuan first. [do not worry about whether 5 yuan is left, because 5 yuan is the basis for buying things, then you can buy the remaining 5 yuan at will]
Apply the 01 backpack for the rest. M-5 as backpack capacity. The price of each dish is regarded as the cost and value.
Then output the remaining money: start to buy the most expensive of the remaining money + apply the thought of backpack the remaining money [can be thought of negative] M-max (A) + M-5 + dp [M-5] PS: recently I don't know what's going on. I always need to add the address symbol in the output, and I can't find any errors in various reasoning. Code:
# Include <stdio. h> # include <string. h ># include <algorithm> using namespace STD; const int maxn = 1000 + 10; int A [maxn]; int DP [maxn]; int n, m; int main () {While (scanf ("% d", & N )! = EOF) {If (n = 0) break; memset (DP, 0, sizeof (DP); For (INT I = 1; I <= N; I ++) scanf ("% d", & A [I]); sort (a + 1, A + n + 1 ); // sort out the most expensive scanf ("% d", & M); If (M <5) // you cannot buy it at the beginning, directly output {printf ("% d \ n", m); continue; // do not add the address character to the output, wa to tears to orz} m-= 5; // buy the most expensive dish with 5 yuan first, and then start to buy it from 0, which facilitates conversion to 01 backpack for (INT I = 1; I <N; I ++) for (Int J = m; j> = A [I]; j --) DP [J] = max (DP [J], DP [J-A [I] + A [I]); printf ("% d \ n ", m-DP [m] + (5-A [N]);} return 0 ;}