poj 1837 dp有点类似背包

本文解决了一个特殊的天平平衡问题,通过动态规划算法寻找所有可能的平衡方式。输入包括天平的钩子分布和一系列砝码的重量,输出则是天平可以达到平衡的不同方式的数量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://poj.org/problem?id=1837

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
题目:给出力臂和各种砝码,求使得天平平衡的方法,要求砝码必须全部用完。

/**
dp[i][j],表示前i个砝码的位置已经确定后,平行点为j的方案数
首先,dp[0][0]=1;转移方程为 dp[i][j+w[i]*pos[k]]+=dp[i-1][j];前提是(dp[i-1][j]!=0)
若 j<0则天平左倾,j>0则天平右倾。最后dp[m][0],即为答案。
又因为数组的下标是不能出现负数的,因此我们刚开始把平衡点看做10000.即dp[0][10000]=1;
因为即使把所有的砝码全部都加在最左面也不过是:10000-20*25*15=2500仍然是整数。
详见代码
*/

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;

int dp[25][20000],w[25],pos[25];
int n,m;

int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        for(int i=1;i<=n;i++)
            scanf("%d",&pos[i]);
        for(int i=1;i<=m;i++)
            scanf("%d",&w[i]);
        memset(dp,0,sizeof(dp));
        dp[0][10000]=1;
        for(int i=1;i<=m;i++)
            for(int j=0;j<=20000;j++)
                if(dp[i-1][j])
                    for(int k=1;k<=n;k++)
                        dp[i][j+w[i]*pos[k]]+=dp[i-1][j];
        printf("%d\n",dp[m][10000]);
    }
    return 0;
}


### 关于拔河问题的动态规划实现 拔河问题是经典的 **0/1 背包变种问题**,其核心目标是将一群人分成两队,使得每队的人数最多相差 1,并且两队的体重总和尽可能接近。此问题可以通过动态规划 (Dynamic Programming, DP) 来解决。 #### 动态规划的核心思路 该问题可以转化为一个子集划分问题:给定一组重量 \( w_1, w_2, \ldots, w_n \),找到两个子集 \( A \) 和 \( B \),满足以下条件: 1. 子集 \( A \) 的权重之和与子集 \( B \) 尽可能接近。 2. 如果总人数为奇数,则其中一个子集多一个人;如果总人数为偶数,则两者人数相等。 通过定义状态转移方程来解决问题。设 \( S \) 是所有人重量的总和,\( half = S / 2 \) 表示一半的重量。我们尝试寻找不超过 \( half \) 的最大子集重量 \( sum_A \),从而另一部分的重量自然就是 \( sum_B = S - sum_A \)[^1]。 #### 实现细节 以下是基于动态规划的具体算法描述: 1. 定义数组 `dp`,其中 `dp[i]` 表示是否存在一种组合方式使其重量恰好等于 \( i \)。 2. 初始化 `dp[0] = true`,表示重量为零的情况总是可行。 3. 遍历每个人的重量 \( w_i \),更新 `dp` 数组的状态。 4. 找到最大的 \( j \leq half \) 并使 `dp[j] == true` 成立,此时 \( j \) 即为一侧的最大重量 \( sum_A \)。 下面是具体的代码实现: ```cpp #include <iostream> #include <vector> using namespace std; int main() { int n; while(cin >> n && n != 0){ vector<int> weights(n); int total_weight = 0; for(int &w : weights){ cin >> w; total_weight += w; } int half = total_weight / 2; vector<bool> dp(half + 1, false); // dp[i] means whether weight 'i' is achievable. dp[0] = true; for(auto w : weights){ for(int j = half; j >= w; --j){ if(dp[j - w]){ dp[j] = true; } } } // Find the largest possible value less than or equal to half int closest_sum = 0; for(int j = half; j >= 0; --j){ if(dp[j]){ closest_sum = j; break; } } cout << min(closest_sum, total_weight - closest_sum) << " " << max(closest_sum, total_weight - closest_sum) << endl; } } ``` 上述程序实现了如何利用动态规划求解拔河问题中的最优分配方案[^2]。 #### 常见错误分析 对于 POJ 和 UVa 上的不同表现,可能是由于输入处理上的差异所致。UVa 版本通常涉及多组测试数据,而 POJ 可能仅限单组输入。因此,在提交至 UVa 时需注意循环读取直到文件结束标志 EOF 出现为止[^3]。 另外需要注意的是边界情况以及整型溢出等问题,确保所有变量范围适当设置以容纳可能出现的最大数值。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值