Poj 3181 (dp)

本文介绍了一道经典的完全背包问题——DollarDays购物组合算法题。农民John在商店用有限的资金购买不同价格的工具,求解所有可能的购买组合。使用了高精度BigInteger来处理大数据情况。
Dollar Dayz
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 2912 Accepted: 1158

Description

Farmer John goes to Dollar Days at The Cow Store and discovers an unlimited number of tools on sale. During his first visit, the tools are selling variously for $1, $2, and $3. Farmer John has exactly $5 to spend. He can buy 5 tools at $1 each or 1 tool at $3 and an additional 1 tool at $2. Of course, there are other combinations for a total of 5 different ways FJ can spend all his money on tools. Here they are: 

        1 @ US$3 + 1 @ US$2

        1 @ US$3 + 2 @ US$1

        1 @ US$2 + 3 @ US$1

        2 @ US$2 + 1 @ US$1

        5 @ US$1
Write a program than will compute the number of ways FJ can spend N dollars (1 <= N <= 1000) at The Cow Store for tools on sale with a cost of $1..$K (1 <= K <= 100).

Input

A single line with two space-separated integers: N and K.

Output

A single line with a single integer that is the number of unique ways FJ can spend his money.

Sample Input

5 3

Sample Output

5

Source

dp入门题。看成完全背包即可,要用高精度。
import java.math.BigInteger;
import java.util.Scanner;

public class Main {

	static final int maxn = 1000 + 5;
	static final int maxk = 100 + 5;
	static BigInteger dp[][]=new BigInteger[maxk][maxn];

	static void solve() {
		for (int i = 0; i < maxk; i++) dp[i][0] = BigInteger.ONE;
		for (int i = 1; i < maxn; i++) dp[0][i] = BigInteger.ZERO;
		for (int i = 1; i < maxk; i++) {
			for (int j = 1; j < maxn; j++) {
				if (j >= i) dp[i][j] = dp[i - 1][j].add(dp[i][j - i]);
				else dp[i][j] = dp[i - 1][j];
			}
		}
	}

	public static void main(String[] args) {
		Scanner cin = new Scanner(System.in);
		int n, k;
		solve();
		while (cin.hasNext()) {
			n = cin.nextInt();
			k = cin.nextInt();
			System.out.println(dp[k][n]);
		}
	}

}


### 关于拔河问题的动态规划实现 拔河问题是经典的 **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]。 另外需要注意的是边界情况以及整型溢出等问题,确保所有变量范围适当设置以容纳可能出现的最大数值。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值