题目:http://acm.hdu.edu.cn/showproblem.php?pid=1171

题意:把商品分成两半,如不能均分,尽可能的让两个数相接近。输出结果:两个数字a,b且a>=b。

思路:01背包。

先把商品的总价值计算出来,sum/2做为背包的容量。

然后讲同种商品的多件,存储为不同商品 同样价值的形式,也就是我们用一个一维数组来存储,不用一个二维或是两个一维数组来存。

感想:好久没有做背包的题目了,今天来做,忘了好多思路,这提醒着我,学习不能一直都在学新东西,也要及时的复习。

复习01背包模板:

//当val==v;

for(i=;i<h;i++)
for(j=val;j>=val[i];j--)
dp[j]=max(dp[j],dp[j-val[i]]+val[i]);
//val!=w; for(int i=;i<n;i++) for(int j=v;j>=w[i];j--) dp[j]=max(dp[j],dp[j-w[i]]+val[i]);

Code:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std; int val[];
int dp[]; int main()
{
int n,i,j,a,b,h,sum;
while(cin>>n&&n>)
{
memset(val,,sizeof(val));
memset(dp,,sizeof(dp));
sum=;
h=;
for(i=;i<n;i++)
{
cin>>a>>b;
while(b--)
{
val[h++]=a;
sum+=a;
}
}
for(i=;i<h;i++)
for(j=sum/;j>=val[i];j--)
dp[j]=max(dp[j],dp[j-val[i]]+val[i]);
cout<<sum-dp[sum/]<<" "<<dp[sum/]<<endl;
}
return ;
}

1171 Big Event in HDU 01背包的更多相关文章

  1. HDU 1171 Big Event in HDU(01背包)

    题目地址:HDU 1171 还是水题. . 普通的01背包.注意数组要开大点啊. ... 代码例如以下: #include <iostream> #include <cstdio&g ...

  2. hdu 1171 Big Event in HDU (01背包, 母函数)

    Big Event in HDU Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  3. HUD 1171 Big Event in HDU(01背包)

    Big Event in HDU Problem Description Nowadays, we all know that Computer College is the biggest depa ...

  4. HDU 1171 Big Event in HDU(01背包)

    题目链接 题意:给出n个物品的价值v,每个物品有m个,设总价值为sum,求a,b.a+b=sum,且a尽可能接近b,a>=b. 题解:01背包. #include <bits/stdc++ ...

  5. HDU 1171 Big Event in HDU 多重背包二进制优化

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1171 Big Event in HDU Time Limit: 10000/5000 MS (Jav ...

  6. HDU 1171 Big Event in HDU dp背包

    Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s ...

  7. HDU - 1171 Big Event in HDU 多重背包

    B - Big Event in HDU Nowadays, we all know that Computer College is the biggest department in HDU. B ...

  8. HDU 1171 Big Event in HDU (多重背包变形)

    Big Event in HDU Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  9. hdu1171Big Event in HDU(01背包)

    Big Event in HDU Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

随机推荐

  1. hdu3586 树形dp+二分答案

    /* dp[i]表示孤立i结点的费用,二分功率上限w,即dp[i]在选择时不可以选择功率大于w的边 */ #include<bits/stdc++.h> using namespace s ...

  2. Spring Boot的Listener机制的用法和实现原理详解

    之前在介绍了在spring-boot启动过程中调用runner的原理,今天我们介绍另外一种可以实现相似功能的机制:spring-boot的Listener机制. 通过注册Listener,可以实现对于 ...

  3. js获取url协议、url, 端口号等信息路由信息

    以路径为 http://www.baidu.com  为例 console.log("location:"+window.location.href); >> &quo ...

  4. CSS预处理器—Sass、LESS和Stylus

    http://www.w3cplus.com/css/css-preprocessor-sass-vs-less-stylus-2.html 一.什么是CSS预处器 CSS预处理器定义了一种新的语言, ...

  5. 设置git记住用户和密码

     git config --global credential.helper store

  6. Redis的搭建和Redis的集群搭建

    1.Redis的官网:https://redis.io/      Redis的测试网站:http://try.redis.io/ 2.参考博客:https://www.cnblogs.com/maf ...

  7. Sql与C#中日期格式转换总结

    SQL中的转换方法: 一.将string转换为datetime,主要是使用Convert方法, 方法,Convert(datetime [ ( length ) ] , expression, [st ...

  8. 通过ping命令查看服务器是linux还是windows系列

    通过ping命令识别服务器类型

  9. User模型扩展和自定义

    参考如下: django文档参考 django signal使用总结 django 信号注册 django信号问题1 django oneTooneFiled     1. django 自定义用户u ...

  10. netty01(长短连接、java)

    使用netty需要添加依赖包 netty版本:netty-5.0.0.Alpha2 http://files.cnblogs.com/files/applerosa/netty-5.0.0.Alpha ...