Given a set of N (>) positive integers, you are supposed to partition them into two disjoint sets A​1​​ and A​2​​ of n​1​​ and n​2​​ numbers, respectively. Let S​1​​ and S​2​​ denote the sums of all the numbers in A​1​​ and A​2​​, 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 2​31​​.

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

题意:

  将一个给定的数组分成两部分,使两部分元素的个数之差最小,总和之差最大。

思路:

  将数组排序,从中间开始划分,前一半的和最小,后一半的和最大,满足题意。

Code:

#include <bits/stdc++.h>

using namespace std;

int sum(vector<int> v, int s, int e) {
int sum = 0;
for (int i = s; i <= e; ++i) {
sum += v[i];
}
return sum;
} int main() {
int n;
cin >> n;
vector<int> v(n);
for (int i = 0; i < n; ++i) cin >> v[i];
sort(v.begin(), v.end());
int mid = n / 2;
int preSum = sum(v, 0, mid - 1);
int postSum = sum(v, mid, n - 1);
if (v.size() % 2 == 0) {
cout << 0 << " " << postSum - preSum << endl;
} else {
cout << 1 << " " << postSum - preSum << endl;
}
return 0;
}

1113 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 甲级 1113 Integer Set Partition

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

  3. 1113. Integer Set Partition (25)

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

  4. PAT 1113 Integer Set Partition

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

  5. PAT (Advanced Level) 1113. Integer Set Partition (25)

    简单题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #in ...

  6. 【PAT甲级】1113 Integer Set Partition (25分)

    题意: 输入一个正整数N(2<=N<=1e5),接着输入N个正整数,将这些数字划分为两个不相交的集合,使得他们的元素个数差绝对值最小且元素和差绝对值最大. AAAAAccepted cod ...

  7. PAT1113: Integer Set Partition

    1113. Integer Set Partition (25) 时间限制 150 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...

  8. PAT_A1113#Integer Set Partition

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

  9. A1113. Integer Set Partition

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

随机推荐

  1. docker 上传到docker hub 采坑

    前面是仓库名称 后面可以命名img名字 docker push gaodi2345/wj:docker_gui

  2. void指针及指针的多次赋值的理解

    1.void指针的类型转换 int A::functionCommamd(const DWORD _from,const DWORD _to,const DWORD Event_type,void * ...

  3. web前端学习笔记(二)---Django

    [前言]前面(一)学习了web的基础知识,介绍到了MVC,项目使用一个Django框架. Django book:https://code.ziqiangxuetang.com/django/djan ...

  4. Web性能优化之瘦身秘笈

    Web 传输的内容当然是越少越好,最近一段时间的工作一直致力于 Web 性能优化,这是我近期使用过的一些缩减 Web 体积的手段 这些手段主要是为了减少 Web 传输的内容大小,只有干货 CSS 删除 ...

  5. MySQL:基本操作与常用函数

    基本操作 这里的基本操作为添加.修改.删除数据表中的记录. INSERT语句 -- 通用INSERT: INSERT INTO 表名 (字段1, 字段2, ...) VALUES (值1, 值2, . ...

  6. 设计模式系列之原型模式(Prototype Pattern)——对象的克隆

    说明:设计模式系列文章是读刘伟所著<设计模式的艺术之道(软件开发人员内功修炼之道)>一书的阅读笔记.个人感觉这本书讲的不错,有兴趣推荐读一读.详细内容也可以看看此书作者的博客https:/ ...

  7. burpsuite 隐藏 detectportal.firefox.com

        0x00原由 抓包时经常出现detectportal.firefox.com,不利于我们的渗透工作 0x01解决方法 在输入框输入:about:config 然后设置以下选项:network. ...

  8. &#128681;数分工作了三年,我干了件很酷的事情

    从17年毕业来,一直都在干数据分析的工作.和很多转行的小伙伴一样,没有对口的科班学习,摸不清数据分析具体情况,起初充满着很多迷茫. 在刚开始的1年半中,都是自己从淘宝买些课程,最多时,网盘放了4-5T ...

  9. BeetleX使用bootstrap5开发SPA应用

        在早期版本BeetleX.WebFamily只提供了vuejs+element的集成,由于element只适合PC管理应用开发相对于移动应用适配则没这么方便.在新版本组件集成了bootstra ...

  10. 社区 正式发布了 CoreWCF 0.1.0 GA

    CoreWCF 项目在2021.2.19 正式发布了0.1.0 GA版本:https://github.com/CoreWCF/CoreWCF/releases/tag/v0.1.0 ,这个版本号虽然 ...