Given a set of N (> 1) positive integers, you are supposed to partition them into two disjoint sets A1 and A2 of n1 and n2 numbers, respectively. Let S1 and S2 denote the sums of all the numbers in A1 and A2, respectively. You are supposed to make the partition so that |n1 - n2| is minimized first, and then |S1 - S2| is maximized.

Input Specification:

Each input file contains one test case. For each case, the first line gives an integer N (2 <= N <= 105), and then N positive integers follow in the next line, separated by spaces. It is guaranteed that all the integers and their sum are less than 231.

Output Specification:

For each case, print in a line two numbers: |n1 - n2| and |S1 - S2|, separated by exactly one space.

Sample Input 1:

10
23 8 10 99 46 2333 46 1 666 555

Sample Output 1:

0 3611

Sample Input 2:

13
110 79 218 69 3721 100 29 135 2 6 13 5188 85

Sample Output 2:

1 9359

首先要保证n1和n2相差最小无非是相差0或者1,也就是n是偶数或者是奇数,然后再保证s1和s2相差最大,排个序,从中间分开即可。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int s[];
int n;
int main()
{
cin>>n;
for(int i = ;i < n;i ++)
{
cin>>s[i];
}
sort(s,s + n);
int sum = ;
for(int i = ;i < n / ;i ++)
sum += s[n - i - ] - s[i];
if(n % )
{
cout<<<<' '<<sum + s[n / ];
}
else cout<<<<' '<<sum;
}

最新文章

  1. Python的平凡之路(16)
  2. C#编程总结(十三)数据压缩
  3. CSS关于元素垂直居中的问题
  4. JS结构图
  5. 从xml文件中读取注释
  6. Linux 调节屏幕亮度
  7. css3 旋转效果加上双面显示效果
  8. Java中boolean类型到底占用多少字节
  9. Python系列之模块、和字符串格式化
  10. 如何写一个SSH项目(三)如何进行交互的
  11. Mac OS X 清除DNS缓存
  12. Handlebars模板引擎之高阶
  13. Web项目开发中常见安全问题防范
  14. Jenkins自动化构建(一)执行selenium+python脚本
  15. 浅谈博弈论之Nim初步(xor正确性的浅显证明)
  16. python3.0与python2.0有哪些不同
  17. 双端队列 ADT接口 数组实现
  18. Java导出数据行写入到Excel表格:基于Apache POI
  19. Bulma 源码解析之 .columns 类
  20. centos下配置ssh使用密钥

热门文章

  1. Python 2 一些实用模块的使用
  2. python Selenium库的使用
  3. 前端 css续
  4. linux中安装软件的集中方法
  5. HTML学习笔记(下)
  6. 【Head First Servlets and JSP】笔记8:监听者
  7. uboot无法引导uImage错误及其解决方法
  8. 基于Visual c++ 2012的php扩展开发 - 环境搭建
  9. 通过公钥解密密文思路(256bits RSA)
  10. ubuntu ssh免密码登录