PAT (Advanced Level) Practice 1113 Integer Set Partition (25 分) 凌宸1642

Posted 凌宸00

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PAT (Advanced Level) Practice 1113 Integer Set Partition (25 分) 凌宸1642相关的知识,希望对你有一定的参考价值。

PAT (Advanced Level) Practice 1113 Integer Set Partition (25 分) 凌宸1642

题目描述:

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

译:给定一组 N (>1) 个正整数,你应该将它们分成两个不相交的集合 A1 和 A2 ,分别由 n1 和 n2 个数字组成。 让 S1 和 S2 分别表示 A1 和 A2 中所有数字的总和。 您应该进行分区,以便首先最小化∣n1 −n2 ∣,然后最大化∣S1 −S2 ∣。


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.

译:每个输入文件包含一个测试用例。 对于每种情况,第一行给出一个整数 N(2 ≤ N ≤ 105),然后在下一行中跟随 N 个正整数,用空格分隔。 保证所有整数及其总和小于 231


output Specification (输出说明):

For each case, print in a line two numbers: ∣n1−n2∣ and ∣S1−S2∣, separated by exactly one space.

译:对于每种情况,在一行中打印两个数字:∣n1 −n2 ∣ 和 ∣S1 −S2 ∣,它们之间正好用一个空格隔开 。


Sample Input1 (样例输入1):

10
23 8 10 99 46 2333 46 1 666 555

Sample Output1 (样例输出1):

0 3611

Sample Input2 (样例输入2):

13
110 79 218 69 3721 100 29 135 2 6 13 5188 85

Sample Output2 (样例输出2):

1 9359

The Idea:

理解好题目意思就是:给你一组数据,你给我分成两个集合,并且保证两个集合数量差最小,集合所有元素的和之差最大。(先排序,排完序从中间断开,求和后相减,得出结果并输出)

The Codes:

#include<bits/stdc++.h>
using namespace std ;
int n , n1 , n2 ;
long long sum1 = 0 , sum2 = 0 ; // 不用long long 应该会有测试点过不了
int main(){
	cin >> n ;
	vector<int> num(n) ;
	for(int i = 0 ; i < n ; i ++) cin >> num[i] ;
	sort(num.begin() , num.end()) ;
	n1 = n / 2 ;
	n2 = (n % 2)?(n1 + 1):n1 ;
	for(int i = 0 ; i < n1 ; i ++)sum1 += num[i] ;
	for(int i = n1 ; i < n ; i ++)sum2 += num[i] ;
	cout << n2 - n1 << \' \' << sum2 - sum1 << endl ;
	return 0 ;
}

以上是关于PAT (Advanced Level) Practice 1113 Integer Set Partition (25 分) 凌宸1642的主要内容,如果未能解决你的问题,请参考以下文章

PAT Advanced Level 1043

PAT Advanced Level 1079

PAT Advanced Level 1095

PAT Advanced Level 1038

pat advanced level 1063

PAT Advanced level 1003 Emergency