Balance
Time Limit: 1000MS |
|
Memory Limit: 30000K |
Total Submissions: 11878 |
|
Accepted: 7417 |
Description
Gigel has a strange "balance" and he wants to poise it. Actually, the device is different from any and ordinary balance.
It orders arms of negligible weight and each arm's length is 15. Some Hooks is attached to these arms and Gigel wants to hang up Some weights from his collection of G weights (1 <= g <=) Knowing that these weights has distinct values in the range 1..25. Gigel may droop any weight of any hooks but he's forced to use all the weights.
Finally, Gigel managed to balance the device using the experience he gained at the National Olympiad in Informatics. Now he would like to know in how many ways the device can be balanced.
Knowing the repartition of the hooks and the set of the weights write a program that calculates the number of Possibilitie s to balance the device.
It is guaranteed, that would exist at least one solution for each test case at the evaluation.
Input
The input has the following structure:
? The first line contains the number C (2 <= C <=) and the number G (2 <= G <= 20);
? The next line contains C integer numbers (these numbers is also distinct and sorted in ascending order) in the range-15. . Representing the repartition of the hooks; Each number represents the position relative to the center of the balance in the X axis (when no weights is attached the Device is balanced and lined up to the X axis; The absolute value of the distances represents the distance between the hook and the balance center and the sign of the Nu Mbers determines the arm of the the balance to which the hook was attached: '-' for the left arm and ' + ' for the right arm);
? On the next line there is G natural, distinct and sorted in ascending order numbers in the range 1..25 representing the W Eights ' values.
Output
The output contains the number M representing the number of possibilities to poise the balance.
Sample Input
2 4-2 3 3 4 5 8
Sample Output
2
Test instructions is to give you a scale, and then give you a few points of position used to put steelyard weight scales, minus sign on the left side of the scale, just represent on the right side of the scale.
Then gave a few steelyard weight scales, gave the weight of steelyard weight scales. The question asks how many kinds of balance schemes are used with these steelyard weight scales.
After reading the question did not feel this is a DP problem ... Later saw more than found that should see such a small number to think of the enumeration state, so gradually pushed down the DP, at least such a problem is also a lot of, what 1 2 3 4 5 6 stone, are this kind of thought.
It is also used dp[i][j] to indicate that the status of placing the I-Steelyard weight scales Scale is a J-time scheme.
The state of the scale is calculated by calculating the 15*25*20, which can reach 7500 farthest. The left and right are, that should be-7500 to 7500. But because the array subscript must be greater than or equal to zero, it is represented by 0~15000.
Dp[0][7500]=1 means that when a steelyard weight scales is not in place, the state is in equilibrium and there is only one balancing scheme.
And then there's ... (I would actually prefer to say it as an enumeration ...) ) Push down.
Core code:
for (i=1;i<=g;i++) {for (j=0;j<=15000;j++) {for (k=1;k<=c;k++) {dp[i][j] + = Dp[i-1][j-weight[i]*weizhi[k]];}}}
That is, for each given steelyard weight scales constantly enumerate its original state, constantly enumerate its location, resulting in this can reach the state.
Code:
#include <iostream> #include <algorithm> #include <cmath> #include <vector> #include <string > #include <cstring> #pragma warning (disable:4996) using namespace std;//the largest state is that all weights are placed at the top of the terminal//i.e. 20*25*15= 7500 i.e. -7500~7500int dp[21][15001];int weizhi[21];int weight[21];int Main () {memset (DP);DP [dp,0,sizeof; int c,g,i,j,k;cin>>c>>g;for (i=1;i<=c;i++) cin>>weizhi[i];for (i=1;i<=g;i++) cin>> Weight[i];for (i=1;i<=g;i++) {for (j=0;j<=15000;j++) {for (k=1;k<=c;k++) {dp[i][j] + = dp[i-1][j-weight[i]* Weizhi[k]];}} Cout<<dp[g][7500]<<endl;return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
POJ 1837:balance Balance DP ...