3019
来源:互联网 发布:湖南海龙网络 编辑:程序博客网 时间:2024/06/06 05:42
3019
Problem S
Time Limit : 10000/5000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 82 Accepted Submission(s) : 31
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.<br>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).<br>
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.<br>A test case starting with a negative integer terminates input and this test case is not to be processed.<br>
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.<br>
Sample Input
210 120 1310 1 20 230 1-1
Sample Output
20 1040 40
题意:
有N种物品,知道价值和数量,若尽量分成两份,两份对应的价值,先输出大的,然后输出小的
思路:
要求分成两份,那么只有两种情况
1、一大一小
2 一样大
所以对于一大一小那种情况中小的那个最大只能是总和的一半。
这样子就可以把完全背包转换成一个0-1背包问题
AC代码:
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
int val[5005];
int dp[255555];
int main()
{
int n,i,j,a,b,l,sum;
while(~scanf("%d",&n),n>0)
{
memset(val,0,sizeof(val));
memset(dp,0,sizeof(dp));
l = 0;
sum = 0;
for(i = 0;i<n;i++)
{
scanf("%d%d",&a,&b);
while(b--)
{
val[l++] = a;//将价值存入数组
sum+=a;
}
}
for(i = 0;i<l;i++)
{
for(j = sum/2;j>=val[i];j--)//01背包
{
dp[j] = max(dp[j],dp[j-val[i]]+val[i]);
}
}
printf("%d %d\n",sum-dp[sum/2],dp[sum/2]);
}
return 0;
}
有N种物品,知道价值和数量,若尽量分成两份,两份对应的价值,先输出大的,然后输出小的
思路:
要求分成两份,那么只有两种情况
1、一大一小
2 一样大
所以对于一大一小那种情况中小的那个最大只能是总和的一半。
这样子就可以把完全背包转换成一个0-1背包问题
AC代码:
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
int val[5005];
int dp[255555];
int main()
{
int n,i,j,a,b,l,sum;
while(~scanf("%d",&n),n>0)
{
memset(val,0,sizeof(val));
memset(dp,0,sizeof(dp));
l = 0;
sum = 0;
for(i = 0;i<n;i++)
{
scanf("%d%d",&a,&b);
while(b--)
{
val[l++] = a;//将价值存入数组
sum+=a;
}
}
for(i = 0;i<l;i++)
{
for(j = sum/2;j>=val[i];j--)//01背包
{
dp[j] = max(dp[j],dp[j-val[i]]+val[i]);
}
}
printf("%d %d\n",sum-dp[sum/2],dp[sum/2]);
}
return 0;
}
0 0
- 3019
- zoj 3019
- ZOJ 3019
- ZOJ-3019
- zoj 3019 Puzzle
- ZOJ 3019 Puzzle【水】
- zoj 3019 Puzzle
- ZOJ 3019 Puzzle
- zoj 3019 Puzzle
- 【ACM打卡】ZOJ 3019
- 电梯 SDUToj 3019
- SDUT 3019 电梯
- YTU 3019: 螺旋方阵
- YTU 3019 螺旋方阵
- [YTU](3019)螺旋方阵
- zoj 3019 Puzzle (4)
- ZOJ Problem Set - 3019 Puzzle
- 【线段树】soj 3019 Count Color
- Ubuntu下安装Qt全部过程
- Record is locked by another user --Oracle行锁解锁
- C++字面值常量
- syslog-ng详细安装配置
- sublime 使用中一些遇到问题解决方法和技巧汇总(持续更新)
- 3019
- 检查Linux服务器性能
- ajaxFileUpload带参数提交
- json数据和bean之间的转换
- Android studio中某些快捷键
- OUVa 232 Crossword answer
- Bootstrap使用Javascript显示模态框后页面上移
- Hexo+Github Pages如何搭建并异地管理Blog
- 防火墙iptable的原理