Big Event in HDU

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 24002    Accepted Submission(s): 8458

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
 
Author
lcy
 

Mean:

有n种物品,第一种物品的单价为v1,数量为m1;第二种物品的单价为v2,数量为m2.....现在要你将这些物品分为两堆,使得这两堆物品的价值尽量接近,输出两堆物品的价值。

analyse:

这道题的解法很多:dp,母函数.....,详见《编程之美》。

这里我用了两种方法来做了一下,发现后面的方法比母函数快多了。

Time complexity:O(n^2)

Source code:

母函数代码:

// Memory   Time
// 1347K 0MS
// by : Snarl_jsb
// 2014-09-18-18.50
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<string>
#include<climits>
#include<cmath>
#define N 234567
#define LL long long
using namespace std; int val[600],cnt[110];
int c1[N],c2[N];
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
// freopen("C:\\Users\\ASUS\\Desktop\\cin.cpp","r",stdin);
// freopen("C:\\Users\\ASUS\\Desktop\\cout.cpp","w",stdout);
int n;
while(cin>>n&&n>0)
{
long long sum=0;
for(int i=1;i<=n;++i)
{
cin>>val[i]>>cnt[i];
sum+=val[i]*cnt[i];
}
memset(c1,0,sizeof(c1));
memset(c2,0,sizeof(c2));
for(int i=0;i<=cnt[1]*val[1];i+=val[1])
c1[i]=1;
for(int i=2;i<=n;++i)
{
for(int j=0;j<=sum;++j)
{
for(int k=0;k<=cnt[i];++k)
{
c2[val[i]*k+j]+=c1[j];
}
}
for(int j=0;j<=sum;++j)
{
c1[j]=c2[j];
c2[j]=0;
}
}
if(c1[sum/2]==2)
{
cout<<sum/2<<" "<<sum/2<<endl;
continue;
}
int t=sum/2;
int QAQ,TAT;
int minn=987654321;
for(int i=0;i<=sum;++i)
{
if(c1[i])
{
if(abs(sum/2-i)<minn)
{
minn=abs(sum/2-i);
QAQ=i;
}
}
}
TAT=sum-QAQ;
if(QAQ<TAT)
{
QAQ^=TAT^=QAQ^=TAT;
}
cout<<QAQ<<" "<<TAT<<endl;
}
return 0;
}

  

数学方法:

// Memory   Time
// 1347K 0MS
// by : Snarl_jsb
// 2014-09-18-23.05
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<string>
#include<climits>
#include<cmath>
#define N 1000010
#define LL long long
using namespace std; int val[N],cnt[N];
int buff[N];
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
// freopen("C:\\Users\\ASUS\\Desktop\\cin.cpp","r",stdin);
// freopen("C:\\Users\\ASUS\\Desktop\\cout.cpp","w",stdout);
int n;
while(cin>>n&&n>0)
{
long long v,t,idx=0,sum=0;
for(int i=1;i<=n;i++)
{
cin>>v>>t;
val[i]=v,cnt[i]=t;
sum+=val[i]*cnt[i];
while(t--)
{
buff[++idx]=v;
}
}
sort(buff+1,buff+1+idx);
int half=sum/2;
long long ans=0;
for(int i=idx;i>=1;--i)
{
if(ans+buff[i]<=half)
{
ans+=buff[i];
}
}
cout<<sum-ans<<" "<<ans<<endl;
}
return 0;
}

  

组合数学 - 母函数的变形 --- hdu 1171:Big Event in HDU的更多相关文章

  1. HDU 1171 Big Event in HDU 多重背包二进制优化

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1171 Big Event in HDU Time Limit: 10000/5000 MS (Jav ...

  2. hdu 1171 Big Event in HDU(母函数)

    链接:hdu 1171 题意:这题能够理解为n种物品,每种物品的价值和数量已知,现要将总物品分为A,B两部分, 使得A,B的价值尽可能相等,且A>=B,求A,B的价值分别为多少 分析:这题能够用 ...

  3. HDU 1171 Big Event in HDU (多重背包变形)

    Big Event in HDU Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  4. hdu 1171 Big Event in HDU (01背包, 母函数)

    Big Event in HDU Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  5. HDU 1171 Big Event in HDU 母函数

    欢迎参加——BestCoder周年纪念赛(高质量题目+多重奖励) Big Event in HDU Time Limit: 10000/5000 MS (Java/Others)    Memory ...

  6. HDU 1171 Big Event in HDU (多重背包)

    Big Event in HDU Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  7. 【01背包】HDU 1171 Big Event in HDU

    Problem Description Nowadays, we all know that Computer College is the biggest department in HDU. Bu ...

  8. HDU 1171 Big Event in HDU dp背包

    Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s ...

  9. HDU 1171 Big Event in HDU【01背包/求两堆数分别求和以后的差最小】

    Big Event in HDU Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) T ...

随机推荐

  1. Android Launcher分析和修改13——实现Launcher编辑模式(1) 壁纸更换

    已经很久没更新Launcher系列文章,今天不分析源码,讲讲如何在Launcher里面添加桌面设置的功能.目前很多第三方Launcher或者定制Rom都有简单易用的桌面设置功能.例如小米MIUI的La ...

  2. 进入做Mvc项目的时候 返现某个文件夹下面css js png等静态文件都访问不了

    原来是我在该文件夹下面添加了一个web.config 里面 静止了所有的文件 直接访问 <system.web>    <httpHandlers>      <add ...

  3. Make it run, make it right, make it fast

    如果问我工作十多年后相比刚毕业参加的时候,学到了哪些重要的经验,那么"Make it work, make it right, make it fast"一定是其中最重要的经验之一 ...

  4. Selenium实战脚本集(3)--抓取infoq里的测试新闻

    描述 打开infoq页面,抓取最新的一些测试文章 需要抓取文章的标题和内容 如果你有个人blog的话,可以将这些文章转载到自己的blog 要求 不要在新窗口打开文章 自行了解最新的测试思潮与实践

  5. asp.net发布到IIS中出现错误:处理程序“PageHandlerFactory-Integrated”在其模块列表中有一个错误模块“ManagedPipelineHandler”

    asp.net发布到IIS中出现错误:处理程序“PageHandlerFactory-Integrated”在其模块列表中有一个错误模块“ManagedPipelineHandler” http:// ...

  6. 中国的 Android:尚未发掘的应用市场?

    作者: Wendy Boswell 本周,中国的搜索引擎公司百度最新公布的一篇报告介绍了中国 Android 用户的移动趋势. 下面是一些有价值的统计数据: 眼下在中国,每天的 Android 活跃用 ...

  7. MySQL解决插入emoji表情失败的问题

    普通的字符串或者表情都是占位3个字节,所以utf8足够用了,但是移动端的表情符号占位是4个字节,普通的utf8就不够用了,为了应对无线互联网的机遇和挑战.避免 emoji 表情符号带来的问题.涉及无线 ...

  8. Openvswitch原理与代码分析(7): 添加一条流表flow

    添加一个flow,调用的命令为 ovs-ofctl add-flow hello "hard_timeout=0 idle_timeout=0 priority=1 table=21 pkt ...

  9. CLR via C# 提纲

    第I部分 CLR基础第1章 CLR的执行模型 31.1 将源代码编译成托管模块 31.2 将托管模块合并成程序集 61.3 加载公共语言运行时 81.4 执行程序集的代码 101.4.1 IL和验证 ...

  10. Java 10大精华文章收集001

    Java语言与JVM中的Lambda表达式全解 Lambda表达式是自Java SE 5引入泛型以来最重大的Java语言新特性,本文是2012年度最后一期Java Magazine中的一篇文章,它介绍 ...