Source:

PAT A1113 Integer Set Partition (25 分)

Description:

Given a set of N (>) positive integers, you are supposed to partition them into two disjoint sets A​1​​and A​2​​ of n​1​​ and n​2​​ numbers, respectively. Let S​1​​ and S​2​​ denote the sums of all the numbers in A​1​​and A​2​​, respectively. You are supposed to make the partition so that ∣ is minimized first, and then ∣ is maximized.

Input Specification:

Each input file contains one test case. For each case, the first line gives an integer N (2), 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 2​31​​.

Output Specification:

For each case, print in a line two numbers: ∣ and ∣, 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

Keys:

  • 简单模拟

Attention:

  • 408的一道真题,最快用O(N)规模完成,思路就是基于快排算法寻找中轴;但这里没卡时间就比较简单了-,-

Code:

 /*
Data: 2019-05-29 21:38:41
Problem: PAT_A1113#Integer Set Partition
AC: 08:30 题目大意:
给定N个整数的集合,把他们分为两个部分,要求两部分和的差最大且元素个数差最小
*/ #include<cstdio>
#include<algorithm>
using namespace std;
const int M=1e5+;
int s[M]; int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("Test.txt", "r", stdin);
#endif int n,sum=;
scanf("%d", &n);
for(int i=; i<n; i++)
scanf("%d", &s[i]);
sort(s,s+n);
for(int i=; i<n/; i++)
sum+= (s[n--i]-s[i]);
if(n%==)
printf("0 ");
else{
printf("1 ");
sum += s[n/];
}
printf("%d\n", sum); return ;
}

PAT_A1113#Integer Set Partition的更多相关文章

  1. PAT1113: Integer Set Partition

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

  2. 1113 Integer Set Partition (25 分)

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

  3. A1113. Integer Set Partition

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

  4. PAT A1113 Integer Set Partition (25 分)——排序题

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

  5. PAT 甲级 1113 Integer Set Partition

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

  6. 1113. Integer Set Partition (25)

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

  7. PAT 1113 Integer Set Partition

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

  8. PAT甲级——A1113 Integer Set Partition

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

  9. 1113 Integer Set Partition

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

随机推荐

  1. 查看当前Java进程工具jps(转)

    jps是JDK提供的一个查看当前Java进程的小工具, 可以看做是JavaVirtual Machine Process Status Tool的缩写.非常简单实用. 命令格式: jps [optio ...

  2. Java对二叉搜索树进行插入、查找、遍历、最大值和最小值的操作

    1.首先,须要一个节点对象的类.这些对象包括数据.数据代表存储的内容,并且还有指向节点的两个子节点的引用 class Node { public int iData; public double dD ...

  3. linux UID,GID,EUID,EGID,SUID,SGID

    SUID, SGID, sticky位可以参考: http://onlyzq.blog.51cto.com/1228/527247/ SUID属性只能运用在可执行文件上,当用户执行该执行文件时,会临时 ...

  4. DSP、Media、AdExchanger之间的关系及交互流程

    广告商,如以下的樱花日语,淘宝卖家.其须要推广自己的产品. Zampdsp(晶赞) 是DSP平台.其与非常多广告商合作,广告商在平台上公布广告创意,并托付平台代为投放. tanx.com 是adExc ...

  5. ant+jmeter中build.xml配置详解

  6. 查看scn headroom变化趋势的几种方法

    查看scn headroom变化趋势的几种方法 scn headroom问题,本文不做解释. 本文为自己的总结,脚本来自于oracle sr技术project师. 转载请注明出处http://blog ...

  7. linux openssl 编程 Client端

    相关配置等请參看上一篇关于server端文章:http://blog.csdn.net/pingd/article/details/47805349 1.Client端源代码: openssl_cli ...

  8. 【POJ 1475】 Pushing Boxes

    [题目链接] http://poj.org/problem?id=1475 [算法] 双重BFS [代码] #include <algorithm> #include <bitset ...

  9. 【BZOJ 2252】 矩阵距离

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=2252 [算法] 将所有是”1“的点入队,然后广度优先搜索,即可 [代码] #incl ...

  10. git的使用(转)

    git 配置文件 git的配置文件位置针对所有用户:/etc/gitconfig针对当前用户: -/.gitconfig 查看配置的方法 git config --list 修改配置的方法 git c ...