(Relax 数论1.19)POJ 3244 Difference between Triplets(找规律..//公式max(a,b,c)-min(a,b,c)=(|a-b|+|b-c|+|a-c)

/*
 * POJ_3244.cpp
 *
 *  Created on: 2013年11月21日
 *      Author: Administrator
 */


#include <iostream>
#include <cstdio>
#include <algorithm>

using namespace std;

typedef long long ll;
const int maxn = 200005;

ll a[maxn];//****这里如果使用int会WA
ll b[maxn];
ll c[maxn];


int main(){
	int n;
	while(scanf("%d",&n)!=EOF,n){
		int i;
		int x,y,z;
		for(i = 0 ; i < n ; ++i){
			scanf("%d%d%d",&x,&y,&z);
			a[i] = x-y;
			b[i] = y-z;
			c[i] = z-x;
		}

		sort(a,a+n);
		sort(b,b+n);
		sort(c,c+n);

		ll sum = 0;
		for(i = 0 ; i < n ; ++i){
			sum += (a[i]+b[i]+c[i])*(2*i-n+1);
		}

		printf("%lld\n",sum/2);
	}

	return 0;
}

你可能感兴趣的:((Relax 数论1.19)POJ 3244 Difference between Triplets(找规律..//公式max(a,b,c)-min(a,b,c)=(|a-b|+|b-c|+|a-c))