| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
Re:秀代码的又来了In Reply To:秀代码的又来了 Posted by:zjut020 at 2008-11-22 22:23:05 /*
从左向右传和从右向左传是对称的, 所以vol×2;
考虑中间的一小段距离,因为左边的牛的声音
要传到右边的牛的耳朵里,必然要经过一些相邻的小段距离,
这些距离是要被重复计算的,而某一段左边有i+1头牛,
右边有n-i-1头牛,所以是(i+1)×(n-i-1)
*/
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int n;
//这个必须为long long..否则计算sum转化过程中高位可能丢失
long long a[10000];
cin >> n;
for(int i=0; i<n; i++)
cin >> a[i];
sort(a, a+n);
long long sum=0;
for(int i = 0; i < n-1; i++)
sum += (a[i+1]-a[i]) * (i+1) * (n-i-1);
//printf("%I64d\n", sum*2);
cout << sum * 2;
return 0;
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator