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. oc83--自定义类实现copy方法

    // // main.m // 自定义类实现copy #import <Foundation/Foundation.h> #import "Person.h" #imp ...

  2. algorithm库———count&&countif

    algorithm头文件定义了一个count的函数,其功能类似于find.这个函数使用一对迭代器和一个值做参数,返回这个值出现次数的统计结果. 编写程序读取一系列int型数据,并将它们存储到vecto ...

  3. java SWing事件调用的两种机制

      Java(91)  /** * java swing中事件调用的两种机制: * (一)响应机制 * (二)回调机制 */ package test; import java.awt.*; impo ...

  4. 51. ExtJs4之Ext.util.JSON编码和解码JSON对象

    转自:https://blog.csdn.net/iteye_9439/article/details/82518158 1.decode() 该方法用于将符合JSON格式的String进行解码成为一 ...

  5. Akka源码分析-Actor&ActorContext&ActorRef&ActorCell

    分析源码的过程中我们发现,Akka出现了Actor.ActorRef.ActorCell.ActorContext等几个相似的概念,它们之间究竟有什么区别和联系呢? /** * Actor base ...

  6. HTML--文本输入框、密码输入框

    当用户要在表单中键入字母.数字等内容时,就会用到文本输入框.文本框也可以转化为密码输入框. 语法: <form> <input type="text/password&qu ...

  7. python django简单操作

    准备: pip3 install  django==1.10.3 cmd django-admin startproject  guest  创建一个guest的项目 cd guest manage. ...

  8. Scala-基础-数据类型

    import junit.framework.TestCase import org.junit.Test import scala.runtime.RichByte //数据类型 class Dem ...

  9. python--9、并发之多进程应用

    multiprocessing模块 想要充分地使用多核CPU的资源(os.cpu_count()查看),在python中大部分情况需要使用多进程.Python提供了multiprocessing.  ...

  10. 01--Java集合知识

    一.Java集合概览 Java中集合分2大块,Collection和Map,分别代表不同功能的集合类对象,整体结构图如下: Collection├List│├LinkedList│├ArrayList ...