Big Event in HDU -背包问题
来源:互联网 发布:linux php 目录权限 编辑:程序博客网 时间:2024/06/03 13:25
Big 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
210 120 1310 1 20 230 1-1
Sample Output
20 1040 40
题意:有N种物品,每种物品的价值和重量分别是V,M。平分这些物品。
把此多重背包问题转化为01背包问题。背包限制最多只能装sum/2的量。虽然时间长,但是也可以通过的。
#include <bits/stdc++.h>using namespace std;int N,Q;int M;int V;//背包最大容量int value[5005];//物品价值int f[255555];int ZeroOnePack(){ memset(f,0,sizeof(f)); //递推 for (int i = 0;i < Q;i++) //枚举物品(注意物品的编号) { for (int j = V;j >= value[i];j--) { f[j] = max(f[j],f[j - value[i]] + value[i]); //cout << j << " " << f[j] << endl; } } return f[V];}int main(){ int v,sum; while(~scanf("%d",&N)) { if(N<0)break; Q=0; sum=0; for(int i=0;i<N;i++) { scanf("%d%d",&v,&M); while(M--) { value[Q++]=v; sum=sum+v; } V=sum/2; } printf("%d %d\n",sum-ZeroOnePack(),ZeroOnePack()); } return 0;}
- Big Event in HDU(多重背包问题)
- Big Event in HDU+完全背包问题
- Big Event in HDU -背包问题
- Big Event in HDU 背包
- HDU 1171(Big Event in HDU)0-1背包问题
- hdu 1171 Big Event in HDU 多重背包问题
- HDU 1171 Big Event in HDU(01背包问题)
- Big Event in HDU(多重背包问题)
- HDU Big Event in HDU - 多重背包
- [HDU 1171 Big Event in HDU]背包
- HDOJ Big Event in HDU 多重背包
- Big Event in HDU (多重背包)
- Big Event in HDU hdu1171 多重背包
- Big Event in HDU(多重背包)
- HDU1171:Big Event in HDU(01背包)
- HDU1171:Big Event in HDU(多重背包)
- Big Event in HDU 多重背包~
- hdu1171 Big Event in HDU (多重背包)
- 什么是webService
- 输出顺序表某一位置的值
- git
- Mac跑tensorflow版SSD
- KCF
- Big Event in HDU -背包问题
- 负载均衡集群企业级应用实战:LVS-DR模式
- 7种查找算法解析
- Android面试相关之基础知识
- mysql启动和创建用户
- 彻底搞懂 字符 编码 GBK 和 UTF8
- 大话设计模式----迪米特法则
- 中文乱码问题的各种对应的解决方案
- 顺序表的前驱与后继