poj1837--Balance(dp:天平问题)
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 10773 | Accepted: 6685 |
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
Source
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <math.h>
using namespace std;
#define maxn 8000
int cc[30] , gg[30] ;
int dp[30][maxn<<1] ;
int main()
{
int c , g , i , j , k , max1 = 0 , m = 0 ;
memset(dp,0,sizeof(dp));
dp[0][maxn] = 1 ;
scanf("%d %d", &c, &g);
for(i = 0 ; i < c ; i++)
{
scanf("%d", &cc[i]);
if( abs(cc[i]) > max1 )
max1 = abs(cc[i]) ;
}
for(i = 1 ; i <= g ; i++)
{
scanf("%d", &gg[i]);
m += max1*gg[i] ;
}
max1 = m ;
for(i = 1 ; i <= g ; i++)
{
for(j = 0 ; j < c ; j++)
{
if( cc[j] > 0 )
{
m = gg[i]*cc[j] ;
for(k = maxn + max1 ; k >= m ; k--)
dp[i][k] += dp[i-1][k-m] ;
}
else
{
m = gg[i]*cc[j] ;
for(k = m ; k <= maxn+max1 ; k++)
dp[i][k] += dp[i-1][k-m] ;
}
}
}
printf("%d\n", dp[g][maxn]);
return 0;
}
poj1837--Balance(dp:天平问题)的更多相关文章
- POJ1837 Balance(DP)
POJ1837http://poj.org/problem?id=1837 题目大意就是说有一个称上有C个挂钩,告诉你每个挂钩的位置,现在有G个重物,求是之平衡的方法数. 转化一下:DP[i][j]表 ...
- POJ1837 Balance[分组背包]
Balance Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 13717 Accepted: 8616 Descript ...
- HDU 5616 Jam's balance(DP)
题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=5616 题目: Jam's balance Time Limit: 2000/1000 MS (Java ...
- [poj 1837] Balance dp
Description Gigel has a strange "balance" and he wants to poise it. Actually, the device i ...
- poj1837 Balance
Balance POJ - 1837 题目大意: 有一个天平,天平左右两边各有若干个钩子,总共有C个钩子,有G个钩码,求将钩码全部挂到钩子上使天平平衡的方法的总数. 其中可以把天枰看做一个以x轴0点 ...
- POJ 1837 -- Balance(DP)
POJ 1837 -- Balance 转载:優YoU http://user.qzone.qq.com/289065406/blog/1299341345 提示:动态规划,01背包 初看此题第 ...
- Codeforces Beta Round #17 C. Balance DP
C. Balance 题目链接 http://codeforces.com/contest/17/problem/C 题面 Nick likes strings very much, he likes ...
- POJ1837 Balance 背包
题目大意: 有一个天平,天平左右两边各有若干个钩子,总共有C个钩子(每个钩子有相对于中心的距离,左负右正),有G个钩码,求将钩码全部挂到钩子上使天平平衡的方法的总数. 将每个砝码看作一组,组内各个物品 ...
- HDU 1709 The Balance( DP )
The Balance Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- 【做题】ECFinal2018 J - Philosophical … Balance——dp
原文链接 https://www.cnblogs.com/cly-none/p/ECFINAL2018J.html 题意:给出一个长度为\(n\)的字符串\(s\),要求给\(s\)的每个后缀\(s[ ...
随机推荐
- 鸟哥的私房菜:Bash shell(二)-变量的丰富功能
一 Shell变量的取用与设定,变量规则 由于系统需要一些变量来提供他数据的存取,所以就有一些所谓的『环境变量』 需要来读入系统中了!这些环境变量例如 PATH.HOME.MAIL.SHELL.为了 ...
- 7.4 (java学习笔记)网络编程之TCP
一.TCP 1.1 TCP(Transmission Control Protocol 传输控制协议),是一种面向连接的,安全的传输协议,但效率相比于UDP而言比较低. TCP传输时需要确保先建立连接 ...
- 监控 Linux 系统的 7 个命令行工具
监控 Linux 系统的 7 个命令行工具: " 深入 关于Linux最棒的一件事之一是你能深入操作系统,来探索它是如何工作的,并寻找机会来微调性能或诊断问题.这里有一些基本的命令行工具,让 ...
- Codeforces Round #360 (Div. 2) D. Remainders Game 数学
D. Remainders Game 题目连接: http://www.codeforces.com/contest/688/problem/D Description Today Pari and ...
- Linux性能监控分析命令(四)—top命令介绍
性能监控分析的命令包括如下: 1.vmstat 2.sar 3.iostat 4.top 5.free 6.uptime 7.netstat 8.ps 9.strace 10.lsof ======= ...
- Window 下安装
Window 下安装 下载地址:https://github.com/MSOpenTech/redis/releases Redis 支持 32 位和 64 位.这个需要根据你系统平台的实际情况选择, ...
- java并发基础(一)
最近在看<java并发编程实战>,希望自己有毅力把它读完. 线程本身有很多优势,比如可以发挥多处理器的强大能力.建模更加简单.简化异步事件的处理.使用户界面的相应更加灵敏,但是更多的需要程 ...
- 多个rs485设备怎样跟上位机通讯?
http://bbs.hcbbs.com/thread-819457-1-1.html 多个rs485设备怎样跟上位机通讯? [复制链接] |关注本帖 fdemeng 签到天数: 1228 天 ...
- 8个免费且实用的C++ GUI库(转载)
原文链接:http://zoomzum.com/8-free-and-useful-c-gui-libraries/ 作者的话:C++标准中并没有包含GUI,这也使得C++开发图形化界面需要依赖于 ...
- “==”和equals之间的区别
通俗点讲,==是看看左右是不是一个东西.equals是看看左右是不是长得一样.如果单纯是想记住,==:等于.equals:相同.两个长得一样的人,只能说长的相同(equals),但是不等于他们俩是一个 ...