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.
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??.
For each case, print in a line two numbers: ∣ and ∣, separated by exactly one space.
10 23 8 10 99 46 2333 46 1 666 555
0 3611
13 110 79 218 69 3721 100 29 135 2 6 13 5188 85
1 9359
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 }
PAT_A1113#Integer Set Partition
原文:https://www.cnblogs.com/blue-lin/p/10946599.html