Poj 1837 balance (DP, 01 backpack)

Source: Internet
Author: User

Link: poj 1837

Question:There is a balance, there are several hooks on both sides of the balance, a total of C hooks, G hook code,

Evaluate the total number of methods that hook the hook to balance the balance. In this example, we can regard the day sequence as a horizontal axis with the X axis 0 as the equilibrium point.

Analysis:Arm = weight * arm length = G [I] * C [J]

When the degree of balance K is 0, it means that the balance will be reached tomorrow, K is> 0, it means that tomorrow's balance tends to the right side (the right half axis of the X axis), and K is less than 0, it is left

Therefore, you can define a status array DP [I] [K], which indicates the number of hooks with a degree of balance of K when the first I hooks are full.

Since the distance from C [I] is-15 ~ 15. The hook weight ranges from 1 ~ 25. The maximum number of hooks is 20.

Therefore, the most extreme degree of balance is that all objects are mounted at the far end. ThereforeThe maximum degree of balance is J = 15*20*25 = 7500..

In principle, DP [1 ~ 20] [-7500 ~ 7500].

Therefore, in order not to let negative numbers appear in the subscript, make a processing to enable the array to be DP [1 ~ 20] [0 ~ 15000], then when J = 7500, the daily timeout is in the balance status.

The state equation is DP [I] [k] + = DP [I-1] [k-G [I] * C [J];

 
# Include <stdio. h> # include <string. h> int DP [21] [1, 15010]; int main () {int m, n, I, J, K, C [21], G [21]; scanf ("% d", & M, & N); for (I = 1; I <= m; I ++) scanf ("% d ", & C [I]); for (I = 1; I <= N; I ++) scanf ("% d", & G [I]); memset (DP, 0, sizeof (DP); DP [0] [7500] = 1; for (I = 1; I <= N; I ++) for (j = 1; j <= m; j ++) for (k = G [I] * C [J]; k <= 15000; k ++) DP [I] [k] + = DP [I-1] [k-G [I] * C [J]; printf ("% d \ n ", DP [N] [1, 7500]); 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.