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 ...
随机推荐
- python调用php函数
由于php不支持多线程,所以想借助python搞一个.1.import subprocessimport time#Simple caller, disguard outputmethod=" ...
- 对JAVA的集合的理解
对JAVA的集合的理解是相对于数组 1.数组是大小固定的,并且同一个数组只能存放类型一样的数据(基本类型/引用类型) 2.JAVA集合可以存储和操作数目不固定的一组数据. 3.所有的JAVA集合都位 ...
- 键盘没有Home键和End键的完美解决办法
最近新入手一个笔记本,发现键盘没有Home/End,这两个键虽然不是必用,但也是用顺手了,特别是选择一行,到行首,行尾的时候甚是方便 作为一枚程序员,怎么能够妥协? 于是开始研究 方案一 通过观察笔记 ...
- 测试 js 方法运行时间
早期测速的时候是这样的,呵呵呵,一开始还挺爽的 function testFunctionTime(fn) { var start = new Date().getTime(); if (fn) fn ...
- Hearbeat + Nginx 安装配置
Hearbeat + Nginx 安装配置 实验环境 两台主机:Linux Centos 6.5 32位 主 服务端:Hearbeat + Nginx eth0:192.168.1.160(公网) e ...
- 主攻ASP.NET MVC4.0之重生:发邮箱激活验证
导入Interop.jmail组件 using jmail;using System.Net.Mail; 点击下载源代码 Controller相关代码 public class SendEmailCo ...
- PS小研
1 ps输入字体不显示原因有很多,解决方法也各不相同,我总结了以下几条原因及相应的解决方法 原因一: 字体颜色和背景色相同或者过于相近,字体虽然存在,但是却看不到字体. 解决方法: 这个问题比较简单, ...
- React Native的生命周期解析
在React Native中使用组件来封装界面模块时,整个界面就是一个大的组件,开发过程就是不断优化和拆分界面组件.构造整个组件树的过程. 上张图涵盖了一个组件从创建.运行到销毁的整个过程.大家可以看 ...
- Linux与Android 多点触摸协议【转】
本文转载自:http://blog.csdn.net/xubin341719/article/details/7833277 一.Linux与Android 多点触摸协议 为了使用功能强大的多点触控设 ...
- 单文件夹下的C程序如何编写Makefile文件
通过学习已经学会了GCC的一些基础的命令,以及如何将C语言源代码编译成可执行文件. 我们已经知道在linux环境下编译源码时,常会有以下三个步骤: ./configure make make clea ...