首页 > 代码库 > poj 1837 Balance
poj 1837 Balance
Balance
Description Gigel has a strange "balance" and he wants to poise it. Actually, the device is different from any other ordinary balance. It orders two arms of negligible weight and each arm‘s length is 15. Some hooks are attached to these arms and Gigel wants to hang up some weights from his collection of G weights (1 <= G <= 20) knowing that these weights have distinct values in the range 1..25. Gigel may droop any weight of any hook but he is 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 possibilities to balance the device. It is guaranteed that will 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 <= 20) and the number G (2 <= G <= 20); ? the next line contains C integer numbers (these numbers are also distinct and sorted in ascending order) in the range -15..15 representing the repartition of the hooks; each number represents the position relative to the center of the balance on the X axis (when no weights are 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 numbers determines the arm of the balance to which the hook is attached: ‘-‘ for the left arm and ‘+‘ for the right arm); ? on the next line there are G natural, distinct and sorted in ascending order numbers in the range 1..25 representing the weights‘ 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 Source Romania OI 2002 真心觉得自己弱爆了,原本很简单的一道DP自己看了3个小时,才有自己的一些算法,可能是我第一次做这个DP的题吧。总之就一个字弱。然后小优的 解题报告真心不错上面解释的很详细。我只是根据他的状态方程,把程序写出来而已。题意的话你们看他的吧小优博客。我就附上我看了DP方程后的代码吧,希望不要喷我,我很水 /*#include<iostream> #include<cstring> using namespace std; int dp[21][15001];//挂的方法数 int main() { int i,j,k; int n;//n为沟的个数 int g;// 为挂的砝码数; int c[21];//为他的距离 int w[21];//为重量; cin>>n>>g; for(i=1;i<=n;i++) cin>>c[i];//输入沟的距离 for(i=1;i<=g;i++) cin>>w[i];//输入第i个的重量 memset(dp,0,sizeof(dp)); dp[0][7500]=1; for(i=1;i<=g;i++) for(j=0;j<=15000;j++) if(dp[i-1][j]) for(k=1;k<=n;k++) dp[i][j+w[i]*c[k]]+=dp[i-1][j]; cout<<dp[g][7500]<<endl; return 0; } */ #include<iostream> #include<cstring> using namespace std; int dp[21][150001];//这个也是小优给的 int main(){ int i,j,k; int c[21];//距离 int w[21];//每个砝码的重量 int n;//钩子的数量 int p;//砝码的数量 cin>>n>>p; for(i=1;i<=n;i++) cin>>c[i]; for(j=1;j<=p;j++) cin>>w[j]; memset(dp,0,sizeof(dp)); dp[0][7500]=1; for(k=1;k<=p;k++) for(j=0;j<=15000;j++) if(dp[k-1][j]) for(i=1;i<=n;i++) dp[k][c[i]*w[k]+j]+=dp[k-1][j]; cout<<dp[p][7500]<<endl; return 0; }不解释啊,感觉动态规划博大精深 |
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。