1. 程式人生 > >Codeforces Round #527 (Div. 3) CF1092B Teams Forming

Codeforces Round #527 (Div. 3) CF1092B Teams Forming

題目:Teams Forming

思路:貪心就好了,把長度最相近的兩根配對一定是最優的。

程式碼:

#include<bits/stdc++.h>
using namespace std;

#define read(x) scanf("%d",&x)
#define maxn 100

int 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; }