PAT甲级——A1113 Integer Set Partition
Given a set of N (>) positive integers, you are supposed to partition them into two disjoint sets A1 and A2 of n1and 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 ∣ 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 Npositive 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
一句话,弱智题
1 #include <iostream> 2 #include <vector> 3 #include <algorithm> 4 using namespace std; 5 int n, s1 = 0, s2 = 0, nMin, sMin; 6 int main() 7 { 8 cin >> n; 9 vector<int>v(n); 10 for (int i = 0; i < n; ++i) 11 cin >> v[i]; 12 sort(v.begin(), v.end()); 13 for (int i = 0; i < n; ++i) 14 { 15 if (i < n / 2) 16 s1 += v[i]; 17 else 18 s2 += v[i]; 19 } 20 cout << n % 2 << " " << s2 - s1 << endl; 21 return 0; 22 }