练习三1019

Problem S

Time Limit : 10000/5000ms (Java/Other)   Memory Limit :65536/32768K (Java/Other)

Problem Description

Nowadays, we allknow that Computer College is the biggest department in HDU. But, maybe youdon't know that Computer College had ever been split into Computer College andSoftware College in 2002.<br>The splitting is absolutely a big event inHDU! At the same time, it is a trouble thing too. All facilities must gohalves. First, all facilities are assessed, and two facilities are thought tobe same if they have the same value. It is assumed that there is N(0&lt;N&lt;1000) kinds of facilities (different value, differentkinds).<br>

 

 

Input

Input containsmultiple test cases. Each test case starts with a number N (0 < N <= 50-- the total number of different facilities). The next N lines contain aninteger 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 aredifferent.<br>A test case starting with a negative integer terminatesinput and this test case is not to be processed.<br>

 

 

Output

For each case,print one line containing two integers A and B which denote the value ofComputer College and Software College will get respectively. A and B should beas equal as possible. At the same time, you should guarantee that A is not lessthan B.<br>

 

 

Sample Input

2

10 1

20 1

3

10 1

20 2

30 1

-1

 

 

Sample Output

20 10

40 40

 

 

题意:求把总价值分为两个数,使这两个数接近相等,而且这两个数必须由所有设备中的其中几种设备价值构成,并先输出比较大的数,再输出另一个数。

 

解题思路:DP算法,背包问题,求法是先求出总价值sum,再用dp[]求sum/2最多能放多少价值!即可以求出其中一个数了,另一个就是sum-dp[sum/2]了。

状态:f[j]:表示软件学院取得容量为j时能获得的最大值,j<=sum/2;

状态转移:f[j]=max{f[j],f[j-v[i]]+v[i]}

注意数组的大小(0<n<=50,0<v<=50,0<m<=100)

 

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
#define MAX 300000
int dp[MAX],v[1000],m[1000];
int main()
{
   int i,j,n,k;
   while(cin>>n&&n>=0)
   {
       int sum=0;
       for(i=0;i<n;i++)
       {
           cin>>v[i]>>m[i];
           sum+=v[i]*m[i];
       }
           memset(dp,0,sizeof(dp));
           for(i=0;i<n;i++)
               for(j=1;j<=m[i];j++)
                   for(k=sum/2;k>=v[i]*j;k--)
                       if(dp[k]<dp[k-v[i]]+v[i])
                           dp[k]=dp[k-v[i]]+v[i];
           if(sum-dp[sum/2]>dp[sum/2])
               cout<<sum-dp[sum/2]<<" "<<dp[sum/2]<<endl;
           else cout<<dp[sum/2]<<" "<<sum-dp[sum/2]<<endl;
   }
  return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值