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. AndroidPullToRefresh拉动效果配置

    最近用了 开源的 AndroidPullToRefresh 库,但是发现拉动时的效果有个很奇怪的地方,无论上下拉动,当列表滚动到顶部或底部时,会瞬间弹出半个列表高度的拉动提示,感觉很不舒服,这种提示根 ...

  2. 使用 Git 报错 error: src refspec master matches more than one.

    今天在使用 Git push 代码时遇到一个报错: error: src refspec master matches more than one. error: failed to push som ...

  3. 为何没有.aspx.designer.cs文件?

    designer.cs 是窗体设计器生成的代码文件,作用是对窗体上的控件做初始化工作(在函数InitializeComponent()中)VS2003以前都把这部分代码放到窗体的cs文件中,由于这部分 ...

  4. 虚拟机在 OpenStack 里没有共享存储条件下的在线迁移[转]

    原文链接:http://www.ibm.com/developerworks/cn/cloud/library/1508_wangyx_openstacklivemigrate/ 迁移(Migrati ...

  5. 【IE6的疯狂之四】IE6文字溢出BUG

    在IE6下使用浮动可能会出现文字重复的情况. 在IE6下,浮动层之间有注释文字的话,之前那个浮动层的内容文字就有可能遭遇一个“隐形”的复制,但是代码里查看文字可并没有多出来. 看个例子: XML/HT ...

  6. Linux GIT服务器配置

    Linux下安装git插件 1. 下载git网址:     https://github.com/git/git/releases 2. 放入usr/src/下 3. tar -zxvf git-** ...

  7. Javascript中valueOf与toString区别

    前言 基本上,所有JS数据类型都拥有这两个方法,null除外.它们俩解决JavaScript值运算与显示的问题,重写会加大它们调用的优化. 测试分析 先看一例:var aaa = {  i: 10,  ...

  8. java 基础知识2

  9. 关于PC端与手机端随着手指移动图片位置放生变化的拖拽事件

    当按下鼠标时,图片随鼠标移动松开时图片回到原位 drag("div_id") function drag(node_id){ var node = document.getElem ...

  10. 编写简单的爬虫从流行的Scrapy 框架讲起

    到目前为止,我们已经完成了向站点添加搜索和过滤的功能,并且我们已经可以向站点添加一些分类和产品信息.下面我们将考虑当尝试删除实体信息时会发生什么事情. 首先,向站点添加一个名为Test的新分类,然后再 ...