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