博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Problem S
阅读量:7223 次
发布时间:2019-06-29

本文共 1752 字,大约阅读时间需要 5 分钟。

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

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
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
题意:给你n中给定数量的不同价值的东西,问怎么样分才能使得两堆的差最小;
解题思路:动态规划中平分东西问题有一个模板:将它所给出的n个东向西加起来sum,将sum/2当作体积,求出在sum/2下的最大值,sum-dp[sum/2];
感悟:01背包问题不能看的太简单,其衍生出来的东西太多了;
代码:
#include
#include
#include
#define maxn 255555
using namespace std;
int main()
{
    //freopen("in.txt","r",stdin);
    int n,m,v,f[maxn],dp[maxn];
    while(~scanf("%d",&n),n>0)
    {
        memset(dp,0,sizeof dp);
        int d=0,sum=0;
        for(int l=0;l
        {
            scanf("%d%d",&v,&m);
            for(int i=0;i
            {
                f[d++]=v;
                sum+=v;
            }//将数据记录到数组,并且求出虽多的价值数
        }
        for(int i=0;i
            for(int j=sum/2;j>=f[i];j--)
        {
            dp[j]=max(dp[j],dp[j-f[i]]+f[i]);
        }
        printf("%d %d\n",sum-dp[sum/2],dp[sum/2]);
    }
    return 0;
}

转载于:https://www.cnblogs.com/wuwangchuxin0924/p/5781550.html

你可能感兴趣的文章
知道各个层使用的是哪个数据交换设备
查看>>
iOS:在cell中使用倒计时的最佳方法
查看>>
带有Apache Spark的Lambda架构
查看>>
分布式系列 - dubbo服务telnet命令【转】
查看>>
分布式高并发物联网(车联网-JT808协议)平台架构方案
查看>>
SQL Server 如何设置数据库的默认初始大小和自动增长大小
查看>>
cmd.exe启动参数详解
查看>>
Spring 注解<context:annotation-config> 和 <context:component-scan>的作用与区别
查看>>
混合开发 Hybird Cordova PhoneGap web 跨平台 MD
查看>>
高效沟通的秘籍-沟通视窗
查看>>
怎么创建SpringBoot项目
查看>>
RabbitMQ 延迟队列实现订单支付结果异步阶梯性通知
查看>>
基于Elastalert的安全告警剖析
查看>>
SQL中ON和WHERE的区别
查看>>
art.template 循环里面分组。
查看>>
[Algorithms] Solve Complex Problems in JavaScript with Dynamic Programming
查看>>
MetaModelEngine:约束和验证
查看>>
垂直居中层 js操作css
查看>>
c_str 以及atoi
查看>>
Wrox红皮书上市十周年 惊喜馈赠读者
查看>>