POJ 1837-Balance(01背包)
来源:互联网 发布:教室楼图纸软件 编辑:程序博客网 时间:2024/04/30 07:12
题目地址:POJ 1837
题意:有一个天平,左右两边各有若干个钩子,总共有C个钩子,有G个钩码,求将钩码全部挂到钩子上使天平平衡的方法的总数。
思路:可以把天枰看做一个以x轴0点作为平衡点的横轴。
定义一个 状态数组dp[i][j],意为在挂满前i个钩码时,平衡度为j的挂法的数量。当j=0时,说明天枰达到平衡,j>0,说明天枰倾向右边(x轴右半轴),j<0则相反。由于距离c[i]的范围是-15~15,钩码重量的范围是1~25,钩码数量最大是20。最极端的平衡是将所有的都挂在最远端,所以15*25*20=7500.所以有dp[1~20][-7500~+7500],为了不让下标出现负数,所以dp[1~20][0~15000],则7500成了平衡点。
#include <stdio.h>#include <math.h>#include <string.h>#include <stdlib.h>#include <iostream>#include <sstream>#include <algorithm>#include <set>#include <queue>#include <stack>#include <map>#pragma comment(linker, "/STACK:102400000,102400000")using namespace std;typedef long long LL;const int inf=0x3f3f3f3f;const double pi= acos(-1.0);const double esp=1e-6;int dp[25][15010];int c[25];int g[25];int main(){ int C,G; int i,j,k; while(~scanf("%d %d",&C,&G)){ memset(dp,0,sizeof(dp)); for(i=1;i<=C;i++) scanf("%d",&c[i]); for(i=1;i<=G;i++) scanf("%d",&g[i]); dp[0][7500]=1;//不挂钩码达到平衡 for(i=1;i<=G;i++) for(j=1;j<=15000;j++) for(k=1;k<=C;k++) dp[i][j+g[i]*c[k]]+=dp[i-1][j]; printf("%d\n",dp[G][7500]); } return 0;}
1 0
- poj 1837 Balance(01背包)
- poj 1837 Balance--01背包
- poj 1837 Balance 01背包
- poj 1837 Balance (01背包)
- poj 1837 Balance (01背包)
- POJ 1837 Balance(01背包)
- POJ 1837 Balance (01背包)
- POJ 1837-Balance(01背包)
- POJ-1837-Balance -01背包
- POJ 1837 Balance(01背包)
- POJ 1837 Balance 01背包
- POJ Balance 1837(01背包)
- POJ 1837 Balance 【01背包】
- POJ 1837:Balance(01背包)
- POJ 1837 Balance (01背包)
- poj 1837 Balance(背包)
- POJ Balance (01背包)
- poj 1837 Balance(01背包)
- 如何更快捷的设置Android Studio和Eclipse的快捷键一样和代码提示
- Ionic开发实战
- C++中的 explicit 关键字
- android接收从其他App传送来的数据
- 内存数据网格hazelcast的一些机制原理
- POJ 1837-Balance(01背包)
- Algorithms—240.Search a 2D Matrix II
- 与时间相关的函数编程
- jsp页面中form表单提交到数据库的方法
- hdu 1711 KMP模板题
- Android ListView的setEmptyView方法的使用
- python StringIO和BytesIO
- python StringIO和BytesIO
- iOS开发系列--UITableView全面解析