Ultraviolet A 10306 e-coins (full backpack: Two-Dimensional restrictions)

Source: Internet
Author: User

Ultraviolet A 10306 e-coins (full backpack: Two-Dimensional restrictions)

Http://uva.onlinejudge.org/index.php? Option = com_onlinejudge & Itemid = 8 & page = show_problem & problem = 1247

Question:

For each example, two numbers N and m are given, respectively, indicating that there are n kinds of coins. For each coin, there are two values: x, y, the question requires you to select some coins to satisfy M * m = x * x + y * Y. X is the sum of all x values of the selected coins, Y is the sum of the values of all selected coins, and there are countless coins. What is required now is the minimum number of coins used to meet the above requirements

Analysis:

The number of coins is infinite, which is obviously a complete backpack problem.Restrictions, What isTarget Condition?

The limitations of this question are: X value and Y value (these values constitute the limit value m)

The goal of this question is to minimize the number of coins selected.

General full backpack status DesignDP [I] [J] = X indicates that when the condition of the first I item is equal to = J after the decision is completed (or cannot exceed J ), the obtained target conditions are optimal (the minimum or maximum value may be required ).

Originally, I wanted to use m as the constraint for J, but I found that if M is used as the one-dimensional constraint, then you cannot launch the m' value before your previous decision based on the current m value and the selected item. therefore, we need to use the X and Y constraints for this question. (In fact, a dimension is added, and there is no essential change)

Status design: DP [k] [I] [J] = num indicates that the X value of all current coins is I, the Y value of all current coins and the minimum number of coins required for J are num. (unique m determined by X value and Y value)

Status transfer:

DP [k] [I] [J] = min (DP [k-1] [I] [J], DP [k] [I-X [k] [J-y [k])

How can we understand the above state transition equation?

First, let's look at DP [k] [I] [J], which indicates that the coin in the first K forms the sum of X value and the sum of I, Y value and J. So:

1. if we don't need the K coins at all (just use the front K-1 coins ), we can know that the DP [k-1] [I] [J] method can (without any K coins) constitute the State of X value and I, Y value and J.

2. if we use at least one k coin to form the target State, we can see that the DP [k] [I-X [k] [J-y [k] method can achieve the goal.

To sum up, DP [k] [I] [J] = min (DP [k-1] [I] [J], DP [k] [I-X [k] [J-y [k])

The initial value is DP [0] [0] [0] = 0, and the other value is Inf (infinity ).

In the end, we want to satisfy the minimum value of I * I + J * j = m * m in all DP [N] [I] [J.

The program uses a rolling array, so DP only has two dimensions: [I] [J.

AC code:

# Include <cstdio> # include <cstring> # include <algorithm> using namespace STD; # define INF 1e8const int maxn = 300 + 5; int n, m; // n is the number of currencies, and m is the value int X [maxn], Y [maxn]; // two attribute values corresponding to each currency: int DP [maxn] [maxn]; int main () {int t; scanf ("% d", & T ); while (t --) {// read input + initialize scanf ("% d", & N, & M); For (INT I = 1; I <= N; I ++) scanf ("% d", & X [I], & Y [I]); For (INT I = 0; I <maxn; I ++) for (Int J = 0; j <maxn; j ++) DP [I] [J] = inf; DP [0] [0] = 0; // recursive int NS = inf; For (int K = 1; k <= N; k ++) for (INT I = x [k]; I <maxn; I ++) for (Int J = Y [k]; j <maxn; j ++) {DP [I] [J] = min (DP [I] [J], DP [I-X [k] [J-y [k] + 1); if (I * I + J * j = m * m) // if k = N is added here, WA will think about why? Ans = min (ANS, DP [I] [J]);} // output if (ANS = inf) printf ("Not possible \ n "); else printf ("% d \ n", ANS);} return 0 ;}

Ultraviolet A 10306 e-coins (full backpack: Two-Dimensional restrictions)

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.