Algorithm: POJ 1837 Balance (DP 01 backpack)

Source: Internet
Author: User

The main effect of the topic:

There is a balance, the balance on both sides of the arm length is 15, the arm above some positions will have hooks. There are g weights (1 <= G <= 20), each with a different weight and a value in 1~25.

C hooks are given, their positions are " -15..15" and do not overlap. The minus is represented on the left arm, plus the positive on the right.

All weights are required to be placed on the hook, multiple weights can be hung on the same hook, ask how many different schemes to balance the scales?

Ideas:

Balance left arm torque and is negative, the right moment and is positive, then left + right moment of the sum, if it is positive, the balance of the representative balance tendency to the right, negative representatives tend to the left, 0 when the balance is balanced. We call the "left moment and the right moment and" the equilibrium factor.

The state F[i][j] represents the number of options for using the first I weights and placing the equilibrium coefficient at J. So, f[i][j] + = F[i-1][j-c[k]*g[i], {0<=k=<c};

Because there are negative values in the equilibrium coefficients, all the equilibrium coefficients are translated to the right, plus a positive enough number. Can calculate the moment and the smallest negative number is put all weights in the balance-15 position, weights up to 20, the maximum value of the case is 6 ... 25, then the sum of weights and finally for (6+25) *20/2 = 310, torque and the sum of -15*310 = 4650 so plus 4650 can, this is position 4650 represents the original balance of the middle position, initialization f[0][4650] = 1, means that a weight is not Hang on, this is a balanced solution.

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

PS: This question at the beginning I was done with a scrolling array, with g++ submitted to death, depressed. Later, you can use C + + to submit the AC.

Code:

 #include <iostream> #include <cstdio> #include <cmath> #include <algorithm> #include <  
Cstring> #define SQ (x) ((x) * (x)) #define MP Make_pair const int INF = 0X3F3F3F3F;  
Const double PI = ACOs (-1.0);  
typedef long 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%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; }

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

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.