Problem C: Celebrity Split
题目描写叙述
Problem C: Celebrity Split
Jack and Jill have decided to separate and divide their property equally. Each of their N mansions has a value between 1,000,000
and 40,000,000 dollars. Jack will receive some of the mansions; Jill will receive some of the mansions; the remaining mansions will be sold, and the proceeds split equally.
Neither Jack nor Jill can tolerate the other receiving property with higher total value. The sum of the values of the mansions Jack receives must be equal to the sum of the values of the mansions Jill receives. So long as the value that each receives is equal,
Jack and Jill would like each to receive property of the highest possible value.
Given the values of N mansions, compute the value of the mansions that must be sold so that the rest may be divided so as to satisfy Jack and Jill.
Example
Suppose Jack and Jill own 5 mansions valued at 6,000,000, 30,000,000, 3,000,000, 11,000,000, and 3,000,000 dollars. To satisfy their requirements, Jack or Jill would receive the mansion worth 6,000,000 and the other would receive both manstions worth 3,000,000
dollars. The mansions worth 11,000,000 and 30,000,000 dollars would be sold, for a total of 41,000,000 dollars. The answer is therefore 41000000.
输入要求
The input consists of a sequence of test cases. The first line of each test case contains a single integer N, the number of mansions, which will be no more than 24. This line is followed by N lines, each giving the value of
a mansion. The final line of input contains the integer zero. This line is not a test case and should not be processed.
输出要求
For each test case, output a line containing a single integer, the value of the mansions that must be sold so that the rest may be divided so as to satisfy Jack and Jill.
假如输入
5
6000000
30000000
3000000
11000000
3000000
0
应当输出
41000000
#include<iostream>
#include<algorithm>
#include <vector>
#include<string.h>
#include<ctype.h>
#include<math.h>
using namespace std;
int sum[200],a[200],ans;
void fun();
void divide(int p,int diff,int hav)//p:剩余房子数,differ:两人差价,hav:两人共取走的钱
{
int i,j,n;
if(diff==0&&ans<hav)
ans=hav;
if(p<0)
return;
if(abs(diff)>sum[p])//剪枝1
return;
if(sum[p]+hav<ans)//剪枝2
return;
divide(p-1,diff+a[p],hav+a[p]);
divide(p-1,diff-a[p],hav+a[p]);
divide(p-1,diff,hav);
}
int main()
{
fun();
return 0;
}
void fun()
{
int n,i;
while(cin>>n&&n)
{
for(i=0;i<n;i++)
{
cin>>a[i];
if(i==0)
sum[i]=a[0];
else
sum[i]=sum[i-1]+a[i];
}
ans=0;
divide(n-1,0,0);
cout<<sum[n-1]-ans<<endl;
}
}
Problem C: Celebrity Split的更多相关文章
- TJU Problem 2548 Celebrity jeopardy
下次不要被长题目吓到,其实不一定难. 先看输入输出,再揣测题意. 原文: 2548. Celebrity jeopardy Time Limit: 1.0 Seconds Memory Lim ...
- WordCount作业提交到FileInputFormat类中split切分算法和host选择算法过程源码分析
参考 FileInputFormat类中split切分算法和host选择算法介绍 以及 Hadoop2.6.0的FileInputFormat的任务切分原理分析(即如何控制FileInputForm ...
- Common Pitfalls In Machine Learning Projects
Common Pitfalls In Machine Learning Projects In a recent presentation, Ben Hamner described the comm ...
- Using zend-paginator in your Album Module
Using zend-paginator in your Album Module TODO Update to: follow the changes in the user-guide use S ...
- MapReduce 模式、算法和用例(MapReduce Patterns, Algorithms, and Use Cases)
在新文章“MapReduce模式.算法和用例”中,Ilya Katsov提供了一个系统化的综述,阐述了能够应用MapReduce框架解决的问题. 文章开始描述了一个非常简单的.作为通用的并行计算框架的 ...
- EM and GMM(Theory)
Part 1: Theory 目录: What's GMM? How to solve GMM? What's EM? Explanation of the result What's GMM? GM ...
- Dream team: Stacking for combining classifiers梦之队:组合分类器
sklearn实战-乳腺癌细胞数据挖掘(博主亲自录制视频) https://study.163.com/course/introduction.htm?courseId=1005269003& ...
- 谷歌的java文本差异对比工具
package com.huawei.common.transfertool.utils; /** * @author Paul * @version 0.1 * @date 2018/12/11 * ...
- 可扩展的Web系统和分布式系统(Scalable Web Architecture and Distributed Systems)
Open source software has become a fundamental building block for some of the biggest websites. And a ...
随机推荐
- shell单引号屏蔽变量方法
[goforit ~]$ name="玖零後大叔" [goforit~]$ echo $name 玖零後大叔 [goforit ~]$ echo "$name" ...
- free---显示内存
free命令可以显示当前系统未使用的和已使用的内存数目,还可以显示被内核使用的内存缓冲区. 语法 free(选项) 选项 -b:以Byte为单位显示内存使用情况: -k:以KB为单位显示内存使用情况: ...
- 再谈CLR查找和加载程序集的方式
原文:再谈CLR查找和加载程序集的方式 这是一个老问题,以前也有朋友写过一些文章介绍,但可能还不是很全面.我也多次被人问到,这里结合案例再次谈谈,希望对大家有所帮助. 本文范例代码可以通过这里下载 h ...
- Ural 1303 Minimal Coverage(贪心)
题目地址:Ural 1303 先按每一个线段的左端点排序,然后设置一个起点s.每次都从起点小于等于s的线段中找到一个右端点最大的. 并将该右端点作为新的起点s,然后继续找. 从左到右扫描一遍就可以. ...
- Effective C++ Item 30 inline里里外外
本文为senlie原创.转载请保留此地址:http://blog.csdn.net/zhengsenlie Item 44 46 1.将大多数 inlining 限制在小型.被频繁调用的函数身上.这可 ...
- Cordic 算法入门
三角函数的计算是个复杂的主题,有计算机之前,人们通常通过查找三角函数表来计算任意角度的三角函数的值.这种表格在人们刚刚产生三角函数的概念的时候就已经有了,它们通常是通过从已知值(比如sin(π/2)= ...
- hdoj--2955--Robberies(背包好题)
Robberies Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- P3507 [POI2010]GRA-The Minima Game
题目描述 Alice and Bob learned the minima game, which they like very much, recently. The rules of the ga ...
- Sandbox
Sandbox Contents Overview Design principles Sandbox windows architecture The broker process The targ ...
- HP 1022N 网络打印机安装步骤
HP 1022N 网络打印机安装步骤