Distance
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 306 Accepted Submission(s): 121
Problem Description
小沃沃所在的世界是一个二维平面。他有 n 个朋友,第 i 个朋友距离他的距离为 a[i],小沃沃并不知道这些朋友具体在什么点上。
请问在最优情况下,小沃沃的朋友两两之间的欧几里得距离的和的最小值是几?
假设小沃沃的位置为 P0=(x0,y0),第 i 个朋友的位置为 Pi=(xi,yi),对于所有的 i,需要满足 dist(P0,Pi)=a[i],并且∑n−1i=1∑nj=i+1dist(Pi,Pj) 最小,其中 dist(X,Y) 为连接点 X 和点 Y 的线段的长度。xi,yi 都可以是任意实数。
请问在最优情况下,小沃沃的朋友两两之间的欧几里得距离的和的最小值是几?
假设小沃沃的位置为 P0=(x0,y0),第 i 个朋友的位置为 Pi=(xi,yi),对于所有的 i,需要满足 dist(P0,Pi)=a[i],并且∑n−1i=1∑nj=i+1dist(Pi,Pj) 最小,其中 dist(X,Y) 为连接点 X 和点 Y 的线段的长度。xi,yi 都可以是任意实数。
Input
第一行一个正整数 test(1≤test≤10) 表示数据组数。
对于每组数据,第一行一个正整数 n(1≤n≤100000)。
接下来一行 n 个整数,第 i 个整数 a[i](1≤a[i]≤1000000000) 表示第 i 个朋友和小沃沃的距离。
对于每组数据,第一行一个正整数 n(1≤n≤100000)。
接下来一行 n 个整数,第 i 个整数 a[i](1≤a[i]≤1000000000) 表示第 i 个朋友和小沃沃的距离。
Output
对每组数据输出一行一个数,表示 ∑n−1i=1∑nj=i+1dist(Pi,Pj) 的最小值。答案需要四舍五入到整数。
Sample Input
2
2
3 5
5
1 2 3 4 5
Sample Output
2
20
Source
Recommend
heyang
析:很明显,所有的人都在一条直线上是最优的,然后这样的话,就可以直接排序,然后计算两个人的距离就可以了。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <list>
#include <assert.h>
#include <bitset>
#include <numeric>
#define debug() puts("++++")
#define print(x) cout<<(x)<<endl
// #define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a, b, sizeof a)
#define sz size()
#define be begin()
#define ed end()
#define pu push_up
#define pd push_down
#define cl clear()
#define lowbit(x) -x&x
#define all 1,n,1
#define FOR(i,n,x) for(int i = (x); i < (n); ++i)
#define freopenr freopen("in.in", "r", stdin)
#define freopenw freopen("out.out", "w", stdout)
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e17;
const double inf = 1e20;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1e5 + 7;
const int maxm = 2000000 + 7;
const LL mod = 1e9 + 7;
const int dr[] = {-1, 1, 0, 0, 1, 1, -1, -1};
const int dc[] = {0, 0, 1, -1, 1, -1, 1, -1};
int n, m;
inline bool is_in(int r, int c) {
return r >= 0 && r < n && c >= 0 && c < m;
}
inline int readInt(){
int x; cin >> x; return x;
}
LL a[maxn];
int main(){
int T; scanf("%d", &T);
while(T--){
scanf("%d", &n);
for(int i = 0; i < n; ++i) scanf("%lld", a + i);
sort(a, a + n);
LL ans = 0;
for(int i = 1; i < n; ++i){
LL det = a[i] - a[i-1];
ans += det * (n-i) * i;
}
printf("%lld
", ans);
}
return 0;
}