HDU 2126 backpack Problems

Source: Internet
Author: User

/* There are n items, and passengers have m oceans. First, how many items can passengers buy at most? Please note that the number of items here is not the most valuable. Therefore, it is very easy to sort all the items by value, first buy cheap, then buy expensive. Greedy thoughts. Some details need to be dealt with in this place. If the total value of all items is less than that of passengers, there is only one solution. Passengers can buy all items. If the amount of money a passenger can't afford to buy the first item, he will output "sorry, you
Can't buy anything. ".

In this way, we can find the maximum number of items a passenger can buy. After finding the maximum number of items, the price of the items has two attributes: the amount of money and the number of items. That is, the cost required to buy an item is the amount of money it costs and the share of one item. In the paper about backpack 9, this is a two-dimensional backpack problem.

If we are looking for an optimal solution, there is still no pressure on this problem. However, the problem is to find the total number of solutions.

We use DP [J] [k] to represent the number of solutions for buying K items in J yuan, in fact, we can easily obtain DP [J] [k] = DP [J] [k] + dp [J-A [I] [k-1]. There are two things to explain about this equation. The first one is the space optimized equation. For the principle of optimization, refer to Chapter 9 about backpack. Lecture 1: 01 about backpack. I will not describe it here. What we need to know is that DP [J] [k] here refers to DP [I-1] [J] [K], that is, the number of solutions for DP [J] [k] After considering the I-1 item. This explains why this DP [J] [k] can be inherited directly. The second one needs to be explained, is there any same solution in DP [J] [k] and DP [J-A [I] [k-1, is there any risk of repeated computing to add the two together. The answer is no. Because I items are included in DP [J-A [I] [k-1, we have said that DP [J] [k] is actually a DP [I-1] [J] [K], where there is no shadow of item I, so the two cannot have a repeated solution. */

# Include <stdio. h>
# Include <string. h>
# Include <stdlib. h>
Int CMP (const void * a, const void * B ){
Return * (int *) A-* (int *) B;
}
Int DP [600] [40];
Int A [40];
Int main (){
Int t, n, m, I, j, P, kind;
Scanf ("% d", & T );
While (t --){
Memset (DP, 0, sizeof (DP ));
Scanf ("% d", & N, & M );
For (I = 0; I <n; I ++)
Scanf ("% d", & A [I]);
Qsort (A, N, sizeof (A [0]), CMP );
P = m;
For (I = 0; I <n; I ++ ){
If (p-A [I]> = 0)
P-= A [I];
Else
Break;
}
Kind = I;
If (kind = 0 ){
Printf ("sorry, you can't buy anything. \ n ");
Continue;
}
For (I = 0; I <= m; I ++)
DP [I] [0] = 1;
For (I = 0; I <n; I ++)
For (j = m; j> = A [I]; j --)
For (int K = kind; k> = 1; k --)
DP [J] [k] = DP [J] [k] + dp [J-A [I] [k-1]; // DP [J-A [I] [k-1] contains item I, DP [J] [k] is equivalent to DP [I-1] [J [K]. Number of solutions selected for the I-th item each time
Printf ("You have % d selection (s) to buy with % d kind (s) of souvenirs. \ n", DP [m] [kind], kind );
}
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.