Given a set of NNN (>1> 1>1) positive integers, you are supposed to partition them into two disjoint sets A1A_1A1 and A2A_2A2 of n1n_1n1 and n2n_2n2 numbers, respectively. Let S1S_1S1 and S2S_2S2 denote the sums of all the numbers in A1A_1A1 and A2A_2A2, respectively. You are supposed to make the partition so that ∣n1−n2∣|n_1 - n_2|∣n1−n2∣ is minimized first, and then ∣S1−S2∣|S_1 - S_2|∣S1−S2∣ is maximized.
Input Specification:
Each input file contains one test case. For each case, the first line gives an integer NNN (2≤N≤1052 le N le 10^52≤N≤105), and then NNN positive integers follow in the next line, separated by spaces. It is guaranteed that all the integers and their sum are less than 2312^{31}231.
Output Specification:
For each case, print in a line two numbers: ∣n1−n2∣|n_1 - n_2|∣n1−n2∣ and ∣S1−S2∣|S_1 - S_2|∣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
思路:
由题目可知,当n是偶数时,n1=n2;当n是奇数时,n2-n1=1,计算abs(sum1-sum2),首先对整个集合进行排序,
然后计算即可。
#include<iostream> #include<vector> #include<algorithm> #include<queue> #include<string> #include<map> #include<set> using namespace std; int main() { int n; cin>>n; int a[n+1]; for(int i=1;i<n+1;i++) cin>>a[i]; sort(a+1,a+n+1); if(n%2==0) { long long int sum=0; for(int i=1;i<=n/2;i++) { sum+=a[n-i+1]-a[i]; } cout<<"0 "<<sum; } else { long long int sum=0; for(int i=1;i<=n/2;i++) { sum+=a[n-i+1]-a[i]; } sum+=a[n/2+1]; cout<<"1 "<<sum; } return 0; }