题目链接
分析
题目要求两类人群尽可能规模相当,在相当情况下才考虑绝对值差最大化。那么,对于偶数人数,排序后平分即可,对于奇数人数,将多出来的人分给外向型获取更大的绝对值之差
代码实现
#include <iostream>
#include <algorithm>
using namespace std;
int arr[100023];
int main()
{
int n, sumi = 0, sumo = 0;
cin >> n;
for (int i = 0; i < n; ++i)
cin >> arr[i];
sort(arr, arr + n);
for (int i = 0; i < n / 2; ++i)
sumi += arr[i];
for (int i = n / 2; i < n; ++i)
sumo += arr[i];
cout << "Outgoing #: " << (n / 2 + n % 2) << endl;
cout << "Introverted #: " << n / 2 << endl;
cout << "Diff = " << sumo - sumi << endl;
}