POJ1276-Cash Machine

Source: Internet
Author: User

 

Reprinted please indicate the source: Thank you

Http://user.qzone.qq.com/289065406/blog/1299341477

 

Tip: Dynamic Planning, multiple backpacks

 

Question:

There are various currencies with different denominations, each of which has different quantities of currencies. Please find the amount of cash that can be used to make up the nearest and less than or equal to the given number.

 

Initial thought:

For multiple knapsack problems, d [I] Has n [I] + 1 options.

Can be converted to 01 backpack problem handling

The general idea of conversion is to multiply each nominal value by a different number and use the different amount as a unique currency. However, there are two problems, one is that D [I] * ki may be equal to d [J] * KJ, where ki in N [I], kJ in N [J], second, it takes some time-out (previous experience O (lead _ timeout) O Haha ~ I will not repeat the same mistakes)

Solution:

First, solve the storage problem. The cash ceiling is too large.... We recommend using new. Remember Delete. Otherwise, even if the AC is down, memory will be very large =

Speaking of the space application, let's talk a little off the topic. This question is not as Bt as poj1837 (1837 of students will know... 1837 it is difficult to apply for a dynamic space, because the dynamic value of the space is determined by the maximum value of the three variables = There is a waste of time looking for the maximum value, rather than applying for a static space, it's only one million)

 

Let's get down to the truth. The solution below is best suited to the program I wrote. Otherwise it will be hard to understand, before reading this article, please make sure that you have some theoretical knowledge about 01 backpacks, full backpacks, and multiple backpacks... Otherwise it should be hard to understand...

 

This question contains four parts:

Cash // backpack capacity (max available amount)

N // item count (denomination)

N [I] The number of I items (the number of I items)

D [I] value of item I (value of item I)

 

It is worth noting that if the value of each item is set to W [I], the volume (which can be understood as the consumed value) is C [I]

Then d [I] = W [I] = C [I]

 

If you consider an amount as a state, a total of 0 ~ Cash status

Apparently, the possible status range isMin {W [I] | 1 <= I <= N [I]} ~ Cash

You can create a status array DP [cash + 1],

DP [J] records the status value of "closest to status J, and <= J", that isDP [J] <= J

The closer J is to DP [J], the better the solution of DP [J], the ideal is DP [J] = J

It should be noted that DP [J] can occur to indicate that status J does not care how DP [J] occurs.

For example, there are 3 RMB 1 and 1 RMB 3.

We can assume that DP [3] = 3 is obtained by obtaining 1 RMB for three times, or DP [3] is obtained by obtaining 3 RMB for one time, however, in any case, DP [3] = 3 will occur,

Note that the Status values of DP [J] will accumulate.

Example: for example, there is a 3 RMB image, Cash = 4

According to the preceding instructions, DP [3] = DP [4] = 3, which means that status 3 and status 4 can both be acquired once and trigger once, this status value 3 will be retained in the current status DP [4],This is actually an optimization function, which will be explained in detail later

In the preceding example, when we add a condition "3 RMB 1", under "DP [4] = 3, we only need to get 1 yuan once to get a better solution than DP [4] = 3 DP [4] = 4, however, we do not need to worry about how DP [4] = 3 came from,We only need to know That DP [4] = 3 will certainly appear.

 

Then let's talk about what we just mentioned."Optimization problem"

It is not hard to understand the knowledge of using the 01 backpackState Equation DP [J] = DP [J-C [I] + W [I]

As for what the equation means, I know what it means when I look at the 01 backpack. If I don't see it, don't blame me for not explaining it.

The optimization is because the status value is recorded, that is, before we get the next currency I, the current status isJ-C [I]

Once we select currency I, we will get the status (J-C [I]) + C [I], That is, status J. The status value DP [J] of status J is added with W [I]. As for the original value of DP [J], ignore it because the value of DP [J] is accumulated. After W [I] is added, as long as the DP [J] value is not optimal, it will also become the next cumulative value.

In this way, you can save a bunch of search time by directly calling the previously obtained status values. This is not done by DFS or BFS and is also an advantage of dynamic planning.

 

Next let's talk about the counter count [J]

In my program,The counter is cleared every time the denomination is changed.In this way, the value of the denomination I w [I] is used as the right, and the number of this denomination is selected, the status value DP [J] is divided by an integer multiple of W [I, in this way, DP [W [I] * k] to DP [W [I] * (k + 1)] the optimal state value between (0 <= k <= N [I]) and under the current denomination W [I.

For example, 3 RMB 3

Natural DP [0] = DP [1] = DP [2] = 0, Count [0] = count [1] = count [2] = 0 this is because the minimum denomination is 3

It is not hard to get DP [3] = DP [4] = DP [5] = 3, count [3] = count [4] = count [5] = 1 This is because the denomination is 3 RMB, and the optimal value of the three States is 3 RMB once.

DP [6] = DP [7] = DP [8] = 6, count [6] = count [7] = count [8] = 2 the optimal value of the three States is to take 2 times 3 yuan

DP [9] = DP [10] = DP [11] = DP […] = 9, Count [9] = count [10] = count [11] = count […] = 3 only 3 RMB 3 at most, and the optimal value for future states is 9

 

Finally, let's talk about the problem of using the memset function initialization.

The program also makes it very clear that because it is a dynamic space, do not use sizeof. Manually calculate the space size, remember + 1

I think this is a defect of the memset function... (Do not dare to call bug, Haro (empty _ Empty) O)

 

 

// Memory time // 1052 K 47 Ms # include <iostream> using namespace STD; int main (int I, Int J) {int N; // Number of item types (denomination) int cash; // backpack capacity (maximum available amount) while (CIN> cash> N) {/* input */int * n = new int [n + 1]; // n [I] the number of items under I (the number of items under I) int * w = new int [n + 1]; // W [I] the value of the I-th item (the value of the I-th denomination) int * c = new int [n + 1]; // C [I] the volume of the I-th item (the value consumed by the I-th denomination) int * dp = new int [cash + 1]; // DP [J] records the value closest to status J and <= J. The value of DP accumulates int * COUNT = new int [cash + 1]. // counter, which limits the selection of certain items (denominations) Count for (I = 1; I <= N; I ++) {CIN> N [I]> W [I]; c [I] = W [I]; // The "volume" of a single item in this question is equal to its "value"}/* Initial */memset (DP, 0, 4 * (cash + 1); // because DP applies for dynamic memory, an error occurs when calculating the length using sizeof. // The number of types must be used here, here it is int * (cash + 1), int size is 4/* DP */for (I = 1; I <= N; I ++) {memset (count, 0, 4 * (cash + 1); // the counter is cleared for (j = W [I]; j <= cash; j ++) // for currency I, the denomination d [I] ~ Any status between cash may occur if (DP [J] <DP [J-C [I] + W [I] & COUNT [J-C [I] <n [I]) // count [J-C [I] <n [I] {// before getting a certain denomination, the number of times this denomination was obtained before this operation must be less than N [I] DP [J] = DP [J-C [I] + W [I]; // After I is selected, the size of the backpack (maximum amount allowed) is reduced by C [I] Count [J] = count [J-C [I] + 1; // for the current status J, the I-level is counted [J] times}/* output */cout <DP [Cash] <Endl; /* release space */delete N; Delete W; Delete C; Delete DP; Delete count;} return 0 ;}

 

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.