HDU - 1171 Big Event in HDU 多重背包
B - Big Event in HDU
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
A test case starting with a negative integer terminates input and this test case is not to be processed.
Output
Sample Input
2
10 1
20 1
3
10 1
20 2
30 1
-1
Sample Output
20 10
40 40 多重背包问题,可以将问题转化成01背包。两种思路:将多个相同物品拆分成一个一个价值相同的不同物品;也可以在01背包递推时加一层物品个数的循环。
注意这题的坑点!是以一个负整数作为结束,不要想当然以为是-1。因为这个TLE了好久。。以后要认真读题
ps:negative integer负整数 positive integer正整数
//第一种写法,耗时1248ms
#include<stdio.h>
#include<string.h> int f[],a[]; int max(int x,int y)
{
return x>y?x:y;
} int main()
{
int n,V,sum,c,x,y,i,j;
while(scanf("%d",&n)&&n>=){
memset(f,,sizeof(f));
memset(a,,sizeof(a));
sum=;c=;
for(i=;i<=n;i++){
scanf("%d%d",&x,&y);
while(y--){
a[++c]=x;
sum+=x;
}
}
V=sum/;
for(i=;i<=c;i++){
for(j=V;j>=a[i];j--){
f[j]=max(f[j],f[j-a[i]]+a[i]);
}
}
printf("%d %d\n",sum-f[V],f[V]);
}
return ;
}
//第二种写法,耗时811ms
#include<stdio.h>
#include<string.h> int f[],a[],b[]; int max(int x,int y)
{
return x>y?x:y;
} int main()
{
int n,V,sum,i,j,k;
while(scanf("%d",&n)&&n>=){
memset(f,,sizeof(f));
memset(a,,sizeof(a));
memset(b,,sizeof(b));
sum=;
for(i=;i<=n;i++){
scanf("%d%d",&a[i],&b[i]);
sum+=a[i]*b[i];
}
V=sum/;
for(i=;i<=n;i++){
for(k=;k<=b[i];k++){
for(j=V;j>=;j--){
if(j-a[i]>=){
f[j]=max(f[j],f[j-a[i]]+a[i]);
}
}
}
}
printf("%d %d\n",sum-f[V],f[V]);
}
return ;
}
HDU - 1171 Big Event in HDU 多重背包的更多相关文章
- 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 ...
- HDU 1171 Big Event in HDU (多重背包)
Big Event in HDU Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- HDU 1171 Big Event in HDU (多重背包变形)
Big Event in HDU Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- 题解报告:hdu 1171 Big Event in HDU(多重背包)
Problem Description Nowadays, we all know that Computer College is the biggest department in HDU. Bu ...
- HDU 1171 Big Event in HDU(多重背包)
Big Event in HDU Problem Description Nowadays, we all know that Computer College is the biggest depa ...
- hdu 1171 Big Event in HDU(多重背包+二进制优化)
题目链接:hdu1171 思路:将多重背包转为成完全背包和01背包问题,转化为01背包是用二进制思想,即件数amount用分解成若干个件数的集合,这里面数字可以组合成任意小于等于amount的件数 比 ...
- HDU 1171 Big Event in HDU dp背包
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s ...
- 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) T ...
- HDU 1171 Big Event in HDU(01背包)
题目地址:HDU 1171 还是水题. . 普通的01背包.注意数组要开大点啊. ... 代码例如以下: #include <iostream> #include <cstdio&g ...
随机推荐
- 九度OJ 1079:手机键盘 (翻译)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:2279 解决:1233 题目描述: 按照手机键盘输入字母的方式,计算所花费的时间 如:a,b,c都在"1"键上,输入a只 ...
- 九度OJ 1057:众数 (基础题)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:8431 解决:2819 题目描述: 输入20个数,每个数都在1-10之间,求1-10中的众数(众数就是出现次数最多的数,如果存在一样多次数的 ...
- 九度OJ 1020:最小长方形 (基础题)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:6019 解决:2849 题目描述: 给定一系列2维平面点的坐标(x, y),其中x和y均为整数,要求用一个最小的长方形框将所有点框在 ...
- Linux系统中的python2.x升级至python3
python2.x升级至python3 由于需要在公司测试环境搭建的Jenkins上面运行接口自动化脚本,所以需要对Jenkins所处的服务器的python环境进行升级,以便运行python3编写的脚 ...
- BZOJ4920: [Lydsy1706月赛]薄饼切割
BZOJ4920: [Lydsy1706月赛]薄饼切割 Description 有一天,tangjz送给了quailty一张薄饼,tangjz将它放在了水平桌面上,从上面看下去,薄饼形成了一个H*W的 ...
- 小记tensorflow-1:tf.nn.conv2d 函数介绍
tf.nn.conv2d函数介绍 Input: 输入的input必须为一个4d tensor,而且每个input的格式必须为float32 或者float64. Input=[batchsize,im ...
- redis的持久化RDB与AOF
redis 持久化 Redis是一种内存型数据库,一旦服务器进程退出,数据库的数据就会丢失,为了解决这个问题,Redis提供了两种持久化的方案,将内存中的数据保存到磁盘中,避免数据的丢失. RDB ...
- Java多线程系列 基础篇04 线程中断
1. 中断线程 中断可以理解为线程的一个标志位属性,它表示一个运行中的线程是否被其他线程进行了中断操作,其他线程通过调用该线程的interrupt()方法对其进行中断操作,线程通过检查自身是否被中断来 ...
- wamp server 安装后 Apache80端口占用
提示:Your port 80 is actually used by :Server: Microsoft-HTTPAPI/2.0 解决方案:计算机->右键管理->服务和应用程序-> ...
- POJ1625 Censored! —— AC自动机 + DP + 大数
题目链接:https://vjudge.net/problem/POJ-1625 Censored! Time Limit: 5000MS Memory Limit: 10000K Total S ...