1113. Integer Set Partition (25)

时间限制
150 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

Given a set of N (> 1) positive integers, you are supposed to partition them into two disjoint sets A1 and A2 of n1 and n2numbers, 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

思路

水题--
1.先求min|n1-n2|
N为奇数时|n1-n2| = 1,反之|n1-n2| = 0;
2.由min|n1-n2| => max|S1-S2|
可以先把输入的数组num排序,然后令s1是num的前(N/2 - 1)个数的和,S2是num剩下数的和,此时|S1-S2|最小。 代码
#include<iostream>
#include<vector>
#include<algorithm>
#include<math.h>
using namespace std;
int main()
{
int N;
while(cin >> N)
{
int n = N % ;
vector<int> nums(N);
for(int i = ;i < N;i++)
{
cin >> nums[i];
}
sort(nums.begin(),nums.end());
int s1 = ,s2 = ;
int index = N/ - ;
for(int i = ;i <= index;i++)
{
s1 += nums[i];
}
for(++index;index < N;index++)
{
s2 += nums[index];
} cout << n << " " << abs(s1-s2) << endl;
}
}

PAT1113: Integer Set Partition的更多相关文章

  1. 1113 Integer Set Partition (25 分)

    1113 Integer Set Partition (25 分) Given a set of N (>1) positive integers, you are supposed to pa ...

  2. PAT_A1113#Integer Set Partition

    Source: PAT A1113 Integer Set Partition (25 分) Description: Given a set of N (>) positive integer ...

  3. A1113. Integer Set Partition

    Given a set of N (> 1) positive integers, you are supposed to partition them into two disjoint se ...

  4. PAT A1113 Integer Set Partition (25 分)——排序题

    Given a set of N (>1) positive integers, you are supposed to partition them into two disjoint set ...

  5. PAT 甲级 1113 Integer Set Partition

    https://pintia.cn/problem-sets/994805342720868352/problems/994805357258326016 Given a set of N (> ...

  6. 1113. Integer Set Partition (25)

    Given a set of N (> 1) positive integers, you are supposed to partition them into two disjoint se ...

  7. PAT 1113 Integer Set Partition

    Given a set of N (>1) positive integers, you are supposed to partition them into two disjoint set ...

  8. PAT甲级——A1113 Integer Set Partition

    Given a set of N (>) positive integers, you are supposed to partition them into two disjoint sets ...

  9. 1113 Integer Set Partition

    Given a set of N (>) positive integers, you are supposed to partition them into two disjoint sets ...

随机推荐

  1. JavaScript进阶(三)常见工具(校验、通用)

    JS常见工具(校验.通用) // 姓名校验 var checkName = function(name) { // 收货人姓名校验(准则:姓名为2-4汉字) var regu = /^[\u4E00- ...

  2. Win8 HTML5与JS编程学习笔记(一)

    微软的Visual Studio提供了多种构成win8应用的方式,其中最让我感到激动的是基于网页设计语言的开发模式,它提供了结合HTML5与Javascript来开发应用的方法,通过这种方法进行开发, ...

  3. SQLServer 基础

    1当设计表时,对表进行结构性的修改(如将原来可以null的改为不可null),直接改则不允许保存修改,需要选择 工具----选项----designers—表设计器和数据库设计器---阻止保持要求重新 ...

  4. 【一天一道LeetCode】 #3 Longest Substring Without Repeating Characters

    一天一道LeetCode (一)题目 Given a string, find the length of the longest substring without repeating charac ...

  5. Linux常用命令(第二版) --Shell应用技巧

    Shell应用技巧 小技巧: 1.命令补全功能: <Tab>键 2.清屏: Ctrl+l 3.删除光标前所有内容: Ctrl+u 4.命令历史记录: history 这时:  !histo ...

  6. 销售行业ERP数据统计分析都有哪些维度?

    场景描述 当前的企业信息化建设主要包括ERP系统.OA系统等.企业希望实现信息系统数据的整合,对企业资源进行分析汇总,方便对企业相关数据的掌控从而便于对业务流程进行及时调整监控. 但是由于系统间数据的 ...

  7. WebStorm常用快捷键总结

    在使用WebStorm的过程中,常用快捷键整理: 1.  必备快捷键 Ctrl+/:注释当前行 Ctrl+Shift+/:当前位置插入注释 Ctrl+Alt+/:块注释,并Focus到首行,写注释说明 ...

  8. JS基础:基于原型的对象系统

    简介: 仅从设计模式的角度讲,如果我们想要创建一个对象,一种方法是先指定它的类型,然后通过这个类来创建对象,例如传统的面向对象编程语言 "C++"."Java" ...

  9. 听《津津乐道》ThinkPad专题节目有感

    自2011年使用Mac以来,就没怎么想过要再换一个windows使用,可是前几天听了<津津乐道>播客节目,主播朱峰讲了ThinkPad的使用经历,这个倒是让我回想起第一次见到IBM电脑时的 ...

  10. MySQL性能调优——索引详解与索引的优化

    --索引优化,可以说是数据库相关优化.理解尤其是查询优化中最常用的优化手段之一.所以,只有深入索引的实现原理.存储方式.不同索引间区别,才能设计或使用最优的索引,最大幅度的提升查询效率! 一.BTre ...