Given a set of N (>1) 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 ∣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​​−n​2​​∣ and ∣S​1​​−S​2​​∣, 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 <stdio.h>
#include <algorithm>
using namespace std;
const int maxn=;
int seq[maxn];
int total[maxn];
int main(){
int n;
int sum=;
scanf("%d",&n);
for(int i=;i<n;i++){
scanf("%d",&seq[i]);
}
sort(seq,seq+n);
for(int i=;i<n;i++){
sum+=seq[i];
total[i]=sum;
}
printf("%d %d",n%,total[n-]-*total[n/-]);
}

注意点:计算差的时候由于总和算了前面部分,要多减一次前半部分。感觉直接算和然后相减也不会超时

PAT A1113 Integer Set Partition (25 分)——排序题的更多相关文章

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

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

  2. A1113 | Integer Set Partition (25)

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

  3. PAT 甲级 1028 List Sorting (25 分)(排序,简单题)

    1028 List Sorting (25 分)   Excel can sort records according to any column. Now you are supposed to i ...

  4. PAT A1141 PAT Ranking of Institutions (25 分)——排序,结构体初始化

    After each PAT, the PAT Center will announce the ranking of institutions based on their students' pe ...

  5. PAT A1137 Final Grading (25 分)——排序

    For a student taking the online course "Data Structures" on China University MOOC (http:// ...

  6. PAT A1028 List Sorting (25 分)——排序,字符串输出用printf

    Excel can sort records according to any column. Now you are supposed to imitate this function. Input ...

  7. PAT A1016 Phone Bills (25 分)——排序,时序

    A long-distance telephone company charges its customers by the following rules: Making a long-distan ...

  8. PAT 甲级 1016 Phone Bills (25 分) (结构体排序,模拟题,巧妙算时间,坑点太多,debug了好久)

    1016 Phone Bills (25 分)   A long-distance telephone company charges its customers by the following r ...

  9. PAT 甲级 1146 Topological Order (25 分)(拓扑较简单,保存入度数和出度的节点即可)

    1146 Topological Order (25 分)   This is a problem given in the Graduate Entrance Exam in 2018: Which ...

随机推荐

  1. docker构建自定义镜像

    docker构建自定义镜像 要构建一个镜像,第一步准备所需要的文件,第二步编写Dockerfile文件,比如我现在构建一个java web镜像 第一步:准备java web工程的war包文件(这里假设 ...

  2. 拜小白教你OpenCV3.2.0+VS2017开发环境配置

    第一部分:OpenCV3.2.0的下载 OpenCV官方下载地址: http://opencv.org/releases.html# 本人选择opencv3.2.0基于Windows平台.读者根据自己 ...

  3. CSS效果:CSS select样式优化 含jquery代码

    CSS 下拉选择菜单基本的CSS样式不怎么好看,通过一些简单的样式优化,可以得到如下图这样的: html结构如下: <div class="sel_wrap"> < ...

  4. Node.js+Koa开发微信公众号个人笔记(二)响应事件

    微信公众号中的事件有订阅事件/扫码事件/点击事件/跳转链接事件等等,具体可以查阅文档. 这里来实现一下订阅事件,其他的事件的实现过程也都类似. 当有人订阅了公众号后,微信服务器会向我们的服务器推送一个 ...

  5. iOS动画-从UIView到Core Animation

    首先,介绍一下UIView相关的动画. UIView普通动画: [UIView beginAnimations: context:]; [UIView commitAnimations]; 动画属性设 ...

  6. VirtualBox下安装CentOS7系统

    本文假定你已经知道如何安装VirtualBox虚拟机软件,并且已经安装好了. 首先我们需要准备好centos的iso镜像文件,可以从centos的官网下载. 以下操作使用的VirtualBox版本号是 ...

  7. 10-openldap同步原理

    openldap同步原理 阅读视图 openldap同步原理 syncrepl.slurpd同步机制优缺点 OpenLDAP同步条件 OpenLDAP同步参数 1. openldap同步原理 Open ...

  8. SpringBoot整合Rabbitmq设置消息请求头

    String str = "{\"origin\":\"BBC\",\"origin_coupon_id\":51,\" ...

  9. 转:Vue2.0+组件库总结

    UI组件 element - 饿了么出品的Vue2的web UI工具套件 Vux - 基于Vue和WeUI的组件库 mint-ui - Vue 2的移动UI元素 iview - 基于 Vuejs 的开 ...

  10. Linux 装机必备工具

    linux 装机必备工具:安装这些基本能满足日常需求. #!/usr/bin/env sh   echo "Env"       # vim   # tmux   # ssh   ...