Poj 1837 Balance (dp backpack)

Source: Internet
Author: User

 


Question:

There is a balance, the length of the left and right sides of the balance is 15, and there are some hooks on the arm. There are also G weights (1 <= G <= 20), they are different in weight, in 1 ~ Value 25.

Given C hooks, they are located in [-15 .. 15] and will not overlap. The negative signs represent the left arm, and the positive signs represent the right.

All weights must be placed on the hook. Multiple Weights can be attached to the same hook. How many different solutions can be used to balance the balance?

 

 

 

Ideas:

The torque on the left arm of the balance is a negative number, and the torque on the right is a positive number. The sum of the torque on the left and the right is a positive number, which indicates the right of the balance trend, and a negative number indicates the tendency on the left, when it is 0, the balance is balanced. We call the "Left torque and + right torque and" A balance coefficient.


Status f [I] [j] indicates the total number of solutions when the first I weight is used and the equilibrium coefficient is set to j.
Then, f [I] [j] + = f [I-1] [j-C [k] * G [I], {0 <= k = <c };


Because the equilibrium coefficient has a negative number, it is necessary to translate all the equilibrium coefficients to the right, that is, add a positive number that is large enough. The minimum negative sum of torque can be calculated. All weights are attached to the position of the balance-15. The maximum weight is 20. The maximum value is 6... 25, then the sum of weights is (6 + 25) * 20/2 = 310, and the sum of torque is-15*310 = 4650.
So add 4650. This is the center position of the original balance,
F [0] [4650] = 1 is initialized, indicating that a weight is not attached. This is a balanced solution.


In the end, f [G] [4650] is the answer.

 


PS: At the beginning, I used a rolling array to submit the questions with G ++ until the RE ends. Then, you can use C ++ to submit the application.

 


Code:


[Cpp] # include <iostream>
# Include <cstdio>
# Include <cmath>
# Include <algorithm>
# Include <cstring>
# Define SQ (x) * (x ))
# Define MP make_pair
Const int INF = 0x3f3f3f;
Const double PI = acos (-1.0 );
Typedef long int64;
Using namespace std;
 
Const int MAXN = 22;
Const int mid = 4650;
Int pos [MAXN], w [MAXN];
Int f [22] [mid * 2 + 10];
Int n, m;
 
Int main (){
While (~ Scanf ("% d", & n, & m )){
 
For (int I = 0; I <n; ++ I)
Scanf ("% d", & pos [I]);
 
For (int I = 0; I <m; ++ I)
Scanf ("% d", & w [I]);
 
Memset (f, 0, sizeof (f ));
F [0] [mid] = 1;
 
For (int I = 0; I <m; ++ I ){
For (int j = 0; j <n; ++ j ){
 
Int add = w [I] * pos [j];
For (int v = mid * 2; v-add> = 0; -- v ){
If (v-add <= mid * 2)
F [I + 1] [v] + = f [I] [v-add];
}
}
}
Printf ("% d \ n", f [m] [mid]);
}
Return 0;
}

# Include <iostream>
# Include <cstdio>
# Include <cmath>
# Include <algorithm>
# Include <cstring>
# Define SQ (x) * (x ))
# Define MP make_pair
Const int INF = 0x3f3f3f;
Const double PI = acos (-1.0 );
Typedef long int64;
Using namespace std;

Const int MAXN = 22;
Const int mid = 4650;
Int pos [MAXN], w [MAXN];
Int f [22] [mid * 2 + 10];
Int n, m;

Int main (){
While (~ Scanf ("% d", & n, & m )){

For (int I = 0; I <n; ++ I)
Scanf ("% d", & pos [I]);

For (int I = 0; I <m; ++ I)
Scanf ("% d", & w [I]);

Memset (f, 0, sizeof (f ));
F [0] [mid] = 1;

For (int I = 0; I <m; ++ I ){
For (int j = 0; j <n; ++ j ){

Int add = w [I] * pos [j];
For (int v = mid * 2; v-add> = 0; -- v ){
If (v-add <= mid * 2)
F [I + 1] [v] + = f [I] [v-add];
}
}
}
Printf ("% d \ n", f [m] [mid]);
}
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.