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

1113. Integer Set Partition (25)的更多相关文章

  1. PAT (Advanced Level) 1113. Integer Set Partition (25)

    简单题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #in ...

  2. 【PAT甲级】1113 Integer Set Partition (25分)

    题意: 输入一个正整数N(2<=N<=1e5),接着输入N个正整数,将这些数字划分为两个不相交的集合,使得他们的元素个数差绝对值最小且元素和差绝对值最大. AAAAAccepted cod ...

  3. 1113 Integer Set Partition (25 分)

    1113 Integer Set Partition (25 分) Given a set of N (>1) positive integers, you are supposed to pa ...

  4. PAT 甲级 1113 Integer Set Partition

    https://pintia.cn/problem-sets/994805342720868352/problems/994805357258326016 Given a set of N (> ...

  5. PAT 1113 Integer Set Partition

    Given a set of N (>1) positive integers, you are supposed to partition them into two disjoint set ...

  6. 1113 Integer Set Partition

    Given a set of N (>) positive integers, you are supposed to partition them into two disjoint sets ...

  7. A1113 | Integer Set Partition (25)

    太简单了 #include <stdio.h> #include <memory.h> #include <math.h> #include <string& ...

  8. PAT1113: Integer Set Partition

    1113. Integer Set Partition (25) 时间限制 150 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...

  9. PAT_A1113#Integer Set Partition

    Source: PAT A1113 Integer Set Partition (25 分) Description: Given a set of N (>) positive integer ...

随机推荐

  1. LeetCode:下一个排列【31】

    LeetCode:下一个排列[31] 题目描述 实现获取下一个排列的函数,算法需要将给定数字序列重新排列成字典序中下一个更大的排列. 如果不存在下一个更大的排列,则将数字重新排列成最小的排列(即升序排 ...

  2. Python学习进程(1)Python简介

        Python是一种结合了"解释性"."编译性"."互动性"和"面向对象"的脚本语言. (1)官方介绍: Pyth ...

  3. $SVN代码版本管理工具的使用

    SVN是一种代码版本管理工具,具有可视化的操作界面,使用简便,和git的功能类似.下面总结一下SVN的基本用法: 1.安装SVN软件,和安装一般的软件的步骤差不多,这里使用的版本是TortoiseSV ...

  4. 约瑟夫环的C语言数组实现

    约瑟夫环问题的具体描述是:设有编号为1,2,……,n的n个(n>0)个人围成一个圈,从第1个人开始报数,报到m时停止报数,报m的人出圈,才从他的下一个人起重新报数,报到m时停止报数,报m的出圈, ...

  5. mysql中int(3)与int(11)有什么区别吗?

    注意:这里的M代表的并不是存储在数据库中的具体的长度,以前总是会误以为int(3)只能存储3个长度的数字,int(11)就会存储11个长度的数字,这是大错特错的. 其实当我们在选择使用int的类型的时 ...

  6. PHPExcel读写封装

    <? require_once ('inc/PHPExcel/PHPExcel/IOFactory.php'); /** * @author lgl * 使用实例 * $fieldMap=['昵 ...

  7. Qt5.3.0的安装与测试

    Qt5.3.0的安装与测试(交叉编译,用于arm,支持tslib触摸屏) 本次移植可以使用触摸屏. 首先下载源码包: http://download.qt.io/official_releases/q ...

  8. 基于SSM的单点登陆01

    使用SSM的Maven聚合项目 建立父项目market的pom文件 <?xml version="1.0" encoding="UTF-8"?> & ...

  9. Python中的条件选择和循环语句

    一.条件选择语句 Python中条件选择语句的关键字为:if .elif .else这三个.其基本形式如下: if condition: block elif condition: block ... ...

  10. Tomcat学习之Wrapper

    Tomcat学习之Wrapper 分类: WEB服务器2012-08-30 22:16 1547人阅读 评论(0) 收藏 举报 tomcatservletwrapperservletslistexce ...