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. fork me on github 彩带设置无效

    挑选彩带地址: https://github.com/blog/273-github-ribbons 发现代码复制粘贴过来,但是在自己博客园上无效,如粘贴如下代码 <a href="h ...

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

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

  3. A java code

    With the help of LiJun I got a piece of JAVA code. With this code, I can do below things like connec ...

  4. [Cypress] Stub a Post Request for Successful Form Submission with Cypress

    In this lesson well stub a POST request and use Cypress commands to fill in and submit a form. We’ll ...

  5. 《python源代码剖析》笔记 Python虚拟机框架

    本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie 1. Python虚拟机会从编译得到的PyCodeObject对象中依次读入每一条字节码指令 ...

  6. COCOS学习笔记--Button类及其相关控件属性

    一.Button介绍 Button就是button.Cocos中提供了Button类对button进行相关的操作.我们看一下Button类继承关系图: 能够看到.Button是继承自Widget类,W ...

  7. Android面试常问的技术问题

    面试时技术经理会问你一些工作中遇到的Android方面的问题.谈谈你所做的项目,和在项目中所扮演的角色. 很多其它内容请參考我的博客:点击打开链接 1.怎样优化ListView? ①Item布局,层级 ...

  8. bzoj4950: [Wf2017]Mission Improbable

    跟着靖靖做题%%%%% 这题一看就觉得和之前的某场模拟赛的一道题很像,找假如某行某列的最大值一样的就可以只堆一个,跑匈牙利就行 一开始以为箱子不能移动-_-! 然后有个坑,大家都知道当这个位置有箱子就 ...

  9. ”W: GPG 错误:http://ppa.launchpad.net lucid Release: 由于没有公钥,无法验证下列签名:“的问题

    在安装更新时,即在运行,命令行sudo apt-get update 或者运行更新管理器的时候,出现如下错误: W: GPG 错误:http://ppa.launchpad.net lucid Rel ...

  10. selenium3 + python - page_source页面源码

    前言: 有时候通过元素的属性的查找页面上的某个元素,可能不太好找,这时候可以从源码中爬出想要的信息.selenium的page_source方法可以获取到页面源码. 本次以博客园为例,先爬取页面源码, ...