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吧 如果只从求解上来讲(不考虑数据值的范围), 只有枚举 ...
随机推荐
- Linux软件安装管理 - CentOS (二)
1. 软件包管理简介 2. rpm命令管理(Redhat Package Manager) 3. yum在线安装 3.1 yum源文件 vi /etc/yum.repos.d/CentOS-Base. ...
- jmeter之jtl文件解析
我们知道命令行的方式执行完成jmeter后,会生成jtl文件,里面打开后就是一行行的测试结果, <httpSample t="1" lt="1" ts=& ...
- IOS Android支持中文与本地文件的读取写入
转自http://www.xuanyusong.com/archives/1069 和http://www.benmutou.com/archives/2094 前几天有个朋友问我为什么在IOS平台中 ...
- redis介绍。
1. Redis是什么 这个问题的结果影响了我们怎么用Redis.如果你认为Redis是一个key value store, 那可能会用它来代替MySQL;如果认为它是一个可以持久化的cache, 可 ...
- Spring Security(15)——权限鉴定结构
目录 1.1 权限 1.2 调用前的处理 1.2.1 AccessDecisionManager 1.2.2 基于投票的AccessDecisionManager实 ...
- 修复ubunut桌面
title: 修复ubunut桌面 tags: 桌面, ubuntu grammar_cjkRuby: true --- ,按下Ctrl+Alt+F2.这会让你进入一个命令行界面而不是默认的用户桌面界 ...
- Scala 中的 apply 和 update 方法[转]
原文链接:http://blog.csdn.net/lyrebing/article/details/21696581 Scala 是构建在 JVM 上的静态类型的脚本语言,而脚本语言总是会有些约定来 ...
- [Q]AdobePDF虚拟打印机自动保存PDF
使用打图精灵打印时,选择“Adobe PDF”虚拟打印机打印(注意不选择“打印到文件”),每张图纸都会弹出一个保存对话框,如何避免? 从 操作系统->控制面板->硬件和声音->设备和 ...
- canvas烟花-娱乐
网上看到一个释放烟花的canvas案例,很好看哦. 新建文本,把下面代码复制进去,后缀名改为html,用浏览器打开即可. 看懂注释后,可以自己修改烟花的各个效果.我试过让烟花炸成了心型.:-) < ...
- F9 开发之左树右表中的左树
1 首先在前端应用树树控件 <div class="fui-left"> <div role="head" title="地区选择& ...