Two solutions to the problem of "full x discount" in promotion: Dynamic programming and enumeration method

Source: Internet
Author: User

Problem Description: Given an unordered array of int num[n], which put the price of goods in N, the merchant stipulated full m-dollar has preferential, the goods do not repeat the purchase, then from the buyer's point of view how to buy to enjoy both the benefits and spend the least money.
Problem Analysis: Abstract into a mathematical problem is to take a number of elements from the array, so that their sum is greater than or equal to the minimum value of M. Below I have introduced the solution idea and the code from the enumeration method and the dynamic programming angle respectively.
1. Enumeration method
Perhaps, the first thing we think of is the enumeration method, n kinds of goods have a total of 2^n in the possible, calculate each possible and, then find the satisfying conditions. Paste the code first and then explain. The Java code is as follows:

    public static int toxdiscount (int[] num, int m) {
        int n = num.length;
        int sum = 0, ans = integer.max_value;
        for (int i=0; i<n; i++) {
            sum + = Num[i];
        }
        if (Sum < m) {
            return-1;
        }
        else if (sum = = m) {
            return m;
        }

        for (int s=1; s< (1<<n); s++) {//1<=s<1024
            int t = 0;
            for (int j=0; j<n; J + +) {
                if (((S>>J) &1)! = 0) {
                    t = t + num[j];
                }
            }
            if (t>=m && t<ans) {
                ans = t;
            }
        }
        return ans;
    }

The

s takes a value of 1-1024, which represents 2^10, for each s value taken to move the binary representation of S to the right and with 1 bitwise AND, take out the position of all 1 in the number of S binary representations, add up the commodity price of these locations to T, so that the sum of each possible commodity price is enumerated, Then from these and the smallest is the optimal solution. This method draws on the solution of the Hiho programming exercise 4.
2, dynamic programming
This is a problem similar to the 0-1 knapsack, set either of the two-dimensional array dp[n+1][sum+1] to take a number of dp[j][k]-from num[j],num[j+1],..., num[n-1]. They do not exceed the maximum value of K. The Java code is as follows:

Import java.math.*;
public static int Dpxdisc (int[] num, int m) {
    int n = num.length; 
    int sum = 0;
    for (int i = 0; i < n; i++) {
        sum + = Num[i];
    }
    if (Sum < m) {
        return-1;
    } else if (sum = = m) {
        return m;
    }

    Int[][] DP = new int[n+1][sum+1];//initial value defaults to 0 for
    (int i=m; i<=sum; i++) {for
        (int j=n-1; j>=0; j--) {
            for (int k=0; k<=i; k++) {
                if (Num[j] > k) {
                    dp[j][k] = dp[j+1][k];
                }
                else{
                    Dp[j][k] = Math.max (Dp[j+1][k],dp[j+1][k-num[j]]+num[j]);
        }}} if (dp[0][i] = = i) {
            return i;
        }
    }
    return sum;
}

As I gradually increment from m to sum, there must be a i0 that makes: Dp[0][i0]==i0, that is, from num[0],num[1],..., num[n-1] Any number of elements, which equals I0, and the first such i0 must be greater than or equal to the minimum value of M. At this point, the program stops and returns I0.

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.