CodeForces - 340C - Tourist Problem

Iahub is a big fan of tourists. He wants to become a tourist himself, so he planned a trip. There are n destinations on a straight road that Iahub wants to visit. Iahub starts the excursion from kilometer 0. The n destinations are described by a non-negative integers sequence a1, a2, …, an. The number ak represents that the kth destination is at distance ak kilometers from the starting point. No two destinations are located in the same place.

Iahub wants to visit each destination only once. Note that, crossing through a destination is not considered visiting, unless Iahub explicitly wants to visit it at that point. Also, after Iahub visits his last destination, he doesn’t come back to kilometer 0, as he stops his trip at the last destination.

The distance between destination located at kilometer x and next destination, located at kilometer y, is |x - y| kilometers. We call a “route” an order of visiting the destinations. Iahub can visit destinations in any order he wants, as long as he visits all n destinations and he doesn’t visit a destination more than once.

Iahub starts writing out on a paper all possible routes and for each of them, he notes the total distance he would walk. He’s interested in the average number of kilometers he would walk by choosing a route. As he got bored of writing out all the routes, he asks you to help him.

Input
The first line contains integer n (2 ≤ n ≤ 105). Next line contains n distinct integers a1, a2, …, an (1 ≤ ai ≤ 107).

Output
Output two integers — the numerator and denominator of a fraction which is equal to the wanted average number. The fraction must be irreducible.

Examples
Input
3
2 3 5
Output
22 3
Note
Consider 6 possible routes:

[2, 3, 5]: total distance traveled: |2 – 0| + |3 – 2| + |5 – 3| = 5;
[2, 5, 3]: |2 – 0| + |5 – 2| + |3 – 5| = 7;
[3, 2, 5]: |3 – 0| + |2 – 3| + |5 – 2| = 7;
[3, 5, 2]: |3 – 0| + |5 – 3| + |2 – 5| = 8;
[5, 2, 3]: |5 – 0| + |2 – 5| + |3 – 2| = 9;
[5, 3, 2]: |5 – 0| + |3 – 5| + |2 – 3| = 8.
The average travel distance is = 1/6*(5+7+7+8+9+8)=22/3

题目大意:
给你N个数a1,a2,a3…,求这N个数的全排列,然后在每种排列的第0个位置上插入0,求Σ|ai-aj| (i=j+1)与n!的比值(最简化)。

思路:刚开始我想用全排列next_permutation求出每种排列,然后暴力计算,但是这样是会TLE的。然后我就开始找别的方法,我们观察到第一个点比较特殊,所以我们进行分类讨论。该思路部分转载自http://www.cnblogs.com/zstu-abc/p/3293099.html

1.当我们走完第一步时,剩下的n-1步一共有(n-1)!种排列,所以第一步走完时和sum1=(a1+a2+a3+…+an)(n-1)!

2.取2个点ai aj , ai-aj这条线段可以出现在第2步到第n步任意一个地方,在所有排列中出现(n-2)!次,则sum2=(|a1-a2|+|a1-a3|+|a2-a3|+…+|an-an-1|)(n-1)(n-2)!*2-----------------乘2的原因:例如,当我们取a1,a2时,会有|a1-a2|,|a2-a1|。

则ans=(sum1+sum2)/n!=(a1+a2+a3+…+an)/n+(|a1-a2|+|a1-a3|+|a2-a3|+…+|an-an-1|)/n。当我们计算(|a1-a2|+|a1-a3|+|a2-a3|+…+|an-an-1|)时如果暴力求解会超时。我们只看正序,我们观察到以a2为结尾的线段只有a1-a2(不考虑a3-a2这种),则S1=a1-a2,以a3为结尾的线段有a1-a3,a2-a3,我们可以把a2-a3分解为a1-a2和a2-a3,则S2=a1-a2+2*(a2-a3)=S1+2*(a2-a3)。以a4为结尾的线段有a1-a4,a2-a4, a3-a4。 a1-a4可分解为a1-a2,a2-a3,a3-a4。a2-a4可分解为a2-a3,a3-a4。则S3=a1-a2+2a2-a3+3(a3-a4)=S2+3*(a3-a4)。
则可得到 Si=S(i-1)+i*|a[i+1]-a[i]|

#include<iostream>
#include<cmath>
#include<algorithm>
#include<stdlib.h>
using namespace std;
typedef long long ll;
int a[100009];
ll gcd(ll x,ll y)
{
	if(y==0)
		return x; 
	else
		return gcd(y,x%y);
}
int main()
{
	ll ans1,ans2,n;
	cin>>n;
	ans1=0;ans2=0;
	for(int i=1;i<=n;i++)
	{
		cin>>a[i];
		ans1+=a[i];
	}
	sort(a+1,a+1+n);
	ll tmp=0;
	for(int i=1;i<n;i++)
	{
		tmp+=(a[i+1]-a[i])*i;
		ans2+=tmp*2;
	}
	tmp=gcd(ans1+ans2,n);
	cout<<(ans1+ans2)/tmp<<" "<<n/tmp<<endl;
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值