1113 Integer Set Partition (25 分)
Given a set of N (>1) 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 ∣n?1??−n?2??∣ is minimized first, and then ∣S?1??−S?2??∣ is maximized.
Input Specification:
Each input file contains one test case. For each case, the first line gives an integer N (2≤N≤10?5??), and then Npositive 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: ∣n?1??−n?2??∣ and ∣S?1??−S?2??∣, 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 /** 2 * Copyright(c) 3 * All rights reserved. 4 * Author : Mered1th 5 * Date : 2019-02-27-13.43.25 6 * Description : A1113 7 */ 8 #include<cstdio> 9 #include<cstring> 10 #include<iostream> 11 #include<cmath> 12 #include<algorithm> 13 #include<string> 14 #include<unordered_set> 15 #include<map> 16 #include<vector> 17 #include<set> 18 using namespace std; 19 const int maxn=100010; 20 int a[maxn]; 21 int main(){ 22 #ifdef ONLINE_JUDGE 23 #else 24 freopen("1.txt", "r", stdin); 25 #endif 26 int n,sum=0; 27 cin>>n; 28 for(int i=0;i<n;i++){ 29 scanf("%d",&a[i]); 30 sum+=a[i]; 31 } 32 sort(a,a+n); 33 int m=n/2,s=0; 34 for(int i=0;i<m;i++){ 35 s+=a[i]; 36 } 37 if(n%2==0){ 38 printf("0 %d",sum-s-s); 39 } 40 else{ 41 printf("1 %d",sum-s-s); 42 } 43 return 0; 44 }
1113 Integer Set Partition (25 分)