B - Big Event in HDU

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 多重背包问题,可以将问题转化成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 ;
}

最新文章

  1. DotNet的JSON序列化与反序列化
  2. maven clean deploy -Pproduction
  3. 深入理解javascript作用域系列第一篇——内部原理
  4. AngularJs angular.forEach、angular.extend
  5. Python之路-python(面向对象进阶(模块的动态导入、断言、Socket Server))
  6. [AYUI]QQ管家源码已经开源
  7. 【转】 iOS开发UI篇—控制器的View的创建
  8. hdu 1111 Secret Code
  9. cocos2d-x游戏开发系列教程-超级玛丽02-代码结构
  10. fast-json.jar的用法
  11. Python - Headless Selenium WebDriver Tests using PyVirtualDisplay
  12. SmartAdmin(SmartAdmin_v1.5.2)
  13. Python内置方法的时间复杂度
  14. zabbix3.2利用自动发现功能对fastcgi模式的php状态进行集中监控
  15. 新特技软件(Analyzer)添加新用户
  16. gotty---用来作为k8s的web terminal,通过参数读取指定pod的日志输出
  17. stark组件开发之分页
  18. 【hexo】01安装
  19. 重温httpsession①
  20. mysql定时删除数据

热门文章

  1. ThreadLocal的简单使用
  2. 注册HttpHandler
  3. 【题解】At2370 Piling Up
  4. sourceSet
  5. 可视化工具与pymongo
  6. HBase存储方案设计
  7. property 中的strong 与weak
  8. P3968 [TJOI2014]电源插排
  9. 获取HDC的几种方法
  10. Linux学习之路(五)压缩命令