hdu——1171Big Event in HDU


Problem Description
Nowadays, we all know that Computer College is the biggest department in HDU. But, maybe you don't know that Computer College had ever been split into Computer College and Software College in 2002.
The splitting is absolutely a big event in HDU! At the same time, it is a trouble thing too. All facilities must go halves. First, all facilities are assessed, and two facilities are thought to be same if they have the same value. It is assumed that there is N (0<N<1000) kinds of facilities (different value, different kinds).
 

Input
Input contains multiple test cases. Each test case starts with a number N (0 < N <= 50 -- the total number of different facilities). The next N lines contain an integer V (0<V<=50 --value of facility) and an integer M (0<M<=100 --corresponding number of the facilities) each. You can assume that all V are different.
A test case starting with a negative integer terminates input and this test case is not to be processed.
 

Output
For each case, print one line containing two integers A and B which denote the value of Computer College and Software College will get respectively. A and B should be as equal as possible. At the same time, you should guarantee that A is not less than B.
 

Sample Input
2 10 1 20 1 3 10 1 20 2 30 1 -1
 

Sample Output
20 10 40 40
 

给你价值,数目,分成2部分,但是差值尽量少,多重背包来解决,
//多重背包

#include<stdio.h>
#include<string.h>

int dp[250010];
int V;
int num[100],cost[100];

int max(int a,int b)
{
return a>b?a:b;
}

int min(int a,int b)
{
return a<b?a:b;
}

void zero_one_package(int cost,int weight)
{
for(int v=V;v>=cost;v--)
dp[v]=max(dp[v],dp[v-cost]+weight);
}

void complete_package(int cost,int weight)
{
for(int v=cost;v<=V;v++)
dp[v]=max(dp[v],dp[v-cost]+weight);
}

void multiple_package(int cost ,int weight,int amount)
{
if(cost * amount >= V)
{
complete_package(cost,weight);
return ;
}
int k=1;
while(k<amount)
{
zero_one_package(k*cost,k*weight);
amount-=k;
k<<=1;
}
zero_one_package(amount*cost,amount*weight);
}

int main()
{
int n;
while(~scanf("%d",&n))
{
if(n<0)
break;
int sum=0;
for(int i=0;i<n;i++)
{
scanf("%d%d",&cost[i],&num[i]);
sum+=cost[i]*num[i];
}
V=sum;
memset(dp,0,sizeof(dp));
for(int i=0;i<n;i++)
{
if(num[i]>1)
multiple_package(cost[i],cost[i],num[i]);
else
zero_one_package(cost[i],cost[i]);
}
int a=max(dp[V/2],sum-dp[V/2]);
int b=min(dp[V/2],sum-dp[V/2]);
printf("%d %d\n",a,b);
}
return 0;
}


### HDU OJ Problem 2566 Coin Counting Solution Using Simple Enumeration and Generating Function Algorithm #### 使用简单枚举求解硬币计数问题 对于简单的枚举方法,可以通过遍历所有可能的组合方式来计算给定面额下的不同硬币组合数量。这种方法虽然直观但效率较低,在处理较大数值时性能不佳。 ```java import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int[] coins = {1, 2, 5}; // 定义可用的硬币种 while (scanner.hasNext()) { int targetAmount = scanner.nextInt(); int countWays = findNumberOfCombinations(targetAmount, coins); System.out.println(countWays); } } private static int findNumberOfCombinations(int amount, int[] denominations) { if (amount == 0) return 1; if (amount < 0 || denominations.length == 0) return 0; // 不使用当前面值的情况 int excludeCurrentDenomination = findNumberOfCombinations(amount, subArray(denominations)); // 使用当前面值的情况 int includeCurrentDenomination = findNumberOfCombinations(amount - denominations[0], denominations); return excludeCurrentDenomination + includeCurrentDenomination; } private static int[] subArray(int[] array) { if (array.length <= 1) return new int[]{}; return java.util.Arrays.copyOfRange(array, 1, array.length); } } ``` 此代码实现了通过递归来穷尽每一种可能性并累加结果的方式找到满足条件的不同组合数目[^2]。 #### 利用母函数解决硬币计数问题 根据定义,可以将离散序列中的每一个元素映射到幂级数的一个项上,并利用这些多项式的乘积表示不同的组合情况。具体来说: 设 \( f(x)=\sum_{i=0}^{+\infty}{a_i*x^i}\),其中\( a_i \)代表当总金额为 i 时能够组成的方案总数,则有如下表达式: \[f_1(x)=(1+x+x^2+...)\] 这实际上是一个几何级数,其封闭形式可写作: \[f_1(x)=\frac{1}{(1-x)}\] 同理,对于其他类型的硬币也存在似的生成函数。因此整个系统的生成函数就是各个单独部分之积: \[F(x)=f_1(x)*f_2(x)...*f_n(x)\] 最终目标是从 F(x) 中提取系数即得到所需的结果。下面给出基于上述理论的具体实现: ```cpp #include<iostream> using namespace std; const int MAXN = 1e4 + 5; int dp[MAXN]; void solve() { memset(dp, 0, sizeof(dp)); dp[0] = 1; // 初始化基础状态 int values[] = {1, 2, 5}, size = 3; for (int j = 0; j < size; ++j){ for (int k = values[j]; k <= 10000; ++k){ dp[k] += dp[k-values[j]]; } } } int main(){ solve(); int T; cin >> T; while(T--){ int n; cin>>n; cout<<dp[n]<<endl; } return 0; } ``` 这段 C++ 程序展示了如何应用动态规划技巧以及生成函数的概念高效地解决问题实例[^1]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值