记两组的分数和分别为s1和s2,全体成员总和为x=s1+s2,题目的本质其实是在给定四个成员各自的成绩下,寻找最接近x/2的s1(s2),根据此思路出发,由于成员中的最大值max{a,b,c,d}必然要加入两组中的任意一组,所以为了使得最大值所在组更接近x/2,就必须让最大值匹配最小值来构成一组。
数学证明:
不妨记s1为最大值所在组
设:delta = s1 - s2;
x = s1 + s2;
x/2 = (s1 + s2)/2;
s1 - x/2 = (s1 - s2)/2 = delta/2;
故,当|delta|最小时,|s1 - x/2|取最小值,即最大值所在组的分数和此时最接近x/2
注:s2同理,因为s2和s1存在约束关系 x = s1 + s2;
代码如下
#include <algorithm>
#include <cmath>
using namespace std;
int main()
{
int a,b,c,d;
cin >> a >> b >> c >> d;
int e1=max({a,b,c,d});
int e2=min({a,b,c,d});
int x=a+b+c+d;
cout << abs(x-2*e1-2*e2);//确保实力差是一个正值
return 0;
}

京公网安备 11010502036488号