题目描写叙述

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;
}
}

最新文章

  1. 攻城狮在路上(贰) Spring(一)--- 软件环境、参考书目等一览表
  2. ORA-12518,TNS:listener could not hand off client connection
  3. &lt; java.util &gt;-- Iterator接口
  4. vs2010打开vs2012的sln文件
  5. @font-face
  6. 各种语言一句话反弹shell
  7. 团队作业9——测试与发布(Beta版本)
  8. python的Collections 模块
  9. JS基础(二)
  10. Git运用基础之如何删除Github上不想要的项目
  11. ubuntu 安装 pycharm
  12. loadtxt函数
  13. linux脚本实现scp命令自动输入密码和yes/no等确认信息
  14. 神州数码多区域OSPF配置
  15. 异步 Apex 类
  16. 第 3 章 镜像 - 019 - 使用公共 Registry
  17. 用node.js和webpack做前后端分离的总结
  18. [转]IDEA 出现编译错误 Multi-catches are not supported a this language level 解决方法
  19. js 参数声明用var和不用var的区别
  20. LevelDb日知录之五:MemTable详解

热门文章

  1. python 异步IO-aiohttp与简单的异步HTTP客户端/服务器
  2. UVALive 5790 Ball Stacking DP
  3. 洛谷 P1293 班级聚会
  4. struts2登录后返回登录前的页面
  5. ArcGIS api for javascript—测量,测距问题
  6. Android4.42-Settings源代码分析之蓝牙模块Bluetooth(上)
  7. CodeForces B. The least round way(dp)
  8. HDU 1520 Anniversary party(DFS或树形DP)
  9. django 笔记4 数据库操作
  10. js sort()函数 排序问题 var arr =[&#39;A-1-5-1&#39;,&#39;A-1-10-2&#39;,&#39;A-1-5-5&#39;,&#39;B-2-3-1&#39;,&#39;C-4-10-1&#39;], 对这个数组进行排序,想达到的效果是[&quot;A-1-5-1&quot;, &quot;A-1-5-5&quot;, &quot;A-4-10-1&quot;, &quot;A-1-10-2&quot;, &quot;A-2-3-1&quot;]