Poj 1837 balance

Source: Internet
Author: User

Http://poj.org/problem? Id = 1837

A balance can be regarded as an X axis. The negative half axis is the left side of the balance, and the right half axis is the right side of the balance,

Now there are c hooks on the balance, so you can attach g weights and ask several ways to balance the balance,

2 <= C <= 20; 2 <= G <= 20;

Hook coordinates [-15, 15]; weight: [];

Idea: imagine that the balance of each attached weight balance is affected by the position of the previous weight, so dynamic planning can be considered.

Can we record the degree of balance J achieved by hanging the first weight in different positions?

After the first weight has been attached to reach the equilibrium degree J, the second weight has reached the equilibrium degree J.

Therefore, DP [I] [k] is used to indicate the number of methods used to reach the equilibrium degree J after I weight is attached (when the equilibrium degree J is 0, it indicates the balance of the balance)

According to the question of balance degree J. When the maximum is, all weights are attached to the outermost hook on the side of the balance 15*20*25 = 7500

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

However, the subscript cannot be negative. Therefore, process DP [1 ~ 20] [0 ~ 15000] after processing, if J is 7500, it indicates a balance.

"

Apparently DP [0] [7500] = 1 is known

Therefore, the value of DP [g] [7500] is the answer.

 

DP [I] [K + W [I] * l [J] + = DP [I-1] [k]

W [I] indicates the weight of the I weight W [I]

L [J] indicates the position of the I-th weight. L [J]

 

1 # include <algorithm> 2 # include <iostream> 3 # include <sstream> 4 # include <cstdlib> 5 # include <cstring> 6 # include <cstdio> 7 # include <string> 8 # include <bitset> 9 # include <vector> 10 # include <queue> 11 # include <stack> 12 # include <cmath> 13 # include <list> 14 # include <map> 15 # include <set> 16 using namespace STD; 17/* 10 ^ 8 ----- 1 S */18 /*************************** * ***********/19 typedef Vector <int> VI; 20 typedef vector <char> VC; 21 typedef vector <string> vs; 22 typedef set <int> Si; 23 typedef set <string> SS; 24 typedef Map <int, int> MII; 25 typedef Map <string, int> MSI; 26 typedef pair <int, int> PII; 27 typedef vector <PII> VII; 28 typedef vector <VI> VVI; 29 /************************************** */30 # define min (, b) (A> B? B: A) 31 # define max (A, B) (A> B? A: B) 32 33 # define CLR (a, B) memset (a, B, sizeof (a) 34 # define all (x ). begin (), (x ). end () 35 # define SZ (x) (INT) (x ). size () 36 # define ll long 37 # define int64 _ int64 38 # define Pb push_back 39 # define MP make_pair 40 # define LL (x) (X) <1) 41 # define RR (x) <1 | 1) 42 # define RI (x) scanf ("% d", & X) 43 # define RII (x, y) scanf ("% d", & X, & Y) 44 # define RD (x) scanf ("% lf ", & X) 45 # define RDD (x, y) Scanf ("% lf", & X, & Y) 46 # define RS (x) scanf ("% s", x) 47 # define Pi (X) printf ("% d", x) 48 # define pin (x) printf ("% d \ n", x) 49 # define PS (X) printf ("% s", x) 50 # define Pn () printf ("\ n") 51 # define sqr (x) * (x )) 52 # define rep (I, a, B) for (INT I = (a); I <(B); I ++) 53 # define Repu (I,, b) For (INT I = (a); I <= (B); I ++) 54 # define repd (I, a, B) for (INT I = (a); I >= (B); I --) 55 # define repc (I, a, c) for (INT I = (); (c); I ++) 56 /************************************** */57 const int INF = 0x7f7f7f7f; 58 const double EPS = 1e-8; 59 const double PIE = ACOs (-1.0); 60 const int DX [] = {0,-, 1 }; 61 const int dy [] = {,-}; 62 const int FX [] = {-1,-1 }; 63 const int FY [] = {-, 1,-,-, 1 }; 64 /************************************** */65 void openfile () 66 {67 freopen ("data. in "," rb ", stdin); 68 freopen (" Data. Out "," WB ", stdout ); 69} 70/********************* The End Of The template ********* * ******/71 72/* 73 74 75 */76 77 int W [30]; // W [I] indicates the weight of the I-th weight W [I]; I = [2 ~ 20] W [I] = [1 ~ 25] 78 int L [21]; // L [J] indicates the position of the I-th weight l [J]; L [J] = [-15 ~ 15] J = [2 ~ 20] 79 int DP [30] [15002]; // DP [I] [k] indicates that I weight is attached, k = [-15*25*20, 15*25*20] 80 // initial DP [0] [7500] = 1 81 int main () 82 {83 int C, G; 84 int I, j, k; 85 scanf ("% d", & C, & G); 86 for (I = 0; I <C; I ++) 87 {88 scanf ("% d", & L [I]); 89} 90 for (I = 0; I <G; I ++) 91 {92 scanf ("% d", & W [I]); 93} 94 memset (DP, 0, sizeof (DP )); 95 DP [0] [0 + 7500] = 1; // The balance is reached when the balance is not linked to the balance. The method is 96 for (I = 1; I <= g; I ++) // Number of weights on the Balance 97 for (k = 0; k <= 15000; k ++) // equilibrium degree K 98 {99 If (DP [I-1] [k]) 100 for (j = 0; j <C; j ++) 101 DP [I] [K + W [I-1] * l [J] + = DP [I-1] [k]; 102} 103 printf ("% d \ n ", DP [g] [7500]); 104 return 0; 105}
View code

 

Poj 1837 balance

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.