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 ...
随机推荐
- Installing Fonts programatically C#
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- Flume Channel Selector
Flume 基于Channel Selector可以实现扇入.扇出. 同一个数据源分发到不同的目的,如下图. 在source上可以定义channel selector: 1 2 3 4 5 6 7 8 ...
- 在td中的输入英文为什么不自动换行???
在表格中如果输入纯汉字,表格中的内容会根据表格大小进行换行,若果一个老外不会写汉字,写了一堆英文,表格的宽度会拉的很长,超过规定宽度 解决方法是在table中加上style="table-l ...
- Xamarin是无懈可击还是鸡肋?浅谈对Xamarin的学习
微软宣布跨平台已经有几个年头,当C#代码可以在其他平台运行时,我相信对于每个热爱.net的程序猿还是十分欣慰的,最近工作需要在一直研究和学习.net的跨平台开发Xamarin,网上对其优点总结也是一大 ...
- 为什么内核访问用户数据之前,要做access_ok?【转】
linuxer 案例 比如内核的如下commit引入了一个严重的安全漏洞(编号CVE-2017-5123): 危害 一个攻击案例可以参考: freebuf <Linux内核Waitid系统调用本 ...
- tcpcopy用法
目标: 将线上服务lighttpd(8000端口)的流量引流到线下测试机 一.测试机: tcpcopy-server,接收流量 modprobe ip_queue iptables -L iptabl ...
- Cockpit subscriptions on CentOS 7 - This system is not registered with an entitlement server. You can use subscription-manager to register.
下午安装 cockpit 时,使用 yum 工具的时候哦,出现如下信息: This system is not registered with an entitlement server. You c ...
- AIX mount nfs 文件系统失败
报 mount: 1831-008 的错,配置系统参数后恢复. 操作系统版本为: # oslevel 6.1.0.0 LOG如下: # mount 192.168.240.69:/xyz/xvdh2/ ...
- 请问在EXECUTE IMMEDIATE中如何使用带有引号
转自:http://bbs.csdn.net/topics/300191423 从第一引号向后,如果遇到第二个引号,则看这个引号后面时候有紧挨着的引号,如果有则第二个引号被转义,即该保留该引号后面紧跟 ...
- linux之在当前目录下按照文件大小进行排序的三种方法
当前目录下按照文件大小排序 [root@test23 script]# ls -lSh 总用量 44K -rw-r--r-- 1 root root 2.4K 12月 8 17:24 test.con ...