hdu 1171 Big Event in HDU
2016-03-16 21:22
274 查看
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
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
#include <iostream> using namespace std; int c1[250010], c2[250010]; //c1保存结果,c2保存中间值 int value[55]; //物品的价值 int amount[55]; //物品的数量 int main() { int nNum; while(scanf("%d", &nNum) && nNum>0) { memset(value, 0, sizeof(value)); memset(amount, 0, sizeof(amount)); int sum = 0; for(int i = 1; i <= nNum; ++ i) { scanf("%d %d", &value[i], &amount[i]); sum += value[i]*amount[i]; //计算物品的总价值 } memset(c1, 0, sum*sizeof(c1[0])); memset(c2, 0, sum*sizeof(c2[0])); for(int i = 0; i <= value[1]*amount[1]; i += value[1]) c1[i] = 1; int len = value[1]*amount[1]; //记录每次需循环的最大次数 **重点 for(int i = 2; i <= nNum; ++ i) { for(int j = 0; j <= len; ++ j)<span style="white-space:pre"> </span>//重点 for(int k = 0; k <= value[i]*amount[i]; k += value[i]) { c2[k + j] += c1[j]; //在原来的基础上加 } len += value[i]*amount[i]; //每次循环后,最大次数增加<span style="white-space:pre"> </span>**重点 for(int j = 0; j <= len; ++ j) { c1[j] = c2[j]; //将c2中的值转移到c1中去 c2[j] = 0; //c1全赋值为0 } } for(int i = sum/2; i >= 0; -- i) //从中间开始向前搜索 if(c1[i] != 0) { printf("%d %d\n", sum - i, i); break; } } return 0; }
相关文章推荐
- (转载)如何用PHP开发一个完整的网站 讲解开发流程 多人开发
- 图的邻接矩阵实现
- cin.get(), cin.getline(), getline()简单区别
- GitHub Android Libraries Top 100 简介
- 2016春季阿里游戏前端实习生笔试题
- android,动画切换界面 animation
- 计算VMT的长度
- Android的系统架构
- bash shell 编程练习一
- 利用递归实现10进制转化成任意进制
- linux 创建 静态 链接库 动态 链接库
- java集合框架总结
- java7新特性之Simplified varargs method invocation
- poj1011 深搜入门
- JavaScript的三种工业化调试方法
- NopCommerce商城系统中的eventbus
- vs2010使用C
- POJ - 1159 Palindrome(dp-回文变形)
- VS2013运行OpenGL例子提示找不到<GL/glew.h>,GL/freeglut_ext.h,GL/freeglut.h,glew32.lib,freeglut.lib等
- STM32 大小端模式 与 堆栈及其增长方向分析