Balance POJ - 1837 地推
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 递推,注意到砝码一边的最大重量不超过7500,讲7500设为新的0点,枚举所有物品选择和节点选择,注意剪枝。。
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<sstream>
#include<algorithm>
#include<queue>
#include<vector>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<memory>
#include<bitset>
#include<string>
#include<functional>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
const int MAXN = * ; #define INF 0x3f3f3f3f //0 1 背包
/*
dp[i][MAXN]
*/
int dp[][MAXN];
int x[], w[];
int solve(int c, int g)
{
for (int i = ; i <= g; i++)
{
for (int v = ; v < MAXN; v++)
{
if (dp[i - ][v])
{
for (int j = ; j <= c; j++)
{
dp[i][v + w[i] * x[j]] += dp[i - ][v];
}
}
}
}
return dp[g][];
}
int main()
{
int c, g;
scanf("%d%d", &c, &g);
memset(dp, , sizeof(dp));
for (int i = ; i <= c; i++)
scanf("%d", &x[i]);
for (int j = ; j <= g; j++)
scanf("%d", &w[j]);
/*for (int i = 0; i < c; i++)
{
for (int j = 0; j < g; j++)
{
dp[0][x[i] * w[j] + 7500]++;
}
}*/
dp[][] = ;
printf("%d\n", solve(c, g));
return ;
}
Balance POJ - 1837 地推的更多相关文章
- Balance POJ - 1837
Description Gigel has a strange "balance" and he wants to poise it. Actually, the device i ...
- 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
Balance Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 9240 Accepted: 5670 Description G ...
- POJ 1837 Balance(01背包变形, 枚举DP)
Q: dp 数组应该怎么设置? A: dp[i][j] 表示前 i 件物品放入天平后形成平衡度为 j 的方案数 题意: 有一个天平, 天平的两侧可以挂上重物, 给定 C 个钩子和G个秤砣. 2 4 - ...
- [poj 1837] Balance dp
Description Gigel has a strange "balance" and he wants to poise it. Actually, the device i ...
- poj 1837 Balance (0 1 背包)
Balance Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 10326 Accepted: 6393 题意:给你n个挂 ...
随机推荐
- 工作中js和jquery 函数收集
1. 判断单选框是否选中 $("xxx").is(":checked") 返回类型是 true/false 2. $(this)[0].nextS ...
- 轻松搞懂Java中的自旋锁
前言 在之前的文章<一文彻底搞懂面试中常问的各种“锁”>中介绍了Java中的各种“锁”,可能对于不是很了解这些概念的同学来说会觉得有点绕,所以我决定拆分出来,逐步详细的介绍一下这些锁的来龙 ...
- 后缀数组 (Suffix Array) 学习笔记
\(\\\) 定义 介绍一些写法和数组的含义,首先要知道 字典序 . \(len\):字符串长度 \(s\):字符串数组,我们的字符串存储在 \(s[0]...s[len-1]\) 中. \(suff ...
- Heart Beat
实现关键: 1.纯css实现心形图(如果使用图片则无需) html代码: <!DOCTYPE html> <html> <head> <meta charse ...
- iOS地图----MapKit框架
1.MapKit框架使用前提 ①导入框架 ②导入主头文件 #import <MapKit/MapKit.h> ③MapKit框架使用须知 MapKit框架中所有数据类型的前缀都是MK Ma ...
- swift不同地方 命名空间module iOS
Objective-C没有命名空间,为了避免冲突,Objective-C的类型一般都会加上两到三个字母的前缀,比如Apple保留的NS和UI前缀,各个系统框架的前缀,各个系统框架的前缀SK(Store ...
- ALTER SCHEMA - 修改一个模式的定义
SYNOPSIS ALTER SCHEMA name RENAME TO newname DESCRIPTION 描述 ALTER SCHEMA 修改一个模式的定义. 现在它唯一的功能就是重命名模式. ...
- anchor_target_layer层解读
总结下来,用generate_anchors产生多种坐标变换,这种坐标变换由scale和ratio来,相当于提前计算好.anchor_target_layer先计算的是从feature map映射到原 ...
- zabbix4.2学习笔记--安装percona插件监控MySQL
percona-monitoring-plugins是percona专门为MySQL监控的工具,支持Nagios,cacti,zabibx,本文主要介绍percona-monitoring-plugi ...
- android 获取application和activity下meta-data中的值
meta-data在AndroidManifest中是以键值对的形式存在的,可以将meta-data放在application根节点下,也可以放在某个activity节点下.因为存放的位置不同,因此获 ...