PAT1113: Integer Set Partition
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 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的更多相关文章
- 1113 Integer Set Partition (25 分)
1113 Integer Set Partition (25 分) Given a set of N (>1) positive integers, you are supposed to pa ...
- PAT_A1113#Integer Set Partition
Source: PAT A1113 Integer Set Partition (25 分) Description: Given a set of N (>) positive integer ...
- 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 ...
随机推荐
- 【一天一道LeetCode】#21. Merge Two Sorted Lists
一天一道LeetCode系列 (一)题目 Merge two sorted linked lists and return it as a new list. The new list should ...
- Leetcode_121_Best Time to Buy and Sell Stock
本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/43024967 Say you have an array ...
- 【公开课】【阿里在线技术峰会】何登成:AliSQL性能优化与功能突破的演进之路
MySQL的公开课,可能目前用不上这些,但是往往能在以后想解决方案的时候帮助到我.以下是阿里对公开课的整理 摘要: 本文根据阿里高级数据库专家何登成在首届阿里巴巴在线技术峰会上的分享整理而成.他主要介 ...
- 开发资源库(repositiory)
1.. 52研发网MTK软件 2.一流研发 3. android+MTK/华为的源代码及资料库(CryToCry96) 点击打开链接 4.android+MTK/华为/联想的源代码及资料库(lucka ...
- Using PL/SQL APIs as Web Services
Overview Oracle E-Business Suite Integrated SOA Gateway allows you to use PL/SQL application program ...
- LeetCode之“链表”:Intersection of Two Linked Lists
此题扩展:链表有环,如何判断相交? 参考资料: 编程判断两个链表是否相交 面试精选:链表问题集锦 题目链接 题目要求: Write a program to find the node at whic ...
- C++之默认参数
C++可以为不指定参数提供默认值.一旦给一个参数赋了默认值,后面的所有参数,也都必须为默认值,并且默认值的类型也必须正确,默认值可以在原型或者函数定义中给出,但是不能两个位置同时给出. 接下来我们上代 ...
- Android使用统计图AChartEngine 来展示数据
本文采用的统计图参考:AChartEngine 访问地址 :http://code.google.com/p/achartengine/ 先给出效果图 本文的开发代码主要是这些:对一些代码进行修改 以 ...
- web报表工具FineReport的公式编辑框的语法简介
FINEREPORT用到公式的地方非常多,单元格(以=开头的便被解析为公式),条件显示,数据字典,报表填报属性值定义,图表标题,轴定义,页眉页脚,甚至单元格的其他属性中的鼠标悬浮提示内容都可以写公式, ...
- LeetCode(29)-Plus One
题目: Given a non-negative number represented as an array of digits, plus one to the number. The digit ...