题目:Teams Forming
思路:贪心就好了,把长度最相近的两根配对一定是最优的。
代码:
#include<bits/stdc++.h>
using namespace std;#define read(x) scanf("%d",&x)
#define maxn 100int n;
int a[maxn+5];int main() {
read(n);for(int i=1;i<=n;i++) {
read(a[i]);}sort(a+1,a+n+1);int ans=0;for(int i=1;i<=n;i+=2) {
if(a[i]!=a[i+1]) {
ans+=a[i+1]-a[i];}}printf("%d",ans);return 0;
}