hdu(1171)

Big Event in HDU

Time Limit:

10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 19752    Accepted Submission(s): 6900
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
分析:多重背包问题,先计算总价值sum,然后取其一半V=sum/2;剩下的就是多重背包问题了
程序:
#include"stdio.h"
#include"string.h"
int f[250005];
int p[100],h[100];
int max(int a,int b)
{
return a>b?a:b;
}
int main()
{
int n,i,j,k,sum,V;
while(scanf("%d",&n),n>=0)
{
sum=0;
for(i=1;i<=n;i++)
{
scanf("%d%d",&p[i],&h[i]);
sum+=p[i]*h[i];
}
V=sum/2;
for(i=0;i<=V;i++)
f[i]=0;
for(i=1;i<=n;i++)
{
for(j=V;j>=p[i];j--)
{
for(k=0;k<=h[i]&&k*p[i]<=j;k++)
f[j]=f[j]>f[j-k*p[i]]+k*p[i]?f[j]:f[j-k*p[i]]+k*p[i]; }
}
printf("%d %d\n",sum-f[V],f[V]);
}
return 0;
}
运行时间2309ms
还有一种可快速的方法就是利用二进制混合背包:
程序:
#include"string.h"
#include"stdio.h"
int f[250020],V;
void zopack(int v,int p)
{
int i;
for(i=V;i>=v;i--)
f[i]=f[i]>f[i-v]+p?f[i]:f[i-v]+p;
}
void allpack(int v,int p)
{
int i;
for(i=v;i<=V;i++)
f[i]=f[i]>f[i-v]+p?f[i]:f[i-v]+p; }
void multiplepack(int v,int p,int h)
{
if(v*h>=V)
{
allpack(v,p);
return;
}
int k=1;
while(k<h)
{
zopack(k*v,k*p);
h-=k;
k<<=1;
}
if(h>0)
{
zopack(h*v,h*p);
}
}
int main()
{
int n,i,v[111],h[111];
while(scanf("%d",&n),n>=0)
{
int sum=0;
for(i=0;i<n;i++)
{
scanf("%d%d",&v[i],&h[i]);
sum+=v[i]*h[i];
}
V=sum/2;
for(i=0;i<=V;i++)
f[i]=-999;
f[0]=0;
for(i=0;i<n;i++)
{
multiplepack(v[i],v[i],h[i]);
}
int ans=-1;
for(i=V;i>=0;i--)
{
if(f[i]>=0)
{
ans=f[i];
break;
}
}
printf("%d %d\n",sum-ans,ans);
}
return 0;
}

时间只用31~64ms

hdu(1171)多重背包的更多相关文章

  1. Big Event in HDU(HDU 1171 多重背包)

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

  2. hdu 1171 多重背包

    题意:给出价值和数量,求能分开的最近的两个总价值,例如10,20*2,30,分开就是40,40 链接:点我 #include<cstdio> #include<iostream> ...

  3. hdu 5445 多重背包

    Food Problem Time Limit: 3000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)To ...

  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. 题解报告:hdu 1171 Big Event in HDU(多重背包)

    Problem Description Nowadays, we all know that Computer College is the biggest department in HDU. Bu ...

  6. hdu 1171 Big Event in HDU(多重背包+二进制优化)

    题目链接:hdu1171 思路:将多重背包转为成完全背包和01背包问题,转化为01背包是用二进制思想,即件数amount用分解成若干个件数的集合,这里面数字可以组合成任意小于等于amount的件数 比 ...

  7. hdu 2191 多重背包 悼念512汶川大地震遇难同胞——珍惜现在,感恩生活

    http://acm.hdu.edu.cn/showproblem.php?pid=2191 New~ 欢迎“热爱编程”的高考少年——报考杭州电子科技大学计算机学院关于2015年杭电ACM暑期集训队的 ...

  8. Big Event in HDU(多重背包套用模板)

    http://acm.hdu.edu.cn/showproblem.php?pid=1171 Big Event in HDU Time Limit: 10000/5000 MS (Java/Othe ...

  9. HDU1171--Big Event in HDU(多重背包)

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

  10. hdu 1059 (多重背包) Dividing

    这里;http://acm.hdu.edu.cn/showproblem.php?pid=1059 题意是有价值分别为1,2,3,4,5,6的商品各若干个,给出每种商品的数量,问是否能够分成价值相等的 ...

随机推荐

  1. IO流学习小结

    今天刚刚看完java的io流操作,把主要的脉络看了一遍,不能保证以后使用时都能得心应手,但是最起码用到时知道有这么一个功能可以实现,下面对学习进行一下简单的总结: IO流主要用于硬板.内存.键盘等处理 ...

  2. shell常用代码

    grep -lr 'hello' /usr/share/* #在/usr/share目录下查找包含hello的字符串 NOW_DATE=`date "+%Y%m%d%H%M%S"` ...

  3. appium关于定位元素

    Windows上定位元素我用的是uiautomatorviewer 这个工具在你的SDK-tools目录下,点击uiautomatorviewer.bat启动,注意appium在跑的时候是取不到的 工 ...

  4. django GET POST

    django需要读取客户端get和post请求的值.读取处理方法和异常记录于此. 参考链接: http://stackoverflow.com/questions/12518517/request-p ...

  5. windows远程桌面连接树莓派

    1.树莓派上需要安装xrdp(An open source remote desktop protocol(rdp) server). sudo apt-get install xrdp 远程桌面打开 ...

  6. LeetCode Shortest Word Distance II

    原题链接在这里:https://leetcode.com/problems/shortest-word-distance-ii/ 题目: This is a follow up of Shortest ...

  7. iOS:app直播---原理篇

    [如何快速的开发一个完整的iOS直播app](原理篇) 转载自简书@袁峥Seemygo:http://www.jianshu.com/p/7b2f1df74420   一.个人见解(直播难与易) 直播 ...

  8. JavaScript:复选框事件的处理

    复选框事件的处理 复选框本身也是多个组件的名字相同.所以在定义复选框的同事依然要使用document.all()取得全部的内容. 范例:操作复选框,要求是可以一个个去选择选项,也可以直接全选,全选按钮 ...

  9. saltstack之(十二)配置管理mount

    线上很多服务器都需要挂载存储上的公共目录,并实现开机启动(/etc/fstab),比如web的静态文件共享目录,日志远程集中收集等. 一.批量挂载部分. 1.在node1上配置nfs服务器,有关nfs ...

  10. svn使用dump和hotcopy进行脚本备份

    [root@svn shell]# cat repolist  dev-arcdev-bmdev-crmdev-paydev-pmdev-portaldev-riskhrproductqarep-op ...