Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

Re:秀代码的又来了

Posted by yayu_myself at 2009-10-09 21:30:47 on Problem 2231
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:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator