poj 1837 Balance 动态规划 (经典好题,很锻炼思维)
题目大意:给你一个天平,并给出m个刻度,n个砝码,刻度的绝对值代表距离平衡点的位置,并给出每个砝码的重量。达到平衡状态的方法有几种。
题目思路:首先我们先要明确dp数组的作用,dp[i][j]中,i为放置的砝码数量,j为平衡状态,0为平衡,j<0左倾,j>0右倾,由于j作为下标不能是负数,所以我们要找一个新的平衡点,因为15*20*20 = 7500,所以平衡点设置为7500,
然后我们可以得出动态方程 dp[i][j+w[i]*c[k])+=dp[i-1][j];
#include<cstdio>
#include<stdio.h>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
#define INF 0x3f3f3f3f
#define MAX 1005 using namespace std; int dp[][],w[MAX],v[MAX]; int main()
{
int i,j,k,n,m; while(scanf("%d%d",&m,&n)!=EOF)
{
for(i=;i<=m;i++)
scanf("%d",&w[i]);
for(i=;i<=n;i++)
scanf("%d",&v[i]); memset(dp,,sizeof(dp));
dp[][]=;//更改平衡点位置 for(i=;i<=n;i++)
{
for(j=;j<=;j++)
{
for(k=;k<=m;k++)
{
dp[i][j+v[i]*w[k]]+=dp[i-][j];
}
}
}
printf("%d\n",dp[n][]);
}
return ;
}
poj 1837 Balance 动态规划 (经典好题,很锻炼思维)的更多相关文章
- 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
Balance Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 9240 Accepted: 5670 Description G ...
- POJ 1837 Balance 【DP】
题意:给出一个天平,给出c个钩子,及c个钩子的位置pos[i],给出g个砝码,g个砝码的质量w[i],问当挂上所有的砝码的时候,使得天平平衡的方案数, 用dp[i][j]表示挂了前i个砝码时,平衡点为 ...
- [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个挂 ...
随机推荐
- spring中的控制反转IoC和依赖注入DI
原文:http://blog.163.com/xianghuxian@126/blog/static/50639037200721345218382/ IoC(Inversion of Control ...
- msdn我告诉你
http://msdn.itellyou.cn/ 微软旗下所有的msdn订阅软件资源 均为ed2k资源 Business Solutions MSDN Library 工具和资源 应用程序 开发人员工 ...
- python常用函数年初大总结
1.常用内置函数:(不用import就可以直接使用) help(obj) 在线帮助, obj可是任何类型 callable(obj) 查看一个obj是不是可以像函数一样调用 repr(obj) 得到o ...
- recursion 递归以及递归的缺点
递归定义的算法有两部分: 递归基:直接定义最简单情况下的函数值: 递归步:通过较为简单情况下的函数值定义一般情况下的函数值. 应用条件与准则: (1)问题具有某种可借用的类同自身的子问题描述的性质: ...
- JavaScript跨域总结与解决办法 什么是跨域
什么是跨域 1.document.domain+iframe的设置 2.动态创建script 3.利用iframe和location.hash 4.window.name实现的跨域数据传输 5.使用H ...
- screen 链接远程桌面
screen 开一个新的screen窗口 screen -ls 查看已经存在的所有screen窗口 screen -r 208111 进入这个窗口 ctrl+a+d 退出screen,回 ...
- Qt之中文显示(QMessageBox、QLineEdit右键菜单等)
来源:http://blog.sina.com.cn/s/blog_a6fb6cc90101art3.html 在编写Qt程序的时候,总会碰到中文问题,一直都很困惑,原本在使用QLineEdit的时候 ...
- 文件系统满的话(filesystem full),该如何处理。
#!/bin/bash function ergodic(){ ` do "/"$file ] then ergodic $"/"$file else loca ...
- 字符函数库 cctype
<cctype> (ctype.h) Character handling functions This header declares a set of functions to cla ...
- Hibernate 系列教程16-二级缓存
pom.xml <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate- ...