Problem S
2016-05-04 15:04
309 查看
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
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
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 题意:给你n中给定数量的不同价值的东西,问怎么样分才能使得两堆的差最小; 解题思路:动态规划中平分东西问题有一个模板:将它所给出的n个东向西加起来sum,将sum/2当作体积,求出在sum/2下的最大值,sum-dp[sum/2]; 感悟:01背包问题不能看的太简单,其衍生出来的东西太多了; 代码: #include
#include
#include
#define maxn 255555
using namespace std;
int main()
{
//freopen("in.txt","r",stdin);
int n,m,v,f[maxn],dp[maxn];
while(~scanf("%d",&n),n>0)
{
memset(dp,0,sizeof dp);
int d=0,sum=0;
for(int l=0;l
{
scanf("%d%d",&v,&m);
for(int i=0;i
{
f[d++]=v;
sum+=v;
}//将数据记录到数组,并且求出虽多的价值数
}
for(int i=0;i
for(int j=sum/2;j>=f[i];j--)
{
dp[j]=max(dp[j],dp[j-f[i]]+f[i]);
}
printf("%d %d\n",sum-dp[sum/2],dp[sum/2]);
}
return 0;
}
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
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
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 题意:给你n中给定数量的不同价值的东西,问怎么样分才能使得两堆的差最小; 解题思路:动态规划中平分东西问题有一个模板:将它所给出的n个东向西加起来sum,将sum/2当作体积,求出在sum/2下的最大值,sum-dp[sum/2]; 感悟:01背包问题不能看的太简单,其衍生出来的东西太多了; 代码: #include
#include
#include
#define maxn 255555
using namespace std;
int main()
{
//freopen("in.txt","r",stdin);
int n,m,v,f[maxn],dp[maxn];
while(~scanf("%d",&n),n>0)
{
memset(dp,0,sizeof dp);
int d=0,sum=0;
for(int l=0;l
{
scanf("%d%d",&v,&m);
for(int i=0;i
{
f[d++]=v;
sum+=v;
}//将数据记录到数组,并且求出虽多的价值数
}
for(int i=0;i
for(int j=sum/2;j>=f[i];j--)
{
dp[j]=max(dp[j],dp[j-f[i]]+f[i]);
}
printf("%d %d\n",sum-dp[sum/2],dp[sum/2]);
}
return 0;
}
相关文章推荐
- BeanFactory工厂
- Unity常用常找(二)
- Android获取设备信息
- Unity常用常找(二)
- select into from 和 insert into select 的用法和区别
- 超好:web app变革之rem
- Hadoop错误码速查
- javaweb学习总结(九)—— 通过Servlet生成验证码图片
- Spark组件之GraphX学习9--使用pregel函数求单源最短路径
- 第三届 山东省ACM省赛 Mine Number(二进制查分+暴力)
- 针对Logstash吞吐量一次优化
- JVM类加载机制
- Oracle之分页查询
- C语言(算法)
- FTP断点续传
- 微软推荐通用 Windows 应用开发者使用 SQLite
- javaweb学习总结(八)——HttpServletResponse对象(二)
- python学习笔记(virtualenv下载安装)
- 产品外观设计在线资源汇总(待完善)
- Centos6.5安装ftp组件