PAT_A1113#Integer Set Partition
Source:
Description:
Given a set of N (>) positive integers, you are supposed to partition them into two disjoint sets A1and A2 of n1 and n2 numbers, respectively. Let S1 and S2 denote the sums of all the numbers in A1and A2, 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 231.
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的更多相关文章
- PAT1113: Integer Set Partition
1113. Integer Set Partition (25) 时间限制 150 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...
- 1113 Integer Set Partition (25 分)
1113 Integer Set Partition (25 分) Given a set of N (>1) positive integers, you are supposed to pa ...
- A1113. Integer Set Partition
Given a set of N (> 1) positive integers, you are supposed to partition them into two disjoint se ...
- PAT A1113 Integer Set Partition (25 分)——排序题
Given a set of N (>1) positive integers, you are supposed to partition them into two disjoint set ...
- PAT 甲级 1113 Integer Set Partition
https://pintia.cn/problem-sets/994805342720868352/problems/994805357258326016 Given a set of N (> ...
- 1113. Integer Set Partition (25)
Given a set of N (> 1) positive integers, you are supposed to partition them into two disjoint se ...
- PAT 1113 Integer Set Partition
Given a set of N (>1) positive integers, you are supposed to partition them into two disjoint set ...
- PAT甲级——A1113 Integer Set Partition
Given a set of N (>) positive integers, you are supposed to partition them into two disjoint sets ...
- 1113 Integer Set Partition
Given a set of N (>) positive integers, you are supposed to partition them into two disjoint sets ...
随机推荐
- 51Nod——T 1113 矩阵快速幂
https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1113 基准时间限制:3 秒 空间限制:131072 KB 分值: 40 ...
- SpringBoot多数据源改造(二)
在上一篇的内容中,主要介绍了spring boot项目的多数据源改造的涉及的基本配置及改动.在spring项目中,常用Mybatis做ORM操作数据库,并且分页操作是避免不了的. 因此,这一篇主要介绍 ...
- 实战体验几种MySQLCluster方案
来源:keepLearning的专栏 http://www.2cto.com/database/201504/387166.html 1.背景 MySQL的cluster方案有很多官方和第三 ...
- POJ 2411 插头DP
//插头DP,算是广义路径的吧. /* 我是这样想的,定义填数的为0,未填的为1.然后,初始自然是(0,0).我还定义了整个棋盘的状态,不知是否多此一举. 这样,把轮廓线上的格子状态记录.当(I,J) ...
- ubuntu上java的运行环境jre的安装
如何在Ubuntu 14.04上面安装 java的运行环境 jre 呢,下面直接采用到 oracle 的java 官网下载 对应的 jre 的tar.gz的包 从 root用户切换到 saynoer ...
- springmvc 时间返回格式化
如果是@ResponseBody,可以通过@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss")设置返回的样式: 如果是不是@ResponseBody ...
- jQuery总结04
1 JavaScript 中的 AJAX 的四个实现步骤分别是? 2 如何处理 XMLHttpRequest 对象的兼容问题? 3 jQuery 中的 AJAX 4 jQuery 选择器包括哪些? 5 ...
- CodeForces - 749C Voting
C. Voting time limit per test 1 second memory limit per test 256 megabytes input standard input outp ...
- constraint和index--转载
primary key和unique约束是要依赖index的,下面通过试验来看看他们之间的依赖关系! SQL> select * from tt; ID NA --------- ...
- c++ string 解析ip
比如输入是192.168.80.12-15,解析成192.168.80.12.192.168.80.13.192.168.80.14.192.168.80.15. #include <iostr ...