杭电acm 1171Big Event in HDU(0-1背包)
来源:互联网 发布:linux cp 合并文件夹 编辑:程序博客网 时间:2024/06/05 01:59
Big Event in HDU
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 42851 Accepted Submission(s): 14718
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).
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.
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
Author
lcy
想法:0-1背包
好久没做dp,得补上来
代码:
#include<stdio.h>
#include<string.h>
int dir[250010];
int a[55],b[55];
int max(int x,int y)
{
return x>y?x:y;
}
int main()
{
int n;
while(scanf("%d",&n)&&n>=0)
{
memset(dir,0,sizeof(dir));
int i,j,sum=0;
for(i=0;i<n;i++)
{
scanf("%d %d",&a[i],&b[i]);
sum+=a[i]*b[i];
}
int m=sum/2;
for(i=0;i<n;i++)
{
while(b[i]--)
{
for(j=m;j>=a[i];j--)
{
dir[j]=max(dir[j],dir[j-a[i]]+a[i]);
}
}
}
printf("%d %d\n",sum-dir[m],dir[m]);
}
return 0;
}
#include<string.h>
int dir[250010];
int a[55],b[55];
int max(int x,int y)
{
return x>y?x:y;
}
int main()
{
int n;
while(scanf("%d",&n)&&n>=0)
{
memset(dir,0,sizeof(dir));
int i,j,sum=0;
for(i=0;i<n;i++)
{
scanf("%d %d",&a[i],&b[i]);
sum+=a[i]*b[i];
}
int m=sum/2;
for(i=0;i<n;i++)
{
while(b[i]--)
{
for(j=m;j>=a[i];j--)
{
dir[j]=max(dir[j],dir[j-a[i]]+a[i]);
}
}
}
printf("%d %d\n",sum-dir[m],dir[m]);
}
return 0;
}
阅读全文
0 0
- 杭电acm 1171Big Event in HDU(0-1背包)
- 杭电1171Big Event in HDU(01背包)
- 杭电 HDU ACM 1171 Big Event in HDU
- 杭电-1171 Big Event in HDU(完全背包转换01背包)
- 杭电ACM hdu 1171 Big Event in HDU 解题报告(母函数)
- 杭电1171 Big Event in HDU(母函数+多重背包解法)
- 【杭电oj】1171 - Big Event in HDU(01背包)
- 杭电 1171 Big Event in HDU
- 杭电1171-Big Event in HDU
- 杭电1171 Big Event in HDU
- 杭电1171Big Event in HDU
- HDU 1171(Big Event in HDU)0-1背包问题
- 0-1背包 HDU 1171---Big Event in HDU
- 0-1背包Big Event in HDU
- Big Event in HDU(杭电1171)(多重背包)和(母函数)两种解法
- hdu 1171 Big Event in HDU(0-1背包问题)
- [HDU 1171 Big Event in HDU]背包
- HDU ACM 1171 Big Event in HDU
- Problem-J-完全背包
- 237. Delete Node in a Linked List
- MySQL创建普通用户
- Java之异常
- Linux C 多线程编程 互斥锁
- 杭电acm 1171Big Event in HDU(0-1背包)
- 【HDU 1016】Prime Ring Problem(DFS、素数筛)
- 2017云哥性能测试实战带你走出迷茫
- Java并发集合操作中对锁的应用
- Python学习之文件处理详解
- Java表格仿mysql实现基本操作(连接,分组排序,统计等)
- Linux C 多线程编程条件变量
- [第五季]14.属性选择器和伪类伪元素
- 美团js面试题