[Hoj] 1485 a good helper [backpack problem]

Source: Internet
Author: User
# Include <cstdio> # include <cstring> int f [1001]; int max (int A, int B) {return (A> B? A: B);} void swap (Int & A, Int & B) // The original swap function is suspected to be incorrect. Here {int c = A; A = B; B = C;} int main () {int I, j, m, n, x, V [1001], big = 1, total, TMP; while (scanf ("% d", & M, & N )! = EOF) // two limit {memset (F, 0, sizeof (f); scanf ("% d", & X); // Total number of packages = 0; // total weight for (I = 1; I <= x; I ++) {scanf ("% d", & V [I]); if (V [I]> V [Big]) Big = I; // index mark Max total + = V [I];} swap (V [Big], V [x]); for (I = 1; I <= x; I ++) {TMP = f [m]; // TMP is used to save f [I-1, j] For (j = m; j> = V [I]; j --) // The Descending Dimension is still processed. f [J] = max (F [J-V [I] + V [I], F [J]);} // The second person does not need DP. You can directly check the sum to determine whether the removal can be performed. // you only need DP to find the maximum weight that the first person can remove, A backpack may not be filled with space. // F [I] indicates the maximum total size of a backpack with the size of I. // The value and capacity are the same, of course, you need to consider situations where the package cannot be installed. // optimize the two dimensions into one dimension, the enumeration of X objects is hidden. // Therefore, if (total-f [m]) <= N) printf ("Yes \ n") is traversed in reverse order "); // two people can remove all else if (total-V [x]-TMP) <= N) printf ("need a helper \ n "); // except for the heaviest else printf ("NO \ n");} return 0 ;}

This is still a DP, which is a simple backpack.

Because the fork assistant can move anything of weight, this strong person should consider it specially. Find the maximum weight at the end of the first time. Then he made his first backpack for 01, and then he tried to move as many items as possible, and then determined whether the remaining items could allow the second person to move. If not, determine whether the last (I .e. the largest weight) thing can be moved by the last two people.

F [I, j] indicates not to remove the maximum weight of the last one, then f [I-1, J] indicates to remove the last one.

Http://blog.sina.com.cn/s/blog_6d6add550100ypqp.html

The source of the article. The content has been corrected.

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.