POJ 1837-Balance(01背包-天平平衡)
来源:互联网 发布:土地面积测量仪软件下载 编辑:程序博客网 时间:2024/05/29 13:51
Balance
Time Limit: 1000MS Memory Limit: 30000KTotal Submissions: 14034 Accepted: 8824
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.
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.
• 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
Source
Romania OI 2002
题目意思:
给出天平上C个刻度的位置(天平臂中间是零刻度),G个钩码的重量,计算要使天平保持平衡的不同方法数。
解题思路:
01背包问题,钩码可以放置或不放置。
由于刻度可能为负,所以偏移量也可能为负,所以:
“将g个挂钩挂上的极限值:15*25*20==7500
那么在有负数的情况下是-7500~~7500 以0为平衡点......
那可以将平衡点往右移7500个单位,范围就是0~15000......这样就好处理多了。”
dp[i][j]表示:放第i个钩码时天平的偏移量为j。状态方程是:dp[i][ j+ g[i]*c[k] ]= ∑(dp[i-1][j])
#include<iostream>#include<cstdio>#include<iomanip>#include<cmath>#include<cstdlib>#include<cstring>#include<map>#include<algorithm>using namespace std;#define INF 0xfffffff#define MAXN 30int a[MAXN],b[MAXN];int dp[MAXN][15005];//dp[i][j]表示:放第i个钩码时天平的偏移量为jint main(){#ifdef ONLINE_JUDGE#else freopen("F:/cb/read.txt","r",stdin); //freopen("F:/cb/out.txt","w",stdout);#endif ios::sync_with_stdio(false); cin.tie(0); int c,g;//位置、钩码数量 cin>>c>>g; for(int i=1; i<=c; ++i) cin>>a[i];//位置 for(int i=1; i<=g; ++i) cin>>b[i];//钩码重量 memset(dp,0,sizeof(dp)); //方法数初始化为0 dp[0][7500]=1; //7500时,当前不挂钩码,达到平衡状态 for(int i=1; i<=g; i++)//钩码 for(int j=0; j<=15000; j++) if(dp[i-1][j])//放第i-1个钩码时的偏移量j已经被处理过 for(int k=1; k<=c; k++)//位置 dp[i][j+b[i]*a[k]] +=dp[i-1][j]; cout<<dp[g][7500]<<endl; return 0;}/*2 4-2 33 4 5 8*/
0 0
- poj 1837 Balance(01背包 天平平衡)
- POJ 1837-Balance(01背包-天平平衡)
- POJ 1837 Balance(天平问题 01背包变形)
- poj 1837 背包(让天平平衡的方法)
- POJ 1837 二维背包 天平平衡问题
- POJ 1837:Balance 天平DP。。。
- POJ 1837 天平平衡问题 类似背包问题
- poj 1837 Balance(01背包)
- poj 1837 Balance (dp 01背包)
- poj 1837 Balance (01背包变形)
- poj 1837 Balance (dp,01背包)
- POJ 1837 Balance(01背包问题)
- 【POJ 1837】Balance(01背包)
- POJ 1837 Balance (DP-01背包)
- poj 1837 Balance(01背包)
- poj 1837 Balance--01背包
- poj 1837 Balance 01背包
- poj 1837 Balance (01背包)
- Python2.7 中使用64位OpenCV 3.2
- poj3281(#网络流最大流)
- FAAC源码阅读(1)——AAC和音频编码的介绍
- hdoj 2035
- 【Java并发编程实战】----- AQS(四):CLH同步队列
- POJ 1837-Balance(01背包-天平平衡)
- HTML DOM addEventListener() 方法
- IOS开发之OC篇(4)—— NSDictionary、NSMutableDictionary
- (@WhiteTaken)设计模式学习——简单工厂模式
- POJ 3393Lucky and Good Months by Gregorian Calendar(模拟)
- 高并发服务端分布式系统设计概要(下)
- Spring Cache原理与使用及ICOP平台中的缓存应用
- Uva1601 The Morning after Halloween【隐式图bfs+图重建】【例题7-9】
- Map遍历的四种方法