1113. 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 首先要保证n1和n2相差最小无非是相差0或者1,也就是n是偶数或者是奇数,然后再保证s1和s2相差最大,排个序,从中间分开即可。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int s[];
int n;
int main()
{
cin>>n;
for(int i = ;i < n;i ++)
{
cin>>s[i];
}
sort(s,s + n);
int sum = ;
for(int i = ;i < n / ;i ++)
sum += s[n - i - ] - s[i];
if(n % )
{
cout<<<<' '<<sum + s[n / ];
}
else cout<<<<' '<<sum;
}
1113. Integer Set Partition (25)的更多相关文章
- PAT (Advanced Level) 1113. Integer Set Partition (25)
简单题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #in ...
- 【PAT甲级】1113 Integer Set Partition (25分)
题意: 输入一个正整数N(2<=N<=1e5),接着输入N个正整数,将这些数字划分为两个不相交的集合,使得他们的元素个数差绝对值最小且元素和差绝对值最大. AAAAAccepted cod ...
- 1113 Integer Set Partition (25 分)
1113 Integer Set Partition (25 分) Given a set of N (>1) positive integers, you are supposed to pa ...
- PAT 甲级 1113 Integer Set Partition
https://pintia.cn/problem-sets/994805342720868352/problems/994805357258326016 Given a set of N (> ...
- PAT 1113 Integer Set Partition
Given a set of N (>1) positive integers, you are supposed to partition them into two disjoint set ...
- 1113 Integer Set Partition
Given a set of N (>) positive integers, you are supposed to partition them into two disjoint sets ...
- A1113 | Integer Set Partition (25)
太简单了 #include <stdio.h> #include <memory.h> #include <math.h> #include <string& ...
- PAT1113: Integer Set Partition
1113. Integer Set Partition (25) 时间限制 150 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...
- PAT_A1113#Integer Set Partition
Source: PAT A1113 Integer Set Partition (25 分) Description: Given a set of N (>) positive integer ...
随机推荐
- windows下客户端开发hdf--环境搭建
1.引入依赖 <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop- ...
- public,protected,privat区别
关于从基类继承来的方法和属性的保护: --class Pig:public Animal {...} C++不仅允许你对在类里定义的方法和属性实施访问控制,还允许你控制子类可以访问基类里的哪些方法和属 ...
- GZDBHelper
NuGet:GZDBHelper 初始化: public class APIBase : ApiController { protected GZDBHelper.IDatabase db; publ ...
- /usr/lib/update-notifier/updates-available
/usrb/update-notifier/update-motd-updates-available: 49: /usrb/update-notifier/update-motd-updates- ...
- 【Head First Servlets and JSP】笔记16:JSP隐式对象(内置对象)的映射关系
接笔记15. 1.不管是JSP中的<%%>还是<%\=%>最终都将处于servlet的方法体中,那么有没有一种元素可以声明成类的成员呢? ——答案是有,而且非常非常简单,这个元 ...
- 20145240 《Java程序设计》第二次实验报告
20145240 <Java程序设计>第二次实验报告 北京电子科技学院(BESTI)实验报告 课程:Java程序设计 班级:1452 指导教师:娄嘉鹏 实验日期:2016.04.12 实验 ...
- dfs的返回条件
用到dfs时要注意设置函数的返回条件,否则会导致一直wa!!!!!
- Qt Ping
QProcess对象可以直接执行cmd的命令,但是ping操作是会阻塞的,所以需要在子线程里ping QProcess *tempCmd = new QProcess(); tempCmd->s ...
- linux基础(5)-用户及权限
用户与组 用户:使用linux时,需要以用户的身份登陆 组 :用来方便组织管理用户 用户种类 -root用户(ID为0的用户为root用户) -系统用户(1-499) -普通用户(500以上 ...
- 关于nginx性能优化及基本概念
参考文章: Nginx面试中最常见的18道题:http://blog.csdn.net/liyanlei5858/article/details/77924420 Nginx性能优化指南:http:/ ...