PAT A1113 Integer Set Partition (25 分)——排序题
Given a set of N (>1) positive integers, you are supposed to partition them into two disjoint sets A1 and A2 of n1 and n2 numbers, 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≤105), 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 231.
Output Specification:
For each case, print in a line two numbers: ∣n1−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 <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 分)——排序题的更多相关文章
- 【PAT甲级】1113 Integer Set Partition (25分)
题意: 输入一个正整数N(2<=N<=1e5),接着输入N个正整数,将这些数字划分为两个不相交的集合,使得他们的元素个数差绝对值最小且元素和差绝对值最大. AAAAAccepted cod ...
- A1113 | Integer Set Partition (25)
太简单了 #include <stdio.h> #include <memory.h> #include <math.h> #include <string& ...
- PAT 甲级 1028 List Sorting (25 分)(排序,简单题)
1028 List Sorting (25 分) Excel can sort records according to any column. Now you are supposed to i ...
- PAT A1141 PAT Ranking of Institutions (25 分)——排序,结构体初始化
After each PAT, the PAT Center will announce the ranking of institutions based on their students' pe ...
- PAT A1137 Final Grading (25 分)——排序
For a student taking the online course "Data Structures" on China University MOOC (http:// ...
- PAT A1028 List Sorting (25 分)——排序,字符串输出用printf
Excel can sort records according to any column. Now you are supposed to imitate this function. Input ...
- PAT A1016 Phone Bills (25 分)——排序,时序
A long-distance telephone company charges its customers by the following rules: Making a long-distan ...
- PAT 甲级 1016 Phone Bills (25 分) (结构体排序,模拟题,巧妙算时间,坑点太多,debug了好久)
1016 Phone Bills (25 分) A long-distance telephone company charges its customers by the following r ...
- PAT 甲级 1146 Topological Order (25 分)(拓扑较简单,保存入度数和出度的节点即可)
1146 Topological Order (25 分) This is a problem given in the Graduate Entrance Exam in 2018: Which ...
随机推荐
- 【Java基础】3、Java 位运算(移位、位与、或、异或、非)
public class Test { public static void main(String[] args) { // 1.左移( << ) // 0000 0000 0000 0 ...
- [转*译]Networking API Improvements in Windows 10
在当今,以云优先,移动优先技术为宗旨的时代下,大多数Apps都至少有一些与web服务或网络上其他设备的集成.这些包括应用程序,它获取天气在线内容,新闻或体育比赛的分数,媒体或下载的播客,甚至对 ...
- 解决升级Spark2.0之后,DataFrame map操作报错
当我们在使用spark1.6的时候,当我们创建SQLContext读取一个文件之后,返回DataFrame类型的变量可以直接.map操作,不会报错.但是升级之后会包一个错误,如下: 报错:No imp ...
- ImmediateFunc.js
(function(){ var days = ['Sun','Mon','Tue','Wed','Thu','Fri','Sat'] var today = new Date() var msg = ...
- css居中的方法
- 【HTML笔记】--- 内联元素间距问题及解决方案
一.内联元素间距问题 在HTML实践中我们会发现,有时候内联元素之间会存在一定的间距,并且这间距和margin和padding无关.这是由编辑时的空白字符引起的,并且间距的大小受父元素的font-si ...
- [工作总结]jQuery在工作开发中常用代码片段集锦(1-10)
1.jQuery,JS实现tab切换 原生JS实现 HTML代码如下: <div class="wrap"> <ul id="tag"> ...
- python 类函数,实例函数,静态函数
一.实现方法 class Function(object): # 在类定义中定义变量 cls_variable = "class varibale" def __init__(se ...
- Linux 操作系统主机名变成bogon怎么解决?
主机名变成bogon怎么解决? by:授客 QQ:1033553122 一:使用hostname命令 [laiyu@localhost ~]$ hostname localhost.localdo ...
- @RequestMapping --注解用法详解
一.编码时我们总会发现如下变量未被使用的警告提示: 上述代码编译通过且可以运行,但每行前面的“感叹号”就严重阻碍了我们判断该行是否设置的断点了.这时我们可以在方法前添加 @SuppressWarnin ...