[poj 1837] Balance dp
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 题意:用所有的砝码挂在天平的挂钩上,使天平平衡的挂法有多少种。
dp[i][j], 表示挂前i个砝码使其平衡系数(力矩和)为j的挂法, i∈[0,20], 当全部挂在最右端时, j = 15*20*25 = 7500, 最左端时-7500
为使其有意义,所以把j取值为[0, 7500*2], 7500时为平衡点
状态转移方程:dp[i][ j + w[i]*dis[k] ] += dp[i-1][j] k∈[1, c]
#include <iostream>
#include <stdio.h>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxf = *;
int c, g;
int dis[];
int w[];
int dp[][maxf]; int main()
{
//freopen("1.txt", "r", stdin);
cin >> c >> g;
for (int i = ; i <= c; i++)
cin >> dis[i];
for (int i = ; i <= g; i++)
cin >> w[i]; memset(dp, , sizeof(dp));
dp[][] = ;
for (int i = ; i <= g; i++) {
for (int j = ; j <= maxf; j++) {
if (dp[i-][j]) {
for (int k = ; k <= c; k++) {
dp[i][j + w[i]*dis[k]] += dp[i-][j];
}
}
}
}
printf("%d\n", dp[g][]); return ;
}
[poj 1837] Balance dp的更多相关文章
- POJ 1837 -- Balance(DP)
POJ 1837 -- Balance 转载:優YoU http://user.qzone.qq.com/289065406/blog/1299341345 提示:动态规划,01背包 初看此题第 ...
- poj 1837 Balance(背包)
题目链接:http://poj.org/problem?id=1837 Balance Time Limit: 1000MS Memory Limit: 30000K Total Submissi ...
- POJ 1837 Balance 01背包
题目: http://poj.org/problem?id=1837 感觉dp的题目都很难做,这道题如果不看题解不知道憋到毕业能不能做出来,转化成了01背包问题,很神奇.. #include < ...
- POJ 1837 Balance 水题, DP 难度:0
题目 http://poj.org/problem?id=1837 题意 单组数据,有一根杠杆,有R个钩子,其位置hi为整数且属于[-15,15],有C个重物,其质量wi为整数且属于[1,25],重物 ...
- POJ 1837 Balance(01背包变形, 枚举DP)
Q: dp 数组应该怎么设置? A: dp[i][j] 表示前 i 件物品放入天平后形成平衡度为 j 的方案数 题意: 有一个天平, 天平的两侧可以挂上重物, 给定 C 个钩子和G个秤砣. 2 4 - ...
- POJ 1837 Balance 【DP】
题意:给出一个天平,给出c个钩子,及c个钩子的位置pos[i],给出g个砝码,g个砝码的质量w[i],问当挂上所有的砝码的时候,使得天平平衡的方案数, 用dp[i][j]表示挂了前i个砝码时,平衡点为 ...
- POJ 1837 Balance
Balance Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 9240 Accepted: 5670 Description G ...
- poj 1837 Balance (0 1 背包)
Balance Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 10326 Accepted: 6393 题意:给你n个挂 ...
- poj 1837 Balance 动态规划 (经典好题,很锻炼思维)
题目大意:给你一个天平,并给出m个刻度,n个砝码,刻度的绝对值代表距离平衡点的位置,并给出每个砝码的重量.达到平衡状态的方法有几种. 题目思路:首先我们先要明确dp数组的作用,dp[i][j]中,i为 ...
随机推荐
- How to handle Imbalanced Classification Problems in machine learning?
How to handle Imbalanced Classification Problems in machine learning? from:https://www.analyticsvidh ...
- python习题-用交集方式产生随机密码
# 1.写一个产生密码的程序,# 输入次数,输入多少次就产生多少条数据,# 要求密码必须包含大写字母.小写字母和数字,长度8位,不能重复 import string ,random num=input ...
- inux命令学习笔记(5):rm 命令
学习了创建文件和目录的命令mkdir ,今天学习一下linux中删除文件和目录的命令: rm命令. rm是常用的命令,该命令的功能为删除一个目录中的一个或多个文件或目录,它也可以将某个目 录及其下的所 ...
- linux命令学习笔记(19):find 命令概览
Linux下find命令在目录结构中搜索文件,并执行指定的操作.Linux下find命令提供了相当多的查找条件,功能 很强大.由于find具有强大的功能,所以它的选项也很多,其中大部分选项都值得我们花 ...
- 【leetcode刷题笔记】N-Queens II
Follow up for N-Queens problem. Now, instead outputting board configurations, return the total numbe ...
- luoguP1941福赖皮波德
#include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #inc ...
- HihoCoder1339 Dice Possibility(概率DP+母函数)
时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 What is possibility of rolling N dice and the sum of the numb ...
- 【Caffe】Ubuntu 安装 Caffe gpu版
安装环境:Ubuntu 16.04lts 64位, gcc5.4 gpu1050ti,cuda8.0,cudnn5.1.10 1. 安装依赖库 sudo apt-get install libprot ...
- 洛谷【P1873】砍树
我对二分的理解:https://www.cnblogs.com/AKMer/p/9737477.html 题目传送门:https://www.luogu.org/problemnew/show/P18 ...
- 百度地图设置div样式宽高为百分比不显示地图
如题,不显示地图只要在样式代码里面加以 position:absolute; 代码就可以了 <style type="text/css"> body, html,#al ...