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 ...
随机推荐
- sql server使用杂记(二)
存储过程CREATE PROCEDURE [dbo].[getprofitandloss]@agentNo varchar(10),@o0 varchar(30),@source varchar(30 ...
- springMvc--接受日期类型参数处理
这个问题,也即是springMvc如何进行参数类型的转换 , 以把client传过来一个String类型,转换为日期类型为例 步骤 1.controller /** * 接收日期类型参数 * 注意: ...
- 前台JSON对象传给springmvc,解析为map对象
前台JSON对象传给springmvc,解析为map对象 javascript: $.ajax({ url : url, method : 'post', contentType : 'applica ...
- ajax——dom基础
javascript中dom实现能够使我们在ajax中通过javascript代码对html和xml数据进行dom方式操作,从而做到页面的动态改动更新和数据的提取处理. dom概念 dom文档对象模型 ...
- 民意调查Django实现(三)
我们接着第二小节的開始继续我们的旅程. 我们会继续Web-poll应用.而且将会专注于创建公共接口 - "Views". 哲学思想 一个视图是你的Django应用中的一个Web页面 ...
- 20170322js面向对象
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- ES transport client使用
ES transport client bulk insert 传输(transport)客户端 TransportClient利用transport模块远程连接一个elasticsearch集群.它 ...
- 444D
分类 首先我们要对询问分类,如果相差log级别就第一种询问,否则第二种. 第一种直接暴力lower_bound,复杂度玄学 第二种归并,复杂度玄学 但是就是过了.感觉很容易卡. #include< ...
- linux下sh语法(转载)
介绍: 1 开头 程序必须以下面的行开始(必须方在文件的第一行): #!/bin/sh 符号#!用来告诉系统它后面的参数是用来执行该文件的程序. 在这个例子中我们使用/bin/sh来执行程序. 当编写 ...
- C99C新增内容
继上一篇复合文字之后,今天我们继续谈一谈C99C的新特性. C99标准是继C89标准之后的第二个C语言官方标准,于1999年12月1日正式发布,其中对数据类型(增加了对_Bool),关键字(增加了in ...