HDU 3033 I love sneakers! I love sports shoes (grouping backpack, 01 backpack, severe deformation)

Source: Internet
Author: User

 

Q: K stores are provided. Each store has all kinds of price shoes (the same pair of shoes can only be bought). Each pair of shoes is valuable and requires each store to buy at least one pair. Give m money and seek the maximum value.

Idea: the grouping of backpacks is severely deformed, which turns to the opposite. Each group of items should have at least one (the grouping of backpacks can have at most one ). Although it is the deformation of the group backpack, it is the idea of 01 backpack. Each store must buy at least one pair. You can buy only one pair or multiple pairs. This is the difficulty. You only need to maintain the DP Status values of the two rows. The first row is the best status of the items in the previous group, and the second row is the best status before the first item (items already in the same group ).

For group I t items, (1) either from the status before group I (that is, until now, Group I only buys t items), (2) either before I purchased the T-pieces (including 1 ~ The status of the I-1 group) is transferred. However, this does not guarantee that some shoes in group I will be selected. The T-pieces may not be cost-effective and will not be loaded. At the same time, it is not cost-effective to install other parts in the same group. To solve this problem, first pre-install one of the cheapest ones, that is, the second row mentioned above is updated to "the cheapest shoes in group I are required ", this ensures at least one piece in each group. If the group I in the official answer should only have one, and it is not the cheapest, the transfer method (1) will certainly overwrite the pre-installed ones; if you need to install multiple parts in group I, the transfer method in group (2) can be considered from multiple parts in the same group.

It is vital that for each capacity J, if you cannot afford the cheapest shoes in all stores, it will be voided because it does not meet the requirements. For example, if you consider 2nd sets of 1st pieces, DP [1] [0 ~ The status of the cheapest shoes in the 1st group-1] is invalid because 0 ~ The cheapest shoes in the 1st group-1 status can't afford even the cheapest shoes in the 1st group. It does not meet the requirements. Even if the answers use these statuses, they do not meet the requirements, therefore, you only need to pay attention to the lower limit when considering the capacity.

Great challenge! Each store, but not necessarily have shoes, such as giving K store, K-1 pieces of shoes, this is possible. Once this happens, "impossible" is immediately output ".

1 # include <bits/stdc ++. h> 2 using namespace STD; 3 int n, m, K, W, DP [15] [1, 11000]; 4 struct node 5 {6 int P, V; 7} R; 8 vector <node> vect; 9 inline int CMP (node A, Node B) {return. P <B. p? 1: 0;} 10 int CAL () 11 {12 memset (DP, 0, sizeof (DP); 13 int up = 0; 14 for (INT I = 1; I <= K; I ++) // 15 {16 R = vect [I] [0] in each group; 17 for (Int J = m; j> = up + R. p; j --) // first install the cheapest one in each group, there is better then update 18 DP [I] [J] = DP [I-1] [j-r.p] + R. v; 19 20 for (int t = 1; t <vect [I]. size (); t ++) // 21 {22 r = vect [I] [T]; 23 for (Int J = m; j> = up + R. p; j --) // capacity. Note that the lower limit is the sum of the cheapest shoes in all the stores. The worst way is to buy the cheapest shoes from all the stores in front, and the others are invalid. 24 {25 DP [I] [J] = max (DP [I] [J], DP [I-1] [j-r.p] + R. V); // separately placed. 26 dp [I] [J] = max (DP [I] [J], DP [I] [j-r.p] + R. V); // fit with the same group. 27} 28} 29 up + = vect [I] [0]. p; // update lower limit 30} 31 if (DP [k] [m]> 0) return 1; 32 else return 0; 33} 34 void Init () 35 {36 vect. clear (); 37 vector <node> TMP; 38 for (INT I = 0; I <= K; I ++) vect. push_back (TMP); 39} 40 41 int main () 42 {43 freopen ("input.txt", "r", stdin ); 44 While (CIN> N> m> K) 45 {46 Init (); 47 for (INT I = 0; I <n; I ++) 48 {49 scanf ("% d", & W, & R. p, & R. v); 50 vect [w]. push_back (r); // save group 51} 52 int big = 0; 53 (INT I = 1; I <= K; I ++) 54 {55 sort (vect [I]. begin (), vect [I]. end (), CMP); // sort the data in order. The lowest price is 56 If (! Vect [I]. Empty () Big + = vect [I] [0]. P; // There are no shoes in some shops! 57 else Big = 0x7fffffff; // if there are no shoes, set it to infinity, which means you cannot afford it. 58} 59 If (big> m) {printf ("impossible \ n"); continue;} // the cheapest shoes in each store cannot afford 60 if (CAL ()) printf ("% d \ n", DP [k] [m]); 61 else printf ("impossible \ n"); 62} 63 return 0; 64}
AC code

 

HDU 3033 I love sneakers! I love sports shoes (grouping backpack, 01 backpack, severe deformation)

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.