HDU 1171 组成最接近sum/2的数 (母函数)
来源:互联网 发布:手机淘宝客户端是什么 编辑:程序博客网 时间:2024/05/17 08:16
给出数的大小与个数,求最接近sum/2的数字
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
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int maxn=250000+10;int c1[maxn],c2[maxn];int v[100],num[100];int main(){int t,i,j,k,n,m;while(scanf("%d",&n)!=EOF) {m=0;if(n<0) break;for(i=1;i<=n;i++) {scanf("%d%d",&v[i],&num[i]);m+=v[i]*num[i];}memset(c1,0,sizeof(c1));memset(c2,0,sizeof(c2));for(i=0;i<=num[1];i++) {c1[v[1]*i]=1;}for(i=2;i<=n;i++) {for(j=0;j<=m/2;j++) {if(c1[j])for(k=0,t=0;k+j<=m/2 && t<=num[i];k+=v[i],t++) {c2[k+j]+=c1[j];}}for(j=0;j<=m/2;j++) {c1[j]=c2[j];c2[j]=0;}}for(i=m/2;;i--) {if(c1[i]) break;}printf("%d %d\n",m-i,i);}return 0;}
0 0
- HDU 1171 组成最接近sum/2的数 (母函数)
- HDU 1085 1,2,5不能组成的最小数 (母函数)
- 59.3Sum Closest-最接近的三数之和(中等题)
- 16. 3Sum Closest(找出和最接近给定值的三个数)
- 3Sum Closest 三个数的和最接近某个值
- HDU 1398 n由i^2(i=1,2,3..17)组成的方案数(母函数 || 完全背包)
- 最接近的数
- 最接近的数
- 最接近的数
- HDU 4585 Shaolin 找最接近的数 Treap / set
- 【LeetCode-面试算法经典-Java实现】【016-3 Sum Closest(最接近的三个数的和)】
- 华为机试: 查找组成一个偶数最接近的两个素数、自守数
- 3Sum Closest 三数最接近
- LeetCode 3Sum Closest 最接近目标数的三个数和
- 组成一个偶数最接近的两个素数
- LeetCode 16. 3Sum Closest(最接近的和)
- hdu 2566 统计硬币 母函数 硬币总个数一定 且正好用完组成m的方案数
- LeetCode 16 3Sum Closest 找出最接近指定target的三个数的和
- stevensanderson@microsoft
- 常用加密算法工具类
- 提供均衡服务的中间件——高可用
- 南阳理工 动物统计
- Android基础之Activity四大启动模式
- HDU 1171 组成最接近sum/2的数 (母函数)
- 详解android:scaleType属性
- java.lang.OutOfMemoryError: PermGen space JVM内存参数配置
- http_build_query应用
- install nginx in centos
- android(6)(读数据的一些权限)
- Tank走迷宫(某比赛决赛题)
- Linux学习笔记(十四、压缩命令)
- 数字功放