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

Big Event in HDU

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

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
 

题意:这个题是一个多重背包,b的最大容量为总价值/2;

网上提供的有用母函数或者是纯暴力的方式,将每一种可达状态标记成1,最后找最接近最大容量的值,例如第二个案例 可以只找小于最大容量的可能达到的状态,及10 20 30 40

这种思路应用到背包上面就是将多重背包转化成0 1背包,

•第i件物品可以分为q[i]件物品,价值分别为w[i], 2*w[i], 3*w[i], ……,费用分别为c[i], 2*c[i], 3*c[i], ……。

即把k个物品i合成一个物品

优化

•把k个物品i合成一个物品
•只保留k= 1, 2, 4, …, 2^(p-1), q[i]-2^p+1的物品
•其中p为满足q[i]-2^p+1>0的最大整数
•这样可以保证这些物品的组合能够取到从1到q[i]的所有值且肯定不会超过q[i]
但是多重背包有一个更快的模板
 #include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
#define N 55 int val;
int f[N*]; //每件物品只能使用一次
void onezeropack(int v,int c)
{
int j;
for(j=val; j>=v; j--)
{
f[j]=max(f[j-v]+c,f[j]);
}
}
//每件物品可以无限使用
void completepack(int v,int c)
{
int j;
for(j=v; j<=val; j++)
{
f[j]=max(f[j-v]+c,f[j]);
}
}
//每件物品有限次使用
void multiplepack(int v,int c,int num)
{
if(v*num>=val)
{
completepack(v,c);
return;
}
int k=;
while(k<num)
{
onezeropack(k*v,k*c);
num=num-k;
k=k*;
}
onezeropack(num*v,num*c);
} int v[N], num[N];
int main()
{
int n;
while(~scanf("%d", &n), n >= )
{
for(int i = ; i < n; i++) scanf("%d %d", &v[i], &num[i]);
val = ;
for(int i = ; i < n; i++) val += v[i]*num[i];
int tm = val;
val /= ;
memset(f, , sizeof(f));
for(int i = ; i < n; i++) multiplepack(v[i], v[i], num[i]);
printf("%d %d\n", tm-f[val], f[val]);
}
return ;
}

Big Event in HDU(多重背包套用模板)的更多相关文章

  1. 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 ...

  2. 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 ...

  3. hdu1171 Big Event in HDU(多重背包)

    http://acm.hdu.edu.cn/showproblem.php?pid=1171 多重背包题目不难,但是有些点不能漏或错. #include<iostream> #includ ...

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

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

  5. HDU1171:Big Event in HDU(多重背包分析)

    通过分析,要使A>=B并且差值最小.所以只要使sum/2的容量下,B最大就Ok了 #include<iostream> #include<cstdio> #include ...

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

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

  7. HDU 1171 Big Event in HDU dp背包

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

  8. hdu1171Big Event in HDU(01背包)

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

  9. 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 ...

随机推荐

  1. 【Zookeeper】源码分析目录

    Zookeeper源码分析目录如下 1. [Zookeeper]源码分析之序列化 2. [Zookeeper]源码分析之持久化(一)之FileTxnLog 3. [Zookeeper]源码分析之持久化 ...

  2. JavaScript的简单入门

    一.导读 简介:JavaScript简称js,是基于对象和事件驱动的脚本语言,主要运用于客户端.原名LiveScript,本身和Java没有任何关系,但语法上很类似. 特点:交互性(它可以做的就是信息 ...

  3. lesson - 3 ls /cd /path /alias /快捷键

    内容概要: 1. 命令ls -l   详细信息-a  查看隐藏的文件或目录-d   只看目录本身,不列出目录下面的文件和目录-t 以时间先后排序 2  目录结构/bin, /sbin, /usr/bi ...

  4. DES加解密、JavaScript、Java

    JavaScript代码    DES.js /** * Created by Andy on 2017/11/30. */ /** * DES加密/解密 * @Copyright Copyright ...

  5. csv文件转json

    http://stackoverflow.com/questions/19766266/directly-convert-csv-file-to-json-file-using-the-jackson ...

  6. Python爬虫通过替换http request header来欺骗浏览器实现登录

    以豆瓣为例,访问https://www.douban.com/contacts/list 来查看自己关注的人,要登录才能查看. 如果用requests.get()方法获取这个http,没登录只能抓取回 ...

  7. C# DataGridView 列的显示顺序

    this.dataGridView1.Columns["列名"].DisplayIndex=Convert.ToInt32("你要放置的位置")

  8. PAGELATCH_x 等待--转载

    转自出处:http://www.cnblogs.com/xwdreamer/archive/2012/08/30/2663232.html 0.参考文献 Microsoft SQL Server企业级 ...

  9. Android OpenGL ES 入门系列(二) --- 环境搭建

    转载请注明出处 本文出自Hansion的博客 本章介绍如何使用GLSurfaceView和GLSurfaceView.Renderer完成在Activity中的最简单实现. 1.在AndroidMan ...

  10. blog建表操作

    表思维导图:   数据库:表 from django.db import modelsfrom django.conf import settingsfrom django.contrib.auth. ...