Multiple HDU 2844 coins backpacks

Source: Internet
Author: User

This is my first thought of as a mother function, but it is said that when the mother function uses TLE, multiple backpacks are decisive. Here we use multiple backpacks, obviously we cannot directly output DP [N, in this case, the result is wrong. How can we find the type. If (DP [I]) ++ C ;?????? Can this be done ???? No, this is the meaning of DP [I]. DP [I] represents the greatest value that can be found in weight I, and here the weight and value are both money, so there is a very convenient place. If DP [I] is the most capable of loading I, then I will certainly be able to choose, therefore, the judgment condition is if (DP [I] = I) ++ C;

Run the Code directly.

# Include <stdio. h> # include <string. h> // # define max (a, B) (a)> (B )? (A): bint n, m, V [105], W [105], num [105], DP [100005]; inline int max (int A, int B) // remember to add inline, which is much faster {return A> B? A: B;} void _ 01 (INT V, int W) {for (Int J = m; j> = V; -- J) DP [J] = max (DP [J-v] + W, DP [J]);} void complete (int v, int W) {for (Int J = V; j <= m; ++ J) DP [J] = max (DP [J-v] + W, DP [J]);} void multiply (int v, int W, int c) {If (V * C> m) complete (V, W); else {int K = 1; while (C> K) {_ 01 (K * v, k * w); C-= K; k <= 1;} _ 01 (C * V, C * w );}} int main () {While (scanf ("% d", & N, & M), N | M) {for (INT I = 1; I <= N; ++ I) scanf ("% d", & V [I]); For (INT I = 1; I <= N; ++ I) scanf ("% d", & num [I]); memset (DP, 0, sizeof (DP); For (INT I = 1; I <= N; ++ I) Multiply (V [I], V [I], num [I]); int C = 0; For (INT I = 1; I <= m; ++ I) if (DP [I] = I) ++ C; printf ("% d \ n", c);} 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.