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:
1 /* 2 Data: 2019-05-29 21:38:41 3 Problem: PAT_A1113#Integer Set Partition 4 AC: 08:30 5 6 题目大意: 7 给定N个整数的集合,把他们分为两个部分,要求两部分和的差最大且元素个数差最小 8 */ 9 10 #include<cstdio> 11 #include<algorithm> 12 using namespace std; 13 const int M=1e5+10; 14 int s[M]; 15 16 int main() 17 { 18 #ifdef ONLINE_JUDGE 19 #else 20 freopen("Test.txt", "r", stdin); 21 #endif 22 23 int n,sum=0; 24 scanf("%d", &n); 25 for(int i=0; i<n; i++) 26 scanf("%d", &s[i]); 27 sort(s,s+n); 28 for(int i=0; i<n/2; i++) 29 sum+= (s[n-1-i]-s[i]); 30 if(n%2==0) 31 printf("0 "); 32 else{ 33 printf("1 "); 34 sum += s[n/2]; 35 } 36 printf("%d\n", sum); 37 38 return 0; 39 }