【PAT】A1113 Integer Set Partition【贪心】

该博客介绍了PAT(A1113)题目,内容涉及如何将一组正整数分成两个子集,使得子集的元素数量差最小,并且两个子集的元素和差最大化。博主提供了思路解析,即先排序后按长度为[N/2]分配给一个子集,其余分配给另一个子集,保证了问题的最优解。最后,博主给出了实现代码。
摘要由CSDN通过智能技术生成

Given a set of N (> 1) positive integers, you are supposed to partition them into two disjoint sets A1 and A2 of n1 and n2numbers, 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≤10^​5), 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: ∣n1−​n2 ∣ and ∣S1​​ −S​2 ∣, separated by exactly one space.

Sample Input 1:

10
23 8 10 99 46 2333 46 1 666 555

Sample Outpu

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值