Balance
Balance
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 11947 Accepted: 7464
Description
Gigel has a strange “balance” and he wants to poise it. Actually, the device is different from any other ordinary balance.
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 input has the following structure:
• 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
The output contains the number M representing the number of possibilities to poise the balance.
Sample Input
2 4
-2 3
3 4 5 8
Sample Output
2
题意:
有一个天平,天平左右两边各有若干个钩子,总共有C个钩子,有G个钩码,求将钩码全部挂到钩子上使天平平衡的方法的总数。
其中可以把天枰看做一个以x轴0点作为平衡点的横轴
动态规划dp[i][j],i代表第i个钩码,j代表平衡状态
将所有的钩码都挂上的极端情况就是[-7500,7500],所以移动平衡点,将7500作为新的平衡点,所以对于dp[i-1][j],dp[i][j+len[k]*w[j]]+=dp[i-1][j],有挂i-1个钩码状态转移到挂i个钩码时对应的状态
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
#include <algorithm>
#define LL long long
using namespace std;
const int MAX = 15000;
int dp[25][MAX+10];
int w[25],len[25];
int main()
{
int C,G;
while(~scanf("%d %d",&C,&G))
{
for(int i=1;i<=C;i++)
{
scanf("%d",&len[i]);
}
for(int i=1;i<=G;i++)
{
scanf("%d",&w[i]);
}
memset(dp,0,sizeof(dp));
dp[0][7500]=1;
for(int i=1;i<=G;i++)
{
for(int j=0;j<=MAX;j++)
{
if(dp[i-1][j])
{
for(int k=1;k<=C;k++)
{
dp[i][j+len[k]*w[i]]+=dp[i-1][j];
}
}
}
}
printf("%d\n",dp[G][7500]);
}
return 0;
}
Balance的更多相关文章
- Sample a balance dataset from imbalance dataset and save it(从不平衡数据中抽取平衡数据,并保存)
有时我们在实际分类数据挖掘中经常会遇到,类别样本很不均衡,直接使用这种不均衡数据会影响一些模型的分类效果,如logistic regression,SVM等,一种解决办法就是对数据进行均衡采样,这里就 ...
- LB(Load balance)负载均衡集群--{LVS-[NAT+DR]单实例实验+LVS+keeplived实验} 菜鸟入门级
LB(Load balance)负载均衡集群 LVS-[NAT+DR]单实例实验 LVS+keeplived实验 LVS是Linux Virtual Server的简写,意即Linux虚拟服务器,是一 ...
- Balance - 七夕悠然
想争取一个月至少一篇博客的,还是没搭上七月的末班车.两个小妹妹来上海看我了,工作上又有点儿忙,充分利用所有时间了,还是没有挪出时间来写东西,貌似写东西也要时机一样,需要在可以静静思考的时候,再加上有淡 ...
- Java-集合=第五题 (Map)设计Account 对象如下: private long id; private double balance; private String password; 要求完善设计,使得该Account 对象能够自动分配id。 给定一个List 如下: List list = new ArrayList(); list.add(new A
第五题 (Map)设计Account 对象如下: private long id; private double balance; private String password; 要求完善设计,使得 ...
- java异常处理:建立exception包,建立Bank类,类中有变量double balance表示存款,Bank类的构造方法能增加存款,Bank类中有取款的发方法withDrawal(double dAmount),当取款的数额大于存款时,抛出InsufficientFundsException,取款数额为负数,抛出NagativeFundsException,如new Bank(100),
建立exception包,建立Bank类,类中有变量double balance表示存款,Bank类的构造方法能增加存款,Bank类中有取款的发方法withDrawal(double dAmount ...
- UVa 673 Parentheses Balance -SilverN
You are given a string consisting of parentheses () and []. A string of this type is said to be corr ...
- POJ1837 Balance[分组背包]
Balance Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 13717 Accepted: 8616 Descript ...
- UVa 673 Parentheses Balance
一个匹配左右括号的问题 /*UVa 673 Parentheses Balance*/ #include<iostream> #include<algorithm> #incl ...
- A feature in Netsuite Reports > Financial > Balance Sheet
最新版本的Customize balance sheet page Left side > Layout > Add Reference Row Then in right side, y ...
- POJ 3142 The Balance
Description Ms. Iyo Kiffa-Australis has a balance and only two kinds of weights to measure a dose of ...
随机推荐
- 脚本:SQLServer 2008 生成某数据库中的所有索引创建脚本
--1. get all indexes from current db, place in temp table select schemaName = s.name, tablename = ob ...
- for 穷举、迭代 while循环
1.穷举: 把所有可能的情况都走一遍,使用if条件筛选出来满足条件的情况. 2.百鸡百钱:公鸡2文钱一只,母鸡1文钱一只,小鸡半文钱一只,总共只有100文钱,如何在凑够100只鸡的情况下刚好花完100 ...
- 免安装版的MySQL的安装与配置
1. 将下载的 mysql-noinstall-5.1.69-win32.zip 解压至需要安装的位置, 如: C:\Program Files; 2. 在安装文件夹下找到 my-small.ini ...
- C++之路起航——标准模板库(set)
set(集合):http://baike.baidu.com/link?url=cb68AB-3qfEK8RoaGHJFClb4ZiWpJfc32lPOLtaNUrdxntFC738zCZsCiUlf ...
- 线性表基本维护[ACM]
#include "iostream" #include "string" using namespace std; typedef struct node{ ...
- 夺命雷公狗ThinkPHP项目之----企业网站15之文章删除的完成(单个)
我们文章最后一步就到他的删除了,这个其实是最简单的一个,废话不多说,先来写控制器: public function del(){ $id = I('ar_id'); if(M('Article')-& ...
- remoting方式
1. WebService跨平台,跨防火墙,但是很抱歉,基于xml速度慢2. RMI(java)/Remoting(.net)平台相关,基于二进制序列化,速度快.3.dcom(com+)spring提 ...
- APP的UI测试要点
1.文字显示是否正确 比如与需求图片对比是否正确,无错别字 2.对齐方式是否正确 3.图片 图片显示的篇幅不要太大. 4.颜色是否正确 颜色与需求规定的是否一致
- 使用javascript判断浏览器对css3的支持情况【译】
Quick Tip: Detect CSS3 Support in Browsers with JavaScript Jeffrey Way on Nov 15th 2010 步骤 1 首先我们要确 ...
- drupal 做301跳转(删除url里的www), 关键代码 可用到任何网站
//hook_init(); function ex_init(){ //删除 url 前面的 www if (substr($_SERVER['HTTP_HOST'],0,3) == 'www'){ ...