HDU 2546 Rice Card (0-1 backpack)

Source: Internet
Author: User

Topic Source: HDU 2546 Rice Card

Rice card
Time limit:5000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 16645 Accepted Submission (s): 5797

Problem Description
UESTC Department Canteen's rice card has a very strange design, that is, before the purchase 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, the output line 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
Main topic:
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. There are n vegetables for sale in the canteen, 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.
Topic Analysis:
When the balance is greater than or equal to 5 yuan, we used to buy the most expensive goods, by the title know, certainly can buy success, and then consider the remaining balance can buy the maximum value, here we can enter the price of goods from small to large to sort, then the last side of the balance of 5 yuan when the purchase, the remaining n-1 a commodity, We can use 0-1 backpack to find the maximum purchase value.
AC Code:

#include <stdio.h>#include <string.h>#include <algorithm>using namespace STD;intf[10010];intw[10010];intMain () {intN,v; while(scanf("%d", &n), N) {memset(F,0,sizeof(f));memset(W,0,sizeof(w)); for(intI=0; i<n;i++) {scanf("%d", &w[i]); } sort (w,w+n);//Sort from small to large        scanf("%d", &v);if(v<5)        {printf("%d\n", v);Continue; } for(intI=0; i<n-1; i++)//Simply select the knapsack problem to remove the most value{ for(intj=v-5; j>=w[i];j--)//maximum capacity is the total capacity minus that 5 yuan, that 5 yuan to buy the most expensive items{F[j]=max (f[j],f[j-w[i]]+w[i]); }        }printf("%d\n", v-f[v-5]-w[n-1]);//The balance of the output is the total balance minus the maximum price of the item minus the maximum consumption that can be purchased with the remaining balance}return 0;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

HDU 2546 Rice Card (0-1 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.