poj1837(Balance)

题目地址:Balance

题目大意:

  有一个天平,天平左右两边各有若干个钩子,总共有C个钩子,有G个钩码,求将钩码全部挂到钩子上使天平平衡的方法的总数。中可以把天枰看做一个以x轴0点作为平衡点的横轴。

解题思路:

  DP:最极端的平衡度是所有物体都挂在最远端,因此平衡度最大值为j=15*20*25=7500。原则上就应该有dp[ 1~20 ][-7500 ~ 7500 ]。 转化为01背包,其中 i 代表 前 i 个挂钩达到 j 平衡度的所有情况,因为是一个平衡所以求出中间值的平衡度情况就是所求的平衡的结果,因为存在负数dp转化为dp[ 1~20 ][0 ~ 16000]。 求出dp[G][8000]即可,记得初始化,初始化只能初始dp[0][8000],如果把所有挂钩为0的平衡度初始会影响结果。

  可以设dp[i-1][j]=num,那么对于dp[i][j]来说有两种决策,就是选第i个砝码放在哪个位置或者不放。所以dp[i][j]+=dp[i-1][j-pos[k]*weight[i]]。当然前提条件是j>=pos[k]*weight[i].

代码:

 1 #include <algorithm>
 2 #include <iostream>
 3 #include <sstream>
 4 #include <cstdlib>
 5 #include <cstring>
 6 #include <cstdio>
 7 #include <string>
 8 #include <bitset>
 9 #include <vector>
10 #include <queue>
11 #include <stack>
12 #include <cmath>
13 #include <list>
14 #include <map>
15 #include <set>
16 using namespace std;/***************************************/
17 #define ll long long
18 #define int64 __int64/***************************************/
19 const int INF = 0x7f7f7f7f;
20 const ll LINF = (1LL<<60);
21 const double eps = 1e-8;
22 const double PIE=acos(-1.0);
23 const int d1x[]= {0,-1,0,1};
24 const int d1y[]= {-1,0,1,0};
25 const int d2x[]= {0,-1,0,1};
26 const int d2y[]= {1,0,-1,0};
27 const int fx[]= {-1,-1,-1,0,0,1,1,1};
28 const int fy[]= {-1,0,1,-1,1,-1,0,1};
29 inline int min_32(int (a),int (b)){return (a)<(b)?(a):(b);}
30 inline int max_32(int (a),int (b)){return (a)>(b)?(a):(b);}
31 inline long long min_64(long long (a),long long (b)){return (a)<(b)?(a):(b);}
32 inline long long max_64(long long (a),long long (b)){return (a)>(b)?(a):(b);}
33 /***************************************/
34 void openfile(){
35     freopen("data.in","rb",stdin);
36     freopen("data.out","wb",stdout);
37 }
38 /**********************华丽丽的分割线,以上为模板部分*****************/
39
40 int dp[21][20000];//代表第i个挂钩的平衡度的情况
41 int c[21];
42 int g[21];
43 int main()
44 {
45     int C,G;
46     while(scanf("%d%d",&C,&G)!=EOF)
47     {
48         memset(c,0,sizeof(c));
49         memset(g,0,sizeof(g));
50         memset(dp,0,sizeof(dp));
51         int i,j,k;
52         for(i=1;i<=C;i++)
53             scanf("%d",&c[i]);
54         for(i=1;i<=G;i++)
55             scanf("%d",&g[i]);
56        // for(i=0;i<=16000;i++)
57             dp[0][8000]=1;  //初始化
58         for(i=1;i<=G;i++)
59             for(j=0;j<=16000;j++)
60                 for(k=1;k<=C;k++)
61                     if (j>=c[k]*g[i])
62                         dp[i][j]+=dp[i-1][j-c[k]*g[i]];
63         printf("%d\n",dp[G][8000]);
64     }
65     return 0;
66 }

时间: 2024-10-05 19:34:45

poj1837(Balance)的相关文章

POJ1837 Balance[分组背包]

Balance Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 13717   Accepted: 8616 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

Poj1837 Balance 动态规划-01背包

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

POJ-1837 Balance (DP背包问题)

Balance Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 13395   Accepted: 8382 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

poj 01背包

首先我是按这篇文章来确定题目的. poj3624 Charm Bracelet 模板题 没有要求填满,所以初始化为0就行 #include<cstdio> #include<algorithm> #include<cmath> #include<iostream> #include<cstring> using namespace std; int w[3403]; int h[3403]; int n,m; int dp[12880+9]; i

POJ之01背包系列

poj3624 Charm Bracelet 模板题 没有要求填满,所以初始化为0就行 #include<cstdio> #include<iostream> using namespace std; #define N 15010 int n,m,v[N],c[N],f[N]; int main(){ scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) scanf("%d%d",&am

poj分类解题报告索引

图论 图论解题报告索引 DFS poj1321 - 棋盘问题 poj1416 - Shredding Company poj2676 - Sudoku poj2488 - A Knight's Journey poj1724 - ROADS(邻接表+DFS) BFS poj3278 - Catch That Cow(空间BFS) poj2251 - Dungeon Master(空间BFS) poj3414 - Pots poj1915 - Knight Moves poj3126 - Prim

POJ1837:Balance(01背包)

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

LB(Load balance)负载均衡集群--{LVS-[NAT+DR]单实例实验+LVS+keeplived实验} 菜鸟入门级

LB(Load balance)负载均衡集群 LVS-[NAT+DR]单实例实验 LVS+keeplived实验 LVS是Linux Virtual Server的简写,意即Linux虚拟服务器,是一个虚拟的服务器集群系统. 逻辑可分为: 1调度层 (Director):它是整个集群对外面的前端机,负责将客户的请求发送到一组服务器上执行,而客户认为服务是来自一个IP地址(我们可称之为虚拟IP地址)上的. 2[服务器池(server pool)/集群层(Real server)]:是一组真正执行客

轻量级网络流量转发工具 - Balance

Balance是一款具有负载均衡和故障转移功能的TCP代理软件.支持IPV6地址的监听和转发,支持rr轮询和ip hash. 某些功能可以替代iptables,比如讲来自本地的80端口转发到8080上: iptables是这样写的: iptables -t nat -A PREROUTING -d 192.168.1.1 -p tcp --dport 80 -j DNAT --to 192.168.1.1:8080 balance只需要这样: balance 80 localhost:8080