K - Balance

Time Limit:1000MS     Memory Limit:30000KB     64bit IO Format:%I64d & %I64u

Submit Status

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

题意:一个天平,天平左右两边各有若干个钩子,总共有C个钩子,有G个钩码,求将钩码全部挂到钩子上使天平平衡的方法的总数。其中可以把天枰看做一个以x轴0点作为平衡点的横轴

思路:钩码和挂钩式各不相同的,所以每一个钩码挂在不同的的挂钩上也唯一对应一个值,我们称之为平衡值,开始时没有挂钩是平衡的然后开始挂钩码,平衡值开始变化每次找到上一个钩码挂上后能够达到的平衡值进行平衡值变化操作。dp[i][j] 表示在挂满前i个物体的时,平衡度为j的挂法的数量。j为正表示右面重。最极端的情况是所有物体都挂在最远端,因此平衡度最大值为15*20*25=7500。原则上就应该有dp[ 0..20 ][-7500 .. 7500 ]。因此做一个处理,使得数组开为 dp[0.. 20][0..15000]。然后利用滚动数组进行优化;

AC代码:

未用滚动数组:

 #include<iostream>
#include<cstdio>
#include<cstring> using namespace std;
int dp[][*+]={};
int sgin[]={};
int gou[]={}; int main()
{
// freopen("1.txt","r",stdin);
int c,g;
cin>>c>>g;
int i;
for(i=;i<=c;i++)
cin>>gou[i];
int a;
int s=;
cin>>a;
for(i=;i<=c;i++){
dp[s][gou[i]*a+]=;
}
s++;
while(s<=g){
cin>>a;
for(i=;i<=c;i++){
sgin[i]=gou[i]*a;
}
for(i=;i<=*+;i++){
if(dp[s-][i]){
for(int j=;j<=c;j++){
dp[s][sgin[j]+i]+=dp[s-][i];
}
}
}
s++;
}
cout<<dp[g][]<<endl;
return ;
}

使用滚动数组优化:

 #include<iostream>
#include<cstdio>
#include<cstring> using namespace std;
int dp[][*+]={};
int sgin[]={};
int gou[]={}; int main()
{
// freopen("1.txt","r",stdin);
int c,g;
cin>>c>>g;
int i;
for(i=;i<=c;i++)
cin>>gou[i];
int a;
cin>>a;
for(i=;i<=c;i++){
dp[g%][gou[i]*a+]=;
}
g--;
while(g){
cin>>a;
for(i=;i<=c;i++){
sgin[i]=gou[i]*a;
}
memset(dp[g%],,sizeof(dp[g%]));
for(i=;i<=*+;i++){
if(dp[-g%][i]){
for(int j=;j<=c;j++){
dp[g%][sgin[j]+i]+=dp[-g%][i];
}
}
}
g--;
}
cout<<dp[][]<<endl;
return ;
}


K - Balance(动态规划专项)的更多相关文章

  1. A - Space Elevator(动态规划专项)

    A - Space Elevator Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u ...

  2. poj 1837 Balance 动态规划 (经典好题,很锻炼思维)

    题目大意:给你一个天平,并给出m个刻度,n个砝码,刻度的绝对值代表距离平衡点的位置,并给出每个砝码的重量.达到平衡状态的方法有几种. 题目思路:首先我们先要明确dp数组的作用,dp[i][j]中,i为 ...

  3. 陕西师范第七届K题----动态规划

    ps: 自己的方法绝对是弱爆了 肯定存在更优的方法 O(n^3)复杂度 暴力求解的.. 链接:https://www.nowcoder.com/acm/contest/121/K来源:牛客网 柯怡最近 ...

  4. G - Zombie’s Treasure Chest(动态规划专项)

    G - Zombie’s Treasure Chest Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d &am ...

  5. 【动态规划】拦截导弹_dilworth定理_最长递增子序列

    问题 K: [动态规划]拦截导弹 时间限制: 1 Sec  内存限制: 256 MB提交: 39  解决: 10[提交][状态][讨论版] 题目描述 张琪曼:“老师,修罗场是什么?” 墨老师:“修罗是 ...

  6. 设计Account 对象如下:  private long id;       private double balance;       private String password; 要求完善设计,使得该Account 对象能够自动分配id。 给定一个List 如下: &#

    package homework005; public class Account { private long id; private double balance; private String ...

  7. 动态规划:POJ2576-Tug of War(二维费用的背包问题)

    Tug of War Time Limit: 3000MS Memory Limit: 65536K Description A tug of war is to be arranged at the ...

  8. Codeforces Round #287 D.The Maths Lecture

    The Maths Lecture 题意:求存在后缀Si mod k =0,的n位数的数目.(n <=1000,k<=100); 用f[i][j]代表 长为i位,模k等于j的数的个数. 可 ...

  9. poj1837挂砝码

    解法(背包DP问题) (下为转) 其实感觉 像此题这种类型的并不属于dp范畴 虽然程序看起来使用的是递推这一过程,但总不能说开个二重循环就是dp吧 如果只从求解上来讲(不考虑数据值的范围), 只有枚举 ...

随机推荐

  1. linux 安装php的redis拓展

    安装步骤: #wget  https://github.com/nicolasff/phpredis/archive/2.2.4.tar.gz #tar -zxvf 2.2.4 #cd phpredi ...

  2. Dom编程(一)

    dom:document object model 文档对象模型 1.事件 body 事件:onload onunload onbeforeunload body.document对象的事件 (1)o ...

  3. 淘宝npm镜像使用方法

    镜像使用方法(三种办法任意一种都能解决问题,建议使用第三种,将配置写死,下次用的时候配置还在): 通过config命令npm config set registry https://registry. ...

  4. H5移动端页面设计心得分享(转载)

    去年JDC出了不少优秀的武媚娘…不,H5呢,大家都很拼,同时当然也积累了一些经验和教训,今天结合咱们的实战案例,从字体,排版,动效,音效,适配性,想法这几个方面好好聊一聊关于H5的设计,希望对同学们有 ...

  5. C++友元

    通过friend关键字,我们可以将不属于当前类的一个函数在当前类中加以声明,该函数便可以成为当前类的友元函数. 例1: #include<iostream>using namespace ...

  6. js获取计算的样式(非行间样式)

    function getStyle(obj, attr){ if(obj.currentStyle){ style = obj.currentStyle[attr]; //兼容IE8以下 }else{ ...

  7. Knockout js 绑定 radio 时,当绑定整形的时候,绑定不生效

    解决方案: 使用checkedValue和checked 组合,如下代码. <div><input type="radio" name="flavorG ...

  8. 学习笔记(C++Primer)--易错点总结(Chapter2)

    2.1.2Type Conversions(1/10/2017) 1.If we assign an out-of-range value to an object of unsigned type, ...

  9. super在构造函数中的运用

    package example;//在子类构造对象时,访问子类构造函数时候,父类也运行.//原因是:在子类的构造函数中第一行有一个默认的隐式语句super();/* 构造函数不能覆盖 子类的实例化过程 ...

  10. 对“针对接口编程,而不是针对实现编程”的理解

    今天在阅读<Head First设计模式>的时候,看到了这句话:"针对接口编程,而不是针对实现编程",第一次见到的时候,不太清楚作者想表达的意思,想着到后来看看实例就懂 ...