排序然后两头开始加,双数一边一半就好,单数看给左边还是右边。
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int n;
vector <int> list;
int main() {
cin >> n;
int temp;
for (int i = 0; i < n; i++) {
scanf("%d",&temp);
list.push_back(temp);
}
sort(list.begin(), list.end());
#ifdef _DEBUG
for (int i = 0; i < list.size(); i++) {
cout << list[i] << endl;
}
#endif
//处理分组
int s1 = 0, s2 = 0;
int n1 = 0, n2 = 0;
int p1 = 0, p2 = list.size() - 1;
while (p1 < p2) {
s1 += list[p1];
s2 += list[p2];
n1++, n2++, p1++, p2--;
}
if (p1 == p2) {//奇数情况
int ts1 = s1 + list[p1], ts2 = s2 + list[p2];
if (abs(ts1 - s2) > abs(ts2 - s1)) {
s1 = ts1, n1++;
}
else {
s2 = ts2, n2++;
}
}
cout << abs(n1 - n2) << " " << abs(s1 - s2) << endl;
return 0;
}