K - Balance(动态规划专项)
Description
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 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
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(动态规划专项)的更多相关文章
- A - Space Elevator(动态规划专项)
A - Space Elevator Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u ...
- poj 1837 Balance 动态规划 (经典好题,很锻炼思维)
题目大意:给你一个天平,并给出m个刻度,n个砝码,刻度的绝对值代表距离平衡点的位置,并给出每个砝码的重量.达到平衡状态的方法有几种. 题目思路:首先我们先要明确dp数组的作用,dp[i][j]中,i为 ...
- 陕西师范第七届K题----动态规划
ps: 自己的方法绝对是弱爆了 肯定存在更优的方法 O(n^3)复杂度 暴力求解的.. 链接:https://www.nowcoder.com/acm/contest/121/K来源:牛客网 柯怡最近 ...
- G - Zombie’s Treasure Chest(动态规划专项)
G - Zombie’s Treasure Chest Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d &am ...
- 【动态规划】拦截导弹_dilworth定理_最长递增子序列
问题 K: [动态规划]拦截导弹 时间限制: 1 Sec 内存限制: 256 MB提交: 39 解决: 10[提交][状态][讨论版] 题目描述 张琪曼:“老师,修罗场是什么?” 墨老师:“修罗是 ...
- 设计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 ...
- 动态规划:POJ2576-Tug of War(二维费用的背包问题)
Tug of War Time Limit: 3000MS Memory Limit: 65536K Description A tug of war is to be arranged at the ...
- 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的数的个数. 可 ...
- poj1837挂砝码
解法(背包DP问题) (下为转) 其实感觉 像此题这种类型的并不属于dp范畴 虽然程序看起来使用的是递推这一过程,但总不能说开个二重循环就是dp吧 如果只从求解上来讲(不考虑数据值的范围), 只有枚举 ...
随机推荐
- ES 6 : Math对象的扩展
ES6在Math对象上新增了17个与数学相关的方法.所有这些方法都是静态方法,只能在Math对象上调用. 1.Math.trunc() Math.trunc方法用于去除一个数的小数部分,返回整数部分. ...
- dplyr 数据操作 常用函数(1)
上面介绍完dplyr中,几个主要的操作函数后,我们再进一步了解dplyr中那些函数可能我们会经常要用到. 这里主要根据dplyr包作者的书籍目录来把它列出来. 1.add_rownames 添加行名称 ...
- 所需的防伪表单字段“__RequestVerificationToken”不存在
错误提示为:所需的防伪表单字段"__RequestVerificationToken"不存在. <%:Html.AntiForgeryToken()%> 在mvc4中的 ...
- 可用fidder测试的一些安全测试点
以下是整理的一些常见的安全渗透测试点 1.用工具fidder抓包拦截篡改服务器端返回的代码,导致下级拥有对上级的访问操作权限 以下是公司开发写的用户角色权限页面跳转 修改普通角色跳转的页面为管理员跳转 ...
- 谈谈ThreadStatic
可能经常做多线程.线程池的童鞋早就知道这种问题,原谅我一直对线程研究不深. 这个东西好像出现有一段时间了,不过最近我才用到,做的API的服务,用来保存当前请求的上下文内容,原来用过Thread.Set ...
- 深入理解java回调机制
Callback的定义 一般在程序中执行回调函数是,是知道回调函数是预留给系统调用的,而且知道该函数的调用时机. 比如说android应用定义一个button对象,并给按钮添加一个监听事件," ...
- mysql bit类型数据库中无法显示
表an_bit bit类型字段id select bin(id+0) from an_bit显示二进制 select id+0 from an_bit显示十进制 http://blog.csdn.ne ...
- [译][待续]Chap1.Using neural nets to recognize handwritten digits
Chapter1 使用神经网络辨识手写数字 人类的视觉系统是自然界的一大奇迹.试看如下的手写数列: 绝大多数人都能毫不费劲地认出这些数字是504192,而这会让人产生识别数字非常简单的错觉.人类大脑的 ...
- URL中文参数乱码的一个解决办法
浏览器对有中文参数的url大部分都用utf-8编码,但我也曾经遇见过用GB2312编码的:如果遇见这样情况,那么接受到的参数就会出现乱码.乱码情况视服务器解码方式,Asp.Net网站一般默认为ut ...
- php 分析
php code in D:\10\11\php test in D:\10\11\php\test issue 1: <html><head><title>标记 ...