Using Hangzhou Electric rice card problem to analyze the optimization of space complexity of 01 knapsack problem

Source: Internet
Author: User

Topic Link Click Open link

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, outputs a line that contains an integer that represents the smallest possible balance on the card. Sample Input

1 5 1 2 3 2 1 1 2 3 2 1-0 Sample Output
-45 32 This is a typical 01 knapsack problem, but there is a restriction on the above (if the remaining amount on the card is greater than or equal to $5 before the purchase of an item, it can be purchased successfully (even if the balance on the card after purchase is negative), it cannot be purchased (even if the amount is sufficient). But the final question is to return to the 01 knapsack problem (ask at least the balance on the card to how much), then how to consider this problem. I first give a set of data   according to their own original idea to do   will have a general idea: the number of dishes: 5     The price of the dishes are: 4,7,18,7,12   Rice card balance: 20 OK, now to let you buy vegetables so that the minimum balance on the rice card. I'm talking about my ideas, according to the restrictions I first deduct a minimum of 5 (although the remaining is not necessarily 5 faster, but certainly more than 5 faster) block money, let him buy the most expensive that dish (18 dollars of the road), and then the remaining balance and the remaining dishes carried 01 backpack, finally bought: after deducting 5 fast, card balance 15 , how to buy it. 7+7=14 This is the best choice (and the remaining 1), the last 5+1=6, to buy the most expensive that dish 6-18=-12. The final answer is-12. and the original 01 knapsack Problem difference: (1) Here Meal Card 01 knapsack problem and items loaded into the backpack inside there is a small difference is the goods pack backpack told the value and quality, and here did not tell you the quality, carefully thought will find that the backpack put the goods into the walk will be the quality of the goods to lose, here the same effect, After you buy the food, you deduct the price of the dish. Relative to the item put backpack, you can consider the price of the dish, that is, the value of the dish is the quality of the dish, and then the balance as a backpack capacity (2) There is no need to know where you bought the dishes, so do not have to look for X[i] The value of the next blog, first look at my first code
#include "iostream"
#include "math.h"
#include "algorithm"
using namespace std;

int main ()
{
	int a[1005],m[100][100],x[1005],n,sum,i,j,c;
	memset (A,0,sizeof (a));
	while (scanf ("%d", &n)!=eof&&n!=0)
	{for
	(i=1;i<=n;i++)
	{
		cin>>a[i];
	}
	
	Sort (a+1,a+n+1);
	cin>>c;
	memset (M,0,sizeof (m));
	if (c < 5)
	{
        printf ("%d\n", c);
	}
	else
	{for
        (i = 1; I <= n-1; i++)  
	{for
               (j = 0; J <=c-5; J + +)  
		{
                   M[i][j] = M[i-1][j];
  if (J>=a[i])
		   {
                    m[i][j] = M[i-1][j] > m[i-1][j-a[i]] + a[i]? M[i-1][j]: M[i-1][j-a[i]] + a[i]; 
		   }
		}
	}
	    Sum=c-m[n-1][c-5]-a[n];
	    cout<<sum<<endl;
	}}}
Notice here that at first my m two-dimensional array is such that the m[1005][1005] result is so that it is only later known that the problem is a two-dimensional array,



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.