Given a set of N (>1) positive integers, you are supposed to partition them into two disjoint sets A1 and A2 of n1​​ and n​2 numbers, respectively. Let S1​ 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 ∣n​1​​ −n​2​​ ∣ is minimized first, and then ∣S​1​​ −S​2​​ ∣ 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: ∣n​1− 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

#include<iostream> //水题
#include<vector>
#include<algorithm>
using namespace std;
bool cmp(const int &a, const int &b){
return a>b;
}
int main(){
int N, sum=0;
cin>>N;
vector<int> vec(N, 0);
for(int i=0; i<N; i++)
cin>>vec[i];
sort(vec.begin(), vec.end(), cmp);
int t=N%2==0?N/2:N/2+1;
for(int i=0; i<N; i++)
if(i<t)
sum+=vec[i];
else
sum-=vec[i];
cout<<2*t-N<<" "<<sum<<endl;
return 0;
}

PAT 1113 Integer Set Partition的更多相关文章

  1. 1113 Integer Set Partition (25 分)

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

  2. PAT 甲级 1113 Integer Set Partition

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

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

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

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

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

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

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

  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. 1113 Integer Set Partition

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

  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. 第二章 在Html中使用JavaScript

    https://www.jianshu.com/p/8247a9401725 2.1 Script元素 https://developer.mozilla.org/en-US/docs/Web/HTM ...

  2. 【Poj1325】Machine Schedule机器调度

    目录 List Description Input Output Sample Input Sample Output HINT Solution Code Position: http://poj. ...

  3. 使用EL表达式正确情况下报错:javax.servlet.jsp cannot be resolved to a type

    这个错误可能是服务器自带的servlet库未导入的原因.右键项目属性,转到Targeted Runtimes,选择一个服务器,例如Tomcat,单击应用,可能就可以解决.

  4. java动态代理实例

    import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflec ...

  5. MVVMLight消息通知实现机制详解(二)

    接上文 MVVMLight消息通知实现机制详解(一) 该工具的内部主要逻辑是以字典模式进行储存持有订阅对象设置的传入参数Type类型.Key值.Action.Target(订阅对象本身) 在发生订阅事 ...

  6. PCB SVN 服务端VisualSVN Server与TortoiseSVN

    PCB 工程系统SVN源代码招病毒破坏以后,一周时间都没有源代码同步更新了,今天终于将SVN源代码数据恢复并重建SVN服务器,这里将SVN搭建安装过程整理如下 一.服务端SVN安装 1.下载地址:ht ...

  7. sql 查询出当天记录数据

    select updatetime,NewComment,HistoryID,sum(1) over(partition by UpdateTime) from LPProjectHistoryord ...

  8. RocketMQ(2)

    1. 消费端集群消费(负载均衡) 示例代码: /** * Producer,发送消息 * */ public class Producer { public static void main(Stri ...

  9. linux 查看内存和cpu

    Linux查看CPU和内存使用情况 在系统维护的过程中,随时可能有需要查看 CPU 使用率,并根据相应信息分析系统状况的需要.在 CentOS 中,可以通过 top 命令来查看 CPU 使用状况.运行 ...

  10. 数据清洗——python定位csv中的特定字符位置

    之前发过一篇关于定位csv中的特殊字符的,主要是用到了python的自带的函数,近期又遇到了一些新的问题,比如isdigit()的缺点在于不能判断浮点型,以及小数中有多个小数点的情况.发现还是正则表达 ...